nginx反向代理概念
反向代理也称reverse proxy,指的就是代理外网用户请求到内部指定web服务器,并将数据返回给用户的一种方式。
nginx除了可以在企业提供高性能的web服务之外,另外还可以将本身不具备的请求通过某种预定的协议转发至其他服务器处理,不同的协议就是nginx服务器与其他服务器进行通信的一种规范,主要在不同场景使用以下模块实现不同的功能。
ngx_http_proxy_module: 将客户端的请求以http协议转发至指定服务器进行处理。
ngx_stream_proxy_module:将客户端的请求以tcp协议转发至指定服务器处理。
ngx_http_fastcgi_module:将客户端对php的请求以fastcgi协议转发至指定服务器助理。
ngx_http_uwsgi_module:将客户端对Python的请求以uwsgi协议转发至指定服务器处理。
反向代理部署流程图:
2.反向代理配置参数
2.1 proxy_pass参数
proxy_pass; #用来设置将请求转发后端服务器的主机,可以是主机名、IP地址加端口,也可以代理预先设置的主机群
nginx配置文件设置
location /web {
#proxy_pass http://192.168.227.132:80/;
proxy_pass http://192.168.142.132:80;
index index.html;
}
其中proxy_pass填写后端服务器地址加/和不加/的区别:
proxy_pass http://192.168.227.132/ ---> 访问/var/www/html/index.html 浏览器输入url为http://www.xiaoma.net/web
proxy_pass http://192.168.227.132 ---> 访问/var/www/html/web/index.html 浏览器输入url为http://www.xiaoma.net/web
测试访问结果:
curl -L http://www.magedu.net/web/index.html
2.2 proxy_hide_header参数
proxy_hide_header; #用于nginx作为反向代理的时候,在返回客户端http响应的时候,隐藏后端服务版本相应头部信息,
#可以设置在http/server或者location
location /web {
proxy_pass http://192.168.142.132:80;
proxy_hide_header ETag;
index index.html;
}
2.3 proxy_pass_request_body参数
proxy_pass_request_body
#是否向后端服务器发送HTTP包体部分,可以设置在http/server或location块,默认即为开启
proxy_pass_request_body on | off;
2.4 proxy_pass_request_headers参数
proxy_pass_request_headers
#是否将客户端的请求头部转发给后端服务器,可以设置在http/server或location块,默认即为开启
proxy_pass_request_headers on | off;
2.5 proxy_set_header参数 (重要)
proxy_set_header
#可以更改或添加客户端的请求头部信息内容并转发至后端服务器,比如在后端服务器想要获取客户端的真实IP的 时候,就要更改每一个报文的头部
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header HOST $remote_addr;
#添加HOST到报文头部,如果客户端为NAT上网那么其值为客户端的公共的公网IP地址
2.6 proxy_hide_header参数
proxy_hide_header field
#用于隐藏后端服务器特定的响应首部,默认nginx在响应报文中不传递后端服务器的首部字段data,server,x-pad等
2.7 proxy_connect_timeout time参数
proxy_connect_timeout 65s;
#配置nginx服务器与后端服务器尝试建立连接的超时时间,默认60s
2.8 proxy_http_version参数
proxy_http_version 1.1;
#用于设置nginx提供代理服务的HTTP协议的版本,默认http 1.0
2.9 proxy_read_time参数
proxy_read_tome time;
#配置nginx服务器向后端服务器或服务器组发起read请求后,等待的超时时间,默认60s
proxy_send_time time;
#配置nginx项后端服务器或服务器组发起write请求后,等待的超时时间,默认60s
2.10 proxy_ignore_client_abort参数
proxy_ignore_client_abort off;
#当客户端网络中断请求时,nginx服务器中断其对后端服务器的请求。即如果此项设置为on开启,则服务器会忽 略客户端中断并
#一直等着代理服务执行返回,如果设置为off,则客户端中断后Nginx也会中断客户端请求并立即 记录499日志,默认为off。
2.11 proxy_headers_hash参数
proxy_headers_hash_bucket_size 64;
#当配置了 proxy_hide_header和proxy_set_header的时候,用于设置nginx保存HTTP报文头的hash表的上限。
proxy_headers_hash_max_size 512;
#设置proxy_headers_hash_bucket_size的最大可用空间
server_namse_hash_bucket_size 512; #server_name hash表申请空间大小
server_names_hash_max_szie 512; #设置服务器名称hash表的上限大小
3. 反向代理-缓存参数
缓存功能默认关闭状态
proxy_cache zone | off; #默认off
#指明调用的缓存,或关闭缓存机制;context:http,server,location
proxy_cache_key string;
#缓存中用于键的内容,默认值:proxy_cache_key $scheme$proxy_host$request_uri
proxy_cache_vaild [code...] time;
#定义对特定响应码的响应内容的缓存时长,定义在http中
#proxy_cache_vaild 200 302 10m;
#proxy_cache_vaild 404 1m;
proxy_cache_path;
#定义用于proxy功能的缓存;context:http
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];
示例:在http配置定义的缓存
proxy_cache_path /var/cache/nginx/prooxy_cache #定义缓存保存路径,自动创建
levels=1:2:2 #定义缓存目录结构层次,1:2:2可以生成1048576个目录
keys_zone=proxycache:20m #指内存中缓存的大小,主要用于存放key和metadata
inactive=120s #有效缓存时间
max_size=1g #最大磁盘占用空间
proxy_cache_methods GET | HEAD | POST...;
#对哪些客户端请求方法对应的的响应进行缓存,GET和HEAD方法总是被缓存
proxy_set_header field value;
#设定发往后端主机的请求报文的请求首部的值 context:http,server,location
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;