Nginx反向代理

Nginx反向代理

1.简介
Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
占用内存:1-2M
并发能力:5万/秒(C语言开发), 3万/秒
免费:不花钱
特点:nginx是软件负载均衡
2.Nginx安装
1).解压nginx文件
注意事项:1.不要放C盘 2.不要有中文路径
2).以管理员身份运行,检测进程项
在这里插入图片描述
主进程:主要提供反向代理和负载均衡服务的.
守护进程:防止主进程意外关闭,
所以关闭时,应该先关闭守护进程.
3.Nginx命令
说明:执行nginx命令,必须在nginx.exe文件所在目录中执行.
1). 启动命令 start nginx
2). 停止命令 nginx -s stop
3). 重启命令 nginx -s reload
4. 实现方向代理-入门案例
说明:当访问http://localhost:80时,默认跳转到nginx系统首页.配置Nginx的配置文件路径nginx-1.9.0conf找到nginx.conf

#配置图片服务器
	server {
		listen 80;
		server_name image.ls123.com;

		location / {
			root E:ls123-upload;
			
		}
	}

没有域名的是要配置本地的hosts文件

127.0.0.1       image.ls123.com

5.实现域名反向代理

#配置域名服务器
	server {
		listen 80;
		server_name  manage.ls123.com;

		location / {
			#发起请求
			proxy_pass http://localhost:8091;
			#配置超时时间
			proxy_connect_timeout       3;  
			proxy_read_timeout          3;  
			proxy_send_timeout          3;

		}
	}

6.Nginx实现负载均衡
6.1 轮询方式
特点:根据配置文件的顺序,依次访问tomcat服务器

#配置负载均衡策略  1轮询
	upstream ls123 {
		server localhost:8091;
		server localhost:8092;
		server localhost:8093;
	}
		
	#配置后台管理服务器
	server {
		listen 80;
		server_name manage.ls123.com;

		location / {
			#发起请求
			#proxy_pass http://localhost:8091;
			proxy_pass http://ls123;
		}
	}

6.2权重方式
根据服务器处理能力的不同,采用权重的策略,处理能力强的服务器,尽可能多处理请求

#配置负载均衡策略   2.权重
	upstream ls123 {
		server localhost:8091 weight=6;
		server localhost:8092 weight=3;
		server localhost:8093 weight=1;
	}

6.3IP_hash

#配置负载均衡策略 3.ip_hash
	upstream ls123 {
		ip_hash;
		server localhost:8091 weight=6;
		server localhost:8092 weight=3;
		server localhost:8093 weight=1;
	}

7.Nginx中下线机制
说明:如果nginx中部署tomcat服务器宕机,需要手动修改nginx配置文件

#配置负载均衡策略  
	upstream ls123 {
		server localhost:8091 weight=6 down;
		server localhost:8092 weight=3;
		server localhost:8093 weight=1;
	}

8. 备用机机制
说明:正常情况下,备用的服务器不会为用户提供服务.当主机遇忙时,或者主机全部宕机时.这时备用机采用生效

#配置负载均衡策略  
	upstream ls123 {
		server localhost:8091 weight=6 down;
		server localhost:8092 weight=3 down;
		server localhost:8093 weight=1 backup;
	}

9.Nginx中健康检测机制
说明:
max_fails=1: 心跳检测时,服务没有正常响应的次数.
fail_timeout=60s 如果出现宕机现象,则在60秒内,不会再把请求发往宕机的机器.当到了下一个周期时会重新进行心跳检测.如果检测通过,则会正常的访问服务器.

#配置负载均衡策略  
	upstream ls123 {
		server localhost:8091 max_fails=1 fail_timeout=60s;
		server localhost:8092 max_fails=1 fail_timeout=60s;
		server localhost:8093 max_fails=1 fail_timeout=60s;
	}

配置超时时间:因为nginx默认的超时时间很久,所以需要修改nginx超时时间,当一个请求超过超时时间,则会访问下一台tomcat服务器.

#配置后台管理服务器
	server {
		listen 80;
		server_name manage.ls123.com;

		location / {
			#发起请求
			#proxy_pass http://localhost:8091;
			proxy_pass http://ls123;
			proxy_connect_timeout       3;  
			proxy_read_timeout          3;  
			proxy_send_timeout          3;  
		}
	}

原文地址:https://www.cnblogs.com/szls-666/p/12494196.html