| 
                            
                                  nginx http 代理通过proxy_set_header,返回客户端真实IP地址及端口,而不是代理主机ip 
	
		
			| 1 2 3 4 5 6 7 8 9 | server { listen 80; location / { proxy_set_header Host h o s t : host: host:server_port; # 设置请求头:代理IP:port proxy_set_header X-Real-IP $remote_addr; # 真实客户端地址IP proxy_set_header X-Real-PORT $remote_port; proxy_set_header X-Forwarded-For KaTeX parse error: Double subscript at position 12: proxy_add_x_?forwarded_for; …http_host:88$request_uri; } } |  在nginx中配置proxy_pass时的加不加/的问题要注意proxy_pass后的url最后的/ 当加上了/,相当于是加了路径,则nginx不会保留location中匹配的路径部分 如果没有/,则会把匹配的路径部分保留 例: 
	
		
			| 1 2 3 4 5 6 | location ^~ /static/css/ {     proxy_cache css_cache;     proxy_set_header Host css.ztit.cn;     proxy_pass http://css.ztit.cn/; } |  如上面的配置 如果请求的url是: 
	
		
			| 1 | http://[域名]/static/css/a.css |  会被代理成: 
	
		
			| 1 | http://css.ztit.cn/a.css |  
	
		
			| 1 2 3 4 5 6 | location ^~ /static/css/ {     proxy_cache css_cache;     proxy_set_header Host css.ztit.cn;     proxy_pass http://css.ztit.cn; } |  如上面的配置 如果请求的url是: 
	
		
			| 1 | http://[域名]/static/css/a.css |  则会被代理到: 
	
		
			| 1 | http://css.ztit.cn/static/css/a.css |  关于proxy_pass配置的path问题如果,你不想nginx对你的URI请求被修改 那么,proxy_pass的配置中就不应该带有任何path。 例: 
	
		
			| 1 2 3 4 | location /static/css/ {     proxy_set_header Host $host;     proxy_pass http://127.0.0.1:85; } |  如果请求的url是: 
	
		
			| 1 | http://127.0.0.1/static/css/a.css |  会被代理到: 
	
		
			| 1 | http://127.0.0.1:85/static/css/a.css |  否则,在proxy_pass的配置中有path 
	
		
			| 1 2 3 4 | location /static/css/ {     proxy_set_header Host $host;     proxy_pass http://127.0.0.1:85/path; } |  如果请求的url是: 
	
		
			| 1 | http://127.0.0.1/static/css/a.css |  会被代理到: 
	
		
			| 1 | http://127.0.0.1:85/path/a.css |  nginx 负载均衡关于nginx负载均衡配置的几个状态参数讲解。 
	down,表示当前的server暂时不参与负载均衡。backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。fail_timeout,在经历了max_fails次失败后,暂停服务的时间。max_fails可以和fail_timeout一起使用。 #热备:如果你有2台服务器,当一台服务器发生事故时,才启用第二台服务器给提供服务。 服务器处理请求的顺序:AAAAAA突然A挂啦,BBB… 
	
		
			| 1 2 3 4 | upstream images {     server 192.168.1.50:8080;     server 192.168.1.50:8080 backup;  #热备     } |  #轮询:nginx默认就是轮询其权重都默认为1,服务器处理请求的顺序:ABABABABAB… 
	
		
			| 1 2 3 4 | upstream images1 {     server 192.168.1.50:8080;     server 192.168.1.51:8080;       } |  #加权轮询:跟据配置的权重的大小而分发给不同服务器不同数量的请求。如果不设置,则默认为1。下面服务器的请求顺序为:ABBABBABBABBABB… 
	
		
			| 1 2 3 4 | upstream images2 {     server 192.168.1.50:8080 weight=1;     server 192.168.1.51:8080 weight=2; } |  #ip_hash:nginx会让相同的客户端ip请求相同的服务器。 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | upstream images3 {     server 192.168.1.50:8080;     server 192.168.1.51:8080;     ip_hash; } upstream images4 {     server 192.168.1.50:8080   max_fails=2 fail_timeout=30s;     server 192.168.1.51:8080   max_fails=2 fail_timeout=30s; } server {     listen 80;     server_name _;       location / {         proxy_read_timeout 1800;         proxy_next_upstream http_502 http_504 error timeout invalid_header;         proxy_set_header Host $host;         proxy_set_header X-Forwarded-For $remote_addr;         proxy_pass http://images;     } } |  
 |