Nginx反向代理和负载均衡配置

1、proxy_pass

语法:proxy_pass URL;
配置块:location、if

此配置项将当前请求方向代理到URL参数指定的服务器上,URL可以是主机名或者IP地址加端口。

例1:指定主机名、IP和端口

proxy_pass http://127.0.0.1:8080;
proxy_pass http://www.130.com:8080;

例2:Unix句柄(不常用)

例3:使用负载均衡中的upstream

upstream backend{
     server www.131.com:8080 weight=5 max_fails=3 fail_timeout=30s; #这里不带http
     server www.130.com:8080 weight=1 max_fails=3 fail_timeout=30s;
 }
 
 server{ 
     listen 80;
     location / {
         proxy_pass http://backend; #必须带http
         proxy_redirect off;
 
     }
 }

例4:将http转换成更安全的https

 proxy_pass  https://192.168.80.130:8080

 2、proxy_method(暂不清楚具体的应用)

语法:proxy_method  method
配置块:http、server、location

例如:
proxy_method POST
那么客户端发来的GET请求,在转发时,方法名也会更改为POST

 3、proxy_hide_header、proxy_pass_header

语法:proxy_hide_header  The Header;
语法:proxy_pass_header  The Header;
配置块:http、 server、location

4、proxy_pass_request_body、proxy_pass_request_headers

语法:proxy_pass_request_body  on | off;
语法:proxy_pass_request_headers  on | off;
默认:proxy_pass_request_body on;
默认:proxy_pass_request_headers on;

5、proxy_redirect

语法:proxy_redirect [ default|off|redirect replacement ];
默认:proxy_redirect default;
配置块:http、server、location

当上游服务器返回的响应是重定向或刷新请求(如HTTP响应码是301或者302)时,proxy_redirect可以重设HTTP头部的location或refresh字段。
例如,如果上游服务器发出的响应是302重定向请求,location字段的URL是:http://localhost:8000/two/some/uri/,那么在下面的配置情况下,实际转发给客户端的location是http://frontend/one/some/uri/。
proxy_redirect http://localhost:8000/two/ http://frontend/one/;
这里还可以使用ngx-http-core-module提供的变量来设置新的location字段。例如
proxy_redirect http://localhost:8000/ http://$host:$server_port/;
也可以省略replacement参数中的主机名部分,这时会用虚拟主机名称来填充。例如:
proxy_redirect http://localhost:8000/two/ /one/;
使用off参数时,将使location或者refresh字段维持不变。例如:
proxy_redirect off;

6、proxy_next_upstream

语法:proxy_next_upstream [error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_404 | off ];
默认:proxy_next_upstream error timeout;
配置块:http、server、location

确定在何种情况下请求将转发到下一个服务器。转发请求只发生在没有数据传递到客户端的过程中

此配置项表示当向一台上游服务器转发请求出现错误时,继续换一台上游服务器处理这个请求。前面已经说过,上游服务器一旦开始发送应答,Nginx反向代理服务器会立刻把应答包转发给客户端。因此,一旦Nginx开始向客户端发送响应包,之后的过程中若出现错误也是不允许换下一台上游服务器继续处理的。这很好理解,这样才可以更好地保证客户端只收到来自一个上游服务器的应答。proxy_next_upstream的参数用来说明在哪些情况下会继续选择下一台上游服务器转发请求。
error:当向上游服务器发起连接、发送请求、读取响应时出错。
timeout:发送请求或读取响应时发生超时。
invalid_header:上游服务器发送的响应是不合法的。
http_500:上游服务器返回的HTTP响应码是500。
http_502:上游服务器返回的HTTP响应码是502。
http_503:上游服务器返回的HTTP响应码是503。
http_504:上游服务器返回的HTTP响应码是504。
http_404:上游服务器返回的HTTP响应码是404。
off:关闭proxy_next_upstream功能—出错就选择另一台上游服务器再次转发。

7、 缓存相关设置
proxy_buffer_size
proxy_buffering
proxy_buffers
proxy_busy_buffers_size
proxy_temp_file_write_size
proxy_temp_path(一般采用默认设置)

语法: proxy_buffer_size the_size #设置代理服务器(nginx)保存用户头信息的缓冲区大小
默认值: proxy_buffer_size 4k/8k
配置块: http, server, location
语法: proxy_buffering on|off
默认值: proxy_buffering on
配置块: http, server, location
语法: proxy_buffers the_number is_size; #缓冲区,网页平均在32k以下的话,这样设置
默认值: proxy_buffers 8 4k/8k;
配置块: http, server, location
语法: proxy_busy_buffers_size size;#高负荷下缓冲大小(proxy_buffers*2)
默认值: proxy_busy_buffers_size proxy_buffer_size * 2;
配置块: http, server, location, if
高负荷下缓冲大小(proxy_buffers*2)
语法: proxy_temp_file_write_size size;#设定缓存文件夹大小,大于这个值,将从upstream服务器传
默认值: proxy_temp_file_write_size proxy_buffer_size * 2;
配置块: http, server, location, if

例子:

 proxy_buffering on;
 proxy_buffers 4 32k;
 proxy_buffer_size 4k;
 proxy_busy_buffers_size 64k;
 proxy_temp_path /var/cache/nginx/proxy_temp_test;
 proxy_temp_file_write_size 64k;
