![图片[1]-Nginx 反向代理功能-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image.png)
反向代理:reverse proxy,指的是代理外网用户的请求到内部的指定的服务器,并将数据返回给用户的一种方式,这是用的比较多的一种方式。
Nginx 除了可以在企业提供高性能的web服务之外,另外还可以将 nginx 本身不具备的请求通过某种预
定义的协议转发至其它服务器处理,不同的协议就是Nginx服务器与其他服务器进行通信的一种规范,
# 主要在不同的场景使用以下模块实现不同的功能
ngx_http_proxy_module: #将客户端的请求以http协议转发至指定服务器进行处理
ngx_http_upstream_module #用于定义为proxy_pass,fastcgi_pass,uwsgi_pass等指令引用的
后端服务器分组
ngx_stream_proxy_module: #将客户端的请求以tcp协议转发至指定服务器处理
ngx_http_fastcgi_module: #将客户端对php的请求以fastcgi协议转发至指定服务器助理
ngx_http_uwsgi_module: #将客户端对Python的请求以uwsgi协议转发至指定服务器处理
![图片[2]-Nginx 反向代理功能-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-1.png)
![图片[3]-Nginx 反向代理功能-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-2.png)
![图片[4]-Nginx 反向代理功能-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-3.png)
![图片[5]-Nginx 反向代理功能-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-4.png)
1、实现 http 反向代理
1.1、反向代理配置参数
proxy_pass;
# 用来设置将客户端请求转发给的后端服务器的主机,可以是主机名(将转发至后端服务做为主机头首部)、IP
地址:端口的方式
# 也可以代理到预先设置的主机群组,需要模块ngx_http_upstream_module支持
# 示例:
location /web {
index index.html;
proxy_pass http://192.168.1.51:8080;
# 8080后面无uri,即无 / 符号,需要将location后面url 附加到proxy_pass指定的url后面,此行为类似于root
# proxy_pass指定的uri不带斜线将访问的/web,等于访问后端服务器http://192.168.1.51:8080/web/index.html,即后端服务器配置的站点根目录要有web目录才可以被访问
# http://nginx-ip/web/index.html ==> http://192.168.1.51:8080/web/index.html
proxy_pass http://10.0.0.18:8080/;
# 8080后面有uri,即有 / 符号,相当于置换,即访问/web时实际返回proxy_pass后面uri内容.此行为类似于alias
# proxy_pass指定的uri带斜线,等于访问后端服务器的http://10.0.0.18:8080/index.html 内
容返回给客户端
}
# http://nginx/web/index.html ==> http://10.0.0.18:8080
# 重启Nginx测试访问效果:
# curl -L http://www.lijiach.com/web
# 如果location定义其uri时使用了正则表达式模式(包括~,~*,但不包括^~),则proxy_pass之后必须不能使用uri; 即不能有/ ,用户请求时传递的uri将直接附加至后端服务器之后
server {
...
server_name HOSTNAME;
location ~|~* /uri/ {
proxy_pass http://host:port; # proxy_pass后面的url 不能加/
}
...
}
http://HOSTNAME/uri/ --> http://host/uri/
proxy_hide_header field;
# 用于nginx作为反向代理的时候,在返回给客户端http响应时,隐藏后端服务器相应头部的信息,可以设置在http,server或location块
# 示例: 隐藏后端服务器ETag首部字段
location /web {
index index.html;
proxy_pass http://10.0.0.18:8080/;
proxy_hide_header ETag;
}
proxy_pass_header field;
# 默认nginx在响应报文中不传递后端服务器的首部字段Date, Server, X-Pad, X-Accel等参数,如果要传递的话则要使用 proxy_pass_header field声明将后端服务器返回的值传递给客户端
# field 首部字段大小不敏感
# 示例:透传后端服务器的Server和Date首部给客户端,同时不再响应报中显示前端服务器的Server字段
proxy_pass_header Server;
proxy_pass_header Date;
proxy_pass_request_body on | off;
# 是否向后端服务器发送HTTP实体部分,可以设置在http,server或location块,默认即为开启
proxy_pass_request_headers on | off;
# 是否将客户端的请求头部转发给后端服务器,可以设置在http,server或location块,默认即为开启
proxy_set_header;
# 可更改或添加客户端的请求头部信息内容并转发至后端服务器,比如在后端服务器想要获取客户端的真实IP的时候,就要更改每一个报文的头部
# 示例:
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
$proxy_add_x_forwarded_for
the “X-Forwarded-For” client request header field with the $remote_addr variable
appended to it, separated by a comma. If the “X-Forwarded-For” field is not
present in the client request header, the $proxy_add_x_forwarded_for variable is
equal to the $remote_addr variable.
proxy_set_header X-Real-IP $remote_addr;
# 添加HOST到报文头部,如果客户端为NAT上网那么其值为客户端的共用的公网IP地址,常用于在日之中记录客户端的真实IP地址。
# 在后端httpd服务器修改配置,添加日志记录X-Forwarded-For字段
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Real-IP}i\"" combined
#在后端服务器查看日志
# tail /var/log/httpd/access_log -f
192.168.1.51 - - [09/Oct/2020:21:50:57 +0800] "HEAD /static/index.html HTTP/1.0" 200- "-" "curl/7.29.0" "192.168.1.52"
proxy_connect_timeout time;
# 配置nginx服务器与后端服务器尝试建立连接的超时时间,默认为60秒,用法如下:
proxy_connect_timeout 6s;
# 60s为自定义nginx与后端服务器建立连接的超时时间,超时会返回客户端504响应码
proxy_read_timeout time;
# 配置nginx服务器向后端服务器或服务器组发起read请求后,等待的超时时间,默认60s
proxy_send_timeout time;
# 配置nginx项后端服务器或服务器组发起write请求后,等待的超时 时间,默认60s
proxy_http_version 1.0;
# 用于设置nginx提供代理服务的HTTP协议的版本,默认http 1.0
proxy_ignore_client_abort off;
# 当客户端网络中断请求时,nginx服务器中断其对后端服务器的请求。即如果此项设置为on开启,则服务器会忽略客户端中断并一直等着代理服务执行返回,如果设置为off,则客户端中断后Nginx也会中断客户端请求并立即记录499日志,默认为off。
proxy_headers_hash_bucket_size 128;
# 当配置了 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_size 512;
# 设置服务器名称hash表的上限大小
proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504;
# 当一台后端朋务器出错,超时,无效首部,500等时,切换至下一个后端服务器提供服务
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END