nginx 反代功能:
一、基本配置
server {
listen 172.16.31.124:80;
server_name ns2.cpe.com;
root /var/www/html/;
location / {
proxy_pass http://172.16.31.125:80;
proxy_set_header Host $host;
# $host 请求服务器的地址
proxy_set_header X-Real-IP $remote_addr;
# $remote_addr 客户机ip地址
}
}
1、反代转发
(1)例如:
location /repo/ {
proxy_pass http://172.16.31.125/soft/;
}
被映射了
访问http://172.16.31.124/repo/相当于直接访问的http://172.16.31.125/soft/
(2)例如:
location ~*\.(jpg|png|gif)$ {
proxy_pass http://172.16.31.125;
}
#不管后面是什么 都会补在后面; http://172.16.31.125;后面不能跟任何东西。
直接http://172.16.31.124/1.jpg可以访问到主机http://172.16.31.125/1.jpg
访问http://172.16.31.124/soft/1.jpg可以访问到主机http://172.16.31.125/soft/1.jpg
2、传递参数给后端服务器记录日志:
(1)重新编译nginx加入:–with-http_realip_module
# ./configure –prefix=/usr/local/nginx –conf-path=/etc/nginx/nginx.conf –user=nginx –group=nginx –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx/nginx.pid –lock-path=/var/lock/nginx.lock –with-http_ssl_module –with-http_stub_status_module –with-http_gzip_static_module –with-http_flv_module –with-http_mp4_module –http-client-body-temp-path=/var/tmp/nginx/client –http-proxy-temp-path=/var/tmp/nginx/proxy –http-fastcgi-temp-path=/var/tmp/nginx/fastcgi –with-http_realip_module
(2)前端服务器配置
location /repo/ {
proxy_pass http://172.16.31.125/soft/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
(3)后端服务器配置
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include server.conf;
set_real_ip_from 172.16.31.124;
real_ip_header X-Real-IP;
log_format main ‘$http_X-Real-IP – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
}
3、反代常用参数
(1)传递头部信息参数
proxy_hide_header field; 设定额外的不从代理服务器传递给客户端的head信息。
proxy_pass_header field;By default, nginx does not pass the header fields “Date”, “Server”, “X-Pad”, and “X-Accel-…” from the response of a proxied server to a client。默认情况nginx不传递”Date”,”Server”,”X-pad”等参数(经测试除去了hider头部全部传递给了客户端而pass的header倍代理服务期修改为自己的信息传递给了客户端)
Syntax: proxy_hide_header field;
Default: —
Context: http, server, location
Syntax: proxy_pass_header field;
Default: —
Context: http, server, location
(2)关于代理长短连接配置
proxy_http_version; 1.0用短链接1.1用于长连接。当缓存服务器时朝后端服务器建议使用短连接。
1.0 | 1.1;By default, version 1.0 is used. Version 1.1 is recommended for use with keepalive connections and NTLM authentication.
Syntax: proxy_http_version 1.0 | 1.1;
Default: proxy_http_version 1.0;
Context: http, server, location
(3)反代连接超时 proxy_connect_timeout
Syntax: proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location
(4)等待代理服务器响应超时时间,一般不做设定。proxy_read_timeout上游服务器响应的超时时间,而connectiontimeout是连接的超时时间。
Syntax: proxy_read_timeout time;
Default: proxy_read_timeout 60s;
Context: http, server, location
(5)是否把客户端的body部分通过代理服务器传递给上游服务器。proxy_pass_request_body
Syntax: proxy_pass_request_body on | off;
Default: proxy_pass_request_body on;
Context: http, server, location
(6)是否把客户端请求报文头部请求原来不动发到上游服务器。proxy_pass_request_headers
Syntax: proxy_pass_request_headers on | off;
Default: proxy_pass_request_headers on;
Context: http, server, location
(7)代理连接超时时间,proxy_connect_timeout官方建议不设置超过75s。tcp连接。
Syntax: proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location
(8)典型配置及参数说明
编辑配置文件在http{}加入:
# vim nginx.conf
proxy_connect_timeout 20s;
#代理建立连接的时长,默认60s,官方建议不超过70s;
proxy_pass_header Server;
#不传递真实server版本信息给客户端;
proxy_hide_header Last-Modified;
#指定不传递的给客户端的header;
proxy_http_version 1.0;
#与上游服务器使用短连接;
proxy_read_timeout 60s;
#上游服务器响应超时时间;
proxy_pass_request_body on;
#直接传递客户端的请求body部分不重新构建请求报文给上游服务器
proxy_pass_request_headers on;
#直接传递客户端的请求headers给上有服务不重新构建头部信息
4、代理缓存设置
(1)定义缓存在http{}加入:
Syntax: proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
Default: —
Context: http
例如:
proxy_cache_path /cache/nginx/ levels=1:2:3 keys_zone=cpe:32m inactive=20m;
#缓存路径,几级子目录,每级子目录多少字段,缓存名字,缓存空间大小
(2)使用缓存(缓存可以定义多组,需要的时候可以调用一个或者多个)
Syntax: proxy_cache zone | off;
Default: proxy_cache off;
Context: http, server, location
例如:
proxy_cache cpe;
#使用cpe缓存
(3)缓存的方法设置
Syntax: proxy_cache_methods GET | HEAD | POST …;
Default: proxy_cache_methods GET HEAD;
Context: http, server, location
例如:
proxy_cache_methods GET HEAD;
#缓存方法为GET HEAD的;
(4)被请求多少次才会被缓存
Syntax: proxy_cache_min_uses number;
Default: proxy_cache_min_uses 1;
Context: http, server, location
例如:
proxy_cache_min_uses 1;
#请求最小一次就缓存;
(5)删除缓存proxy_cache_purge
(6)proxy_cache_revalidate:缓存到起后是否重新检验
Syntax: proxy_cache_revalidate on | off;
Default: proxy_cache_revalidate off;
Context: http, server, location
proxy_cache_revalidate on;
#缓存到期后重新检查后,重新启用;
(7)缓存过期后,无法连接后端,是否使用过期的缓存,如何使用
Syntax: proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | off …;
Default: proxy_cache_use_stale off;
Context: http, server, location
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504 http_403 http_404;
#为下面情况,可以使用过期缓存
(8)按时间定义不同缓存的相应码的
Syntax: proxy_cache_valid [code …] time;
Default: —
Context: http, server, location
例如:
proxy_cache_valid 200 302 10m;
#缓存状态码为:200 302 10分钟;
proxy_cache_valid 301 1h;
#缓存状态码为:301的一小时;
proxy_cache_valid any 1m;
#缓存其它的1分钟;
(9)缓存配置案例
(1)创建缓存文件夹
# mkdir -pv /cache/nginx/
# chown -R nginx:nginx /cache/nginx/
(2)编辑配置文件在httpd段加入
# vim nginx.conf
proxy_cache_path /cache/nginx/ levels=1:2:3 keys_zone=cpe:32m inactive=20m;
#缓存路径,几级子目录,每级子目录多少字段,缓存名字,缓存空间大小
(3)在location中使用缓存,编辑配置文件server.conf在location段中加入使用缓存配置
# vim /etc/nginx/conf.d/server.conf
proxy_cache cpe;
#使用cpe缓存
(4)缓存优化配置,编辑配置文件server.conf在location段中加入使用缓存配置;
# vim /etc/nginx/conf.d/server.conf
proxy_cache_valid 200 302 10m;
#缓存状态码为:200 302 10分钟;
proxy_cache_valid 301 1h;
#缓存状态码为:301的一小时;
proxy_cache_valid any 1m;
#缓存其它的1分钟;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504 http_403 http_404;
#为下面情况,可以使用过期缓存。
proxy_cache_revalidate on;
#缓存到期后重新检查后,重新启用;
proxy_cache_min_uses 1;
#请求最小一次就缓存;
proxy_cache_methods GET HEAD;
#缓存方法为GET HEAD的;
4、代理buffer相关配置
(1)proxy_buffering 启用或禁用缓存代理服务器的响应。
Syntax: proxy_buffering on | off;
Default: proxy_buffering on;
Context: http, server, location
proxy_buffering启用的时候,代理nginx服务收到上游服务器的响应,将立即被放入缓冲里(由the proxy_buffer_size 和 proxy_buffers 定义)。如果整个缓冲无法完全放入缓冲区里,一部分将被存到磁盘的临时文件中(由proxy_max_temp_file_size和 proxy_temp_file_write_size定义)。
proxy_buffering关闭的时候,nginx将立即传递相应给客户端。nginx将不试图读取整个响应了。nginx最大每次从服务器收到的数据大小由proxy_buffer_size指定。
proxy_buffering也可以通过从响应报文header:”X-Accel-Buffering” 传递”yes” or “no” 启用或者关闭。 也可以被proxy_ignore_headers参数忽略掉。
(2)proxy_buffer_size 从代理后端服务器取得的第一部分的响应内容大小;
Syntax: proxy_buffer_size size;
Default: proxy_buffer_size 4k|8k;
Context: http, server, location
该指令设置缓冲区大小,从代理后端服务器取得的第一部分的响应内容,会放到这里小的响应header通常位于这部分响应内容里边.默认来说,该缓冲区大小等于指令 proxy_buffers所设置的;但是,你可以把它设置得更小.
(3)proxy_buffers 设定每个连接的缓冲数量以及大小。
Syntax: proxy_buffers number size;
Default: proxy_buffers 8 4k|8k;
Context: http, server, location
默认情况等于内存窗口大小4k或者8k,基于平台配置。
(4)proxy_busy_buffers_size
Syntax: proxy_busy_buffers_size size;
Default: proxy_busy_buffers_size 8k|16k;
Context: http, server, location
(5)典型配置
编辑配置文件在http{}加入:
# vim nginx.conf
proxy_buffer_size 8k;
#代理首部缓冲大小
proxy_buffers 4 128k;
#单个连接缓冲大小
proxy_busy_buffers_size 256k;
#忙时缓冲大小
proxy_temp_file_write_size 256k;
#临时文件大小
proxy_max_temp_file_size 128m;
#最大临时文件大小