form1.cn
Make a little progress every day

Nginx反向代理基本设置的21个指令

01th of November 2016 Linux Nginx 4543

1,proxy_pass
设置被代理服务器的地址,可以主机名,IP加端口号的形势,语法位:proxy_pass URL,下面举例说明:

upstream proxy_sers {
    server 192.168.123.1/URI;
    server 192.168.123.2/URI;
    server 192.168.123.3/URI;
}
server{
    listen 80;
    server_name www.form1.cn;
    location / {
    proxy_pass http://proxy_sers;          #server中指明 http:// 在proxy_pass就不需要指定
    }
}

proxy_pass中URL是否包含URI的问题,当访问 www.form1.cn/server

location /server/ {
    server_name www.form1.cn;
    proxy_pass http://192.168.123.2;
}

由于proxy_pass中URL不包含URI,所以转向的地址为 http://192.168.123.2/server

location /server/ {
    server_name www.form1.cn;
    proxy_pass http://192.168.123.2/loc;
}

由于proxy_pass中URL指明了URI,所以转向的地址为 http://192.168.123.2/loc

proxy_pass http://192.168.123.2;   www.form1.cn/server/  http://192.168.123.2/server/
proxy_pass http://192.168.123.2/;  www.form1.cn/server/  http://192.168.123.2/


2,proxy_hide_header
Nginx在发送HTTP响应时,可以去掉相关的响应头信息:

proxy_hide_header Set-Cookie;


3,proxy_pass_header
默认情况下,Nginx服务器在发送响应报头时,报文头中不包含"Date 、Server、X-Accel"等来自代理服务器的头域信息。proxy_pass_header可以调置这些头域信息以被发送
语法为

proxy_pass_header field;

field 为需要发送的头域

4,proxy_pass_request_body
配置是否将客户端请求的请求体发送给代理服务器。
语法为

proxy_pass_request_body on | off;

默认为开启状态(on)

5,proxy_pass_request_headers
配置是否将客户端请求的请求头发送给代理服务器。
语法为

proxy_pass_request_headers on | off;

默认为开启状态(on)

6,proxy_set_header
可以更改nginx服务接收到的客户端请求的请求头信息,然后将新头发送给被代理服务器。
语法为

proxy_set_header field value;

默认值:

proxy_set_header Host $proxy_host;
proxy_set_header Connection close;


7,proxy_set_body
可以更改nginx服务接收到的客户端请求的请求体信息,然后将新体发送给被代理服务器。
语法为

proxy_set_body value;


8,proxy_bind
强制将与代理主机的连接绑定到指定IP地址。
语法为

proxy_bind address; 其中 address为IP地址


9,proxy_connect_timeout
配置Nginx服务器与后端被代理服务器尝试建立连接的超时时间,
语法为

proxy_connect_timeout time; 其中time为设置的超时时间,默认为 60s;


10,proxy_read_timeout
配置Nginx服务器向后端被代理服务器(组)发出read请求后,等待响应的超时时间,
语法为

proxy_read_timeout time; 其中time为设置的超时时间,默认为 60s;


11,proxy_send_timeout
配置Nginx服务器向后端被代理服务器(组)发出write请求后,等待的响应超时时间,
语法为

proxy_send_timeout time; 其中time为设置的超时时间,默认为 60s;


12,proxy_http_version
设置用于Nginx服务器提供代理服务的HTTP协议版本,
语法为

proxy_http_version 1.0 | 1.1;

默认为 1.0版本。1.1版本支持upsteam服务器组设置中的keepalive指令

13,proxy_method
设置Nginx服务器请求被代理服务器时使用的请求方法,一般为 POST 或者 GET ,设置了该值,客户端的请求方法将被忽略。
语法为

proxy_method POST | GET;


14,proxy_ignore_client_abort
用于设置在客户端中断网络请求时,Nginx服务器是否中断对被代理服务器的请求,
语法为

proxy_ignore_client_abort on | off;

默认为 off ,客户端断了,nginx对被代理服务器也断

15,proxy_ignore_headers
用于设置一些HTTP响应头中的头域,Nginx服务器接收到被代理服务器的响应数据后不会处理被设置的头域
语法为

proxy_ignore_headers field....

field为要设置的HTTP响应头的头域,例如 "X-Accel-Redirect 、X-Accel-Expires 、EXpires 、Cache-Control 、Set-Cookie"

16,proxy_redirect
假设前端url是example.com。后端server域名是csdn123.com,那么后端server在返回refresh或location的时候,host为csdn123.com,显然这个信息直接返回给客户端是不行的,需要nginx做转换,这时可以设置:

proxy_redirect http://csdn123.com  /

nginx会将host及port部分替换成自身的server_name及listen port。不过这种配置对server_name有多个值的情况下支持不好。
我们可以用nginx内部变量来解决这一问题:

proxy_redirect http://csdn123.com http://$host:$server_port


17,proxy_intercept_errors
配置一个状态是开启还是关闭。
开启时:被代理服务器返回状态码为 400或400以上,Nginx服务器使用自己的 error_page。
关闭时:Nginx直接将代理服务器返回的状态码返回给客户端

语法为

proxy_intercept_errors on | off;


18,proxy_headers_hash_max_size
存放HTTP报文头的哈希表的容量
默认为

proxy_headers_hash_max_size 512;


19,proxy_headers_hash_bucket_size
Nginx服务器申请存放HTTP报文头的哈希表容量的单位大小
默认为

proxy_headers_hash_max_size 64;

对(18和19):在启动 Nginx 的时候,有时候会遇到这样的一个错误:
解决办法就是在配置文件中新增以下配置项:

proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;


20,proxy_next_upstream
如果Nginx定义了 upstream 后端服务器组,如果组内有异常情况,将请求顺次交给下一个组内服务器处理
语法为

proxy_next_upstream status...

可以是一个也可以是多个

status = error,timeout,invalid_header,http_500 502 503 504 404,off


21,proxy_ssl_session_reuse
该指令用于配置是否使用基于SSL安全协议的会话连接(htts://)被代理服务器,
语法为

proxy_ssl_session_reuse on | off;

默认为开启 on,如果在错误日志中发现“SSL3_GET_FINISHED:digest check fiailed”的情况,关闭该指令即可