语法: proxy_temp_path dir-path [ level1 [ level2 [ level3 ] ;
默认值: $NGX_PREFIX/proxy_temp
上下文: http, server, location

8、proxy_store
proxy_store_access

语法: proxy_store [on | off | path]
默认值: proxy_store off
配置块: http, server, location
语法: proxy_store_access users:permissions [users:permission ...]
默认值: proxy_store_access user:rw
配置块: http, server, location

proxy_store、proxy_store_access主要用户文件镜像,将后端服务器的文件保存在proxy_store中,减少每次从后端服务器获取文件,特别适用于静态文件,但这些文件没有exprice,需要手动清空文件。

proxy_store /var/cache/nginx/proxy_temp_static/abc.jpg;
proxy_store_access user:rw group:rw all:r;

具体的应用:nginx实时生成缩略图到硬盘上

 9、proxy_connect_timeout、proxy_read_timeout、proxy_send_timeout

语法: proxy_connect_timeout timeout_in_seconds
配置块: http, server, location
语法: proxy_read_timeout the_time
默认值: proxy_read_timeout 60
配置块: http, server, location
语法: proxy_send_timeout time_in_seconds
默认值: proxy_send_timeout 60
配置块: http, server, location

proxy_connect_timeout
说明 该指令设置与upstream server的连接超时时间,有必要记住,这个超时不能超过75秒。
这个不是等待后端返回页面的时间,那是由proxy_read_timeout声明的。如果你的upstream服务器起来了,但是hanging住了(例如,没有足够的线程处理请求,所以把你的请求放到请求池里稍后处理),那么这个声明是没有用的,由于与upstream服务器的连接已经建立了。

proxy_read_timeout
连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)。说明 该指令设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应。这个时间不是获得整个response的时间,而是两次reading操作的时间。

proxy_send_timeout
后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据。说明 这个指定设置了发送请求给upstream服务器的超时时间。超时设置不是为了整个发送期间,而是在两次write操作期间。如果超时后,upstream没有收到新的数据,nginx会关闭连接

 10、proxy_set_header、proxy_set_body

语法: proxy_set_header header value
默认值: Host and Connection
上下文: http, server, location
语法: proxy_set_body [ on | off ]
默认值: proxy_set_body off;
上下文: http, server, location, if
 例如:
proxy_set_header Host $host; 
proxy_set_header X-Real-IP $remote_addr; 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
#X-Forwarded-For: client1, proxy1, proxy2的一些列IP字符窜

11、proxy_headers_hash_bucket_size、proxy_headers_hash_max_size

语法: proxy_headers_hash_bucket_size size;
默认值: proxy_headers_hash_bucket_size 64;
配置块: http, server, location, if
#设置头部哈希表大小。
#这将限制头部字段名称的长度大小,如果你使用超过64个字符的头部名可以加大这个值。
语法: proxy_headers_hash_max_size size;
默认值: proxy_headers_hash_max_size 512;
配置块: http, server, location, if
#设置头部哈希表的最大值,不能小于你后端服务器设置的头部总数。

 12、proxy_ignore_client_abort、proxy_intercept_errors

语法: proxy_ignore_client_abort [ on|off ]
默认值: proxy_ignore_client_abort off
配置块: http, server, location
如果客户端断开请求,也保持后端的下载
语法: proxy_intercept_errors [ on|off ]
默认值: proxy_intercept_errors off
配置块: http, server, location

 13、变量

$host、$proxy_host、$proxy_port、$proxy_add_x_forwarded_for、$remote_addr
主要用在:proxy_set_header,注意$host和$proxy_host的区别,默认为$proxy_host,一般情况下都需要将其更改为$host.

完整例子:

user nginx;
worker_processes 1;
worker_cpu_affinity 1;
worker_rlimit_nofile 409600;

pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log info;

events{
use epoll;
worker_connections 65535;
}
http{
include mime.types;
default_type application/octet-stream;

upstream backend{
#server www.131.com:8080 weight=5 max_fails=3 fail_timeout=30s;
server www.130.com:8080 weight=1 max_fails=3 fail_timeout=30s;
}

server{
listen 80;
location / {
proxy_pass http://backend;
proxy_redirect off;

proxy_buffering on;
proxy_buffers 4 32k;
proxy_buffer_size 4k;
proxy_busy_buffers_size 64k;
proxy_temp_path /var/cache/nginx/proxy_temp 1 2;
proxy_temp_file_write_size 64k;

proxy_store /var/cache/nginx/proxy_temp_static/abc.jpg;
proxy_store_access user:rw group:rw all:r;

client_max_body_size 50m;
client_body_buffer_size 256k;

proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;

proxy_set_header Host $host;
proxy_set_header Port $proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_headers_hash_bucket_size 64;
proxy_headers_hash_max_size 51200;
}
}
}

参考官方文档:http://www.nginx.cn/doc/standard/httpproxy.html

相关文章:

Nginx反向代理和负载均衡配置》上有1条评论

  1. Pingback引用通告: Nginx中http_fastcgi_module的配置 | 精彩每一天

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>