Nginx Reverse Proxy and Load Balancing Deployment Guide

 1.        Installation

1) Download the installation package of the latest version of Nginx (currently version 1.5.13 ) from the Nginx official website download page ( http://nginx.org/en/download.html );

2) Unzip and copy to the deployment directory.

 

2.        Start and stop Nginx

Nginx currently only supports command line operations. Before the operation, enter the Dos command environment and enter the Nginx deployment directory.

1) Start Nginx: start nginx

2) Stop Nginx: nginx -s stop

3) Restart after modifying the configuration: nginx -s reload

These three commands can be made into bat files respectively and placed in the deployment directory to facilitate subsequent operations.

start nginx.bat file content: start nginx

stop nginx.bat file content: nginx -s stop

Reload nginx.bat file content: nginx -s reload

 

3.        Reverse proxy configuration

Modify the content of the nginx.conf file (such as nginx-1.5.13\conf\nginx.conf) in the conf subdirectory of the deployment directory to adjust the related configuration.

Example of reverse proxy configuration:

location / {

        #Set the host header and the real address of the client so that the server can get the real IP of the client

             proxy_set_header Host $host;

             proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 

             #disable cache

             proxy_buffering off;

 

             #设置反向代理的地址

             proxy_pass http://192.168.1.1;       

      }

代理地址根据实际情况修改。

 

4.        负载均衡配置

nginx 的 upstream默认是以轮询的方式实现负载均衡,这种方式中,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

另外一种方式是ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 

 

负载均衡配置示例:

///nginx相对路劲的会直接加上项目名,2upstram用ip(代理)地址

upstream backend {

             #ip_hash;//iphash和权重选择一个

 

//tomcat发布到root方法在负载时直接ip+端口即可访问(因为这里不可以配置项目名只能ip+端口)(见tomcat一文)

             server 192.168.1.251;

             server 192.168.1.252;

             server 192.168.1.247;

 

         }

 

server {

        listen       80;

        server_name  trffweb;

 

        location / {

 

             #反向代理的地址

             proxy_pass http://backend;     

        }

}

 

Upstream命名和服务器地址根据实际情况修改。

 

5.        完整配置示例

nginx.conf:

 

worker_processes  1;

events {

    worker_connections  1024;

}

 

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

 

    upstream backend {

             #ip_hash;

             server 192.168.1.251;

             server 192.168.1.252;

             server 192.168.1.247;

         }

 

    server {

        listen       80;

        server_name  2;

 

        location / {

        #设置主机头和客户端真实地址,以便服务器获取客户端真实IP

             proxy_set_header Host $host;

             proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 

             #禁用缓存

             proxy_buffering off;

 

             #反向代理的地址

             proxy_pass http://backend;     

        }

    }

 

}

 

 

注意:

 

nginx 错误502 upstream sent too big header while reading response header from upstream

按字面意思理解应该是upstream负载均衡的模块转发的header头超出限制值了,查看配置文件中的相关配置,并搜索相关信息。

网上同类型的错误原因,说是cookie携带的header太多了

和tomcat设置maxHttpHeaderSize一样

 <Connector port="8080" maxHttpHeaderSize="102400" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />

 

 

在http配置段设置
{
                proxy_buffer_size  128k;

                proxy_buffers   32 32k;

                proxy_busy_buffers_size 128k;
        
        proxy_temp_file_write_size 128k;
}
proxy_temp_file_write_size 这个选项的值不能小于proxy_buffer_size,否则报错。
优化后,错误日志中没有继续出现报错。
另外,如果你用nginx做负载均衡的话,改了上述参数没用的的话,在转发的配置上,修改proxy_buffer_size、proxy_buffers、proxy_busy_buffers_size的值。
配置示例:
location / {

                proxy_buffer_size  128k;

                proxy_buffers   32 32k;

                proxy_busy_buffers_size 128k;

                add_header X-Static transfer;

                proxy_redirect off;

                proxy_set_header Host $host;

                proxy_set_header X-Real-IP  $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                proxy_pass http://backend;    

        }

 

总结原因:
proxy是nginx作为client转发时使用的,如果header过大,超出了默认的1k,就会引发上述的upstream sent too big header。

fastcgi_* 可以理解成nginx接受client请求时的响应使用的。

 

参看:

http://blog.csdn.net/haitun312366/article/details/12647237

http://www.nginx.cn/76.html  //配置项的说明

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326159598&siteId=291194637