nginx代理常用参数
目录
Nginx 代理常用参数
1.添加发往后端服务的请求头信息
Syntax: proxy_set_header field value;
Default: proxy_set_header Host $http_host;
proxy_set_header Connection close;
Context: http, server, location
# 用户请求的时候HOST的值是linux.proxy.com, 那么代理服务会像后端传递请求的还是linux.proxy.com
proxy_set_header Host $http_host;
# 将$remote_addr的值放进变量X-Real-IP中,$remote_addr的值为客户端的ip
proxy_set_header X-Real-IP $remote_addr;
# 客户端通过代理服务访问后端服务, 后端服务通过该变量会记录真实客户端地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
案例
[root@lb01 conf.d]# vim /etc/nginx/conf.d/game.conf
server {
listen 80;
server_name py.test1.com;
location / {
proxy_pass http://172.16.1.7:80;
}
}
1.1.访问超级玛丽小游戏后查看web01的日志文件
[root@lb01 conf.d]# cat /var/log/nginx/accees.log
1.2.修改配置文件
[root@lb01 conf.d]# vim /etc/nginx/conf.d/supermary.conf
server {
listen 80;
server_name py.test1.com;
location / {
proxy_pass http://172.16.1.8:80;
# 用户请求的时候HOST的值是linux.proxy.com, 那么代理服务会像后端传递请求的还是linux.proxy.com
proxy_set_header Host $http_host;
# 将$remote_addr的值放进变量X-Real-IP中,$remote_addr的值为客户端的ip
proxy_set_header X-Real-IP $remote_addr;
# 客户端通过代理服务访问后端服务, 后端服务通过该变量会记录真实客户端地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
1.3.再次访问小游戏,查看日志
2.代理到后端的TCP连接,响应,返回第超时时间
#nginx代理与后端服务器连接超时时间(代理连接超时)
Syntax: proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location
#nginx代理等待后端服务器的响应时间
Syntax: proxy_read_timeout time;
Default: proxy_read_timeout 60s;
Context: http, server, location
#后端服务器数据回传给nginx代理超时时间
Syntax: proxy_send_timeout time;
Default: proxy_send_timeout 60s;
Context: http, server, location
proxy_connect_timeout 1s;
proxy_read_timeout 3s;
proxy_send_timeout 3s;
[root@lb01 conf.d]# vim /etc/nginx/conf.d/supermary.confserver {
listen 80;
server_name youxi.com;
location / {
proxy_pass http://172.16.1.8:80;
proxy_connect_timeout 1s;
proxy_read_timeout 3s;
proxy_send_timeout 3s;
}
}
3.proxy_buffer代理缓冲区
#nignx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端,边收边传, 不是全部接收完再传给客户端
Syntax: proxy_buffering on | off;
Default: proxy_buffering on;
Context: http, server, location
#设置nginx代理保存用户头信息的缓冲区大小
Syntax: proxy_buffer_size size;
Default: proxy_buffer_size 4k|8k;
Context: http, server, location
#proxy_buffers 缓冲区
Syntax: proxy_buffers number size;
Default: proxy_buffers 8 4k|8k;
Context: http, server, location
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
[root@lb01 conf.d]# vim /etc/nginx/conf.d/supermary.conf
server {
listen 80;
server_name youxi.com;
location / {
proxy_pass http://172.16.1.8:80;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
}
}
4.配置nignx代理的优化文件
这9个参数是常用的,所以我们每次配置项目的时候都要加这9个参数,所以我们把它们写在一个配置文件里,写项目的时候直接用include调用
[root@lb01 ~]# vim /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 10s;
proxy_read_timeout 10s;
proxy_send_timeout 10s;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
[root@lb01 conf.d]# vim game.conf
server {
listen 80;
server_name youxi.com;
location / {
proxy_pass http://172.16.1.8:80;
include /etc/nginx/proxy_params; # 在这一步直接调用外部文件就行了
}
}
[root@lb01 conf.d]# systemctl restart nginx
# 再次访问youxi.com网站 然后查看访问日志