nginx--反向代理

server/2024/9/24 10:47:26/

反向代理

指的是代理外网用户的请求到内部的指定web服务器器,并将数据返回给用户的一种方式,这是用的比较多的一种方式

 模块和功能

  • ngx_http_proxy_module: 将客户端的请求以http协议转发至指定服务器进行处理。
  • ngx_stream_proxy_module:将客户端的请求以tcp协议转发至指定服务器处理。
  • ngx_http_fastcgi_module:将客户端对php的请求以fastcgi协议转发至指定服务器助理。
  • ngx_http_uwsgi_module:将客户端对Python的请求以uwsgi协议转发至指定服务器处理

 逻辑调用关系

 

 生产部署结构

 实现http反向代理

 访问逻辑图

部署apache服务器

web1

yum install -y httpd 
echo "web2 192.168.33.171" > /var/www/html/index.html
systemctl enable --now httpd

web2

 yum install -y httpdecho "web2 192.168.33.149" > /var/www/html/index.htmlsystemctl enable --now httpd

nginx反向代理参数

Module ngx_http_proxy_module

proxy_pass;用来设置将客户端请求转发给的后端服务器器的主机,可以是主机名、IP地址:端口的方式,也可以代理到预先设置的主机群组,需要模块ngx_http_upstream_module支持。

server {listen 80;listen       443 ssl;ssl_certificate  /apps/nginx/certs/www.fxq.com.crt;ssl_certificate_key /apps/nginx/certs/www.fxq.com.key;ssl_session_cache shared:sslcache:20m;ssl_session_timeout 10m;server_name www.fxq.com;location / {root /data/nginx/html/pc;index index.html;}location = /favicon.ico {root /data/nginx/html/pc;}location /web1{proxy_pass http://192.168.33.149:80/;带斜线,等于访问后端服务器器的http://http://192.168.33.149:80/index.html 内容返回给客户端index index.html;}location /web2{proxy_pass http://192.168.33.171:80;不带斜线将访问的/web,等于访问后端服务器http://http://192.168.33.171:80/web/index.html,即后端服务器配置的站点根目录要有web目录才可以被访问index index.html;}} 

proxy_hide_header field;用于nginx作为反向代理的时候,在返回给客户端http响应的时候,隐藏后端服务版本相应头部的信息,可以设置在http/server/location块,

 location /web2{proxy_pass http://192.168.33.171:80/;index index.html;proxy_hide_header Etag;} }

proxy_pass_header field;  默认nginx在响应报文中不传递后端服务器器的首部字段Date,Server, X-Pad, X-Accel等参数,如果要传递的话则要使用 proxy_pass_header field声明将后端服务器返回的值传递给客户端。

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_set_header X-Forwarded-For $remote_addr; 添加HOST到报文头部,如果客户端为NAT上网那么其值为客户端的共用的公网IP地址,常用于在日之中记录客户端的真实IP地址。

proxy_connect_timeout time;配置nginx服务器与后端服务器尝试建立连接的超时间,默认为60秒,⽤法如下:
proxy_connect_timeout 60s;60s为自定义nginx与后端服务器建立连接的超时

proxy_read_time time;配置nginx服务器向后端服务器或服务器组发起read请求后,等待的超时间,默认60s
proxy_send_time 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_names_hash_bucket_size 512; server_name hash表申请空间大小
server_names_hash_max_szie 512; 设置服务器名称hash表的上限大小 

nginx缓存功能

参数详解

proxy_cache zone | off; 默认off 指明调用的缓存,或关闭缓存机制;Context:http, server, location

 proxy_cache_key string; 缓存中用于“键”的内容

默认值:proxy_cache_key $scheme $proxy_host $request_uri

 proxy_cache_valid [code ...] time; 定义对特定响应码的响应内容的缓存时长,定义在http{...}中

proxy_cache_valid 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/proxy_cache 定义缓存保存路径,proxy_cache会自动创建
levels=1:2:2 定义缓存目录结构层次,1:2:2可以⽣生成2^4x2^8x2^8=1048576个目录
keys_zone=proxycache:20m 指内存中缓存的大小,主要用于存放key和metadata(如:使用次数)
inactive=120s; 缓存有效时间
max_size=1g; 最大磁盘占用空间,磁盘存入文件内容的缓存空间最大值

调⽤用缓存功能,需要定义在相应的配置段,如server{...};或者location等
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 10m; #指定的状态码返回的数据缓存多⻓长时间
proxy_cache_valid any 1m;

 proxy_cache_use_stale error http_502 http_503; 在被代理的后端服务器出现哪种情况下,可直接使用过期的缓存响应客户端

proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ; #默认是off

 proxy_cache_methods GET | HEAD | POST ...; 对哪些客户端请求方法对应的响应进行存,GET和HEAD方法总是被缓存

案例

非缓存场景压测

web配置

 cd /var/www/html/

 cp /var/log/messages /var/www/html/index.html

 systemctl restart httpd

ab命令使用

参考:https://www.cnblogs.com/niuben/p/18139837

命令格式: ab [选项] [URL]选项包括:
-n 请求总数:指定要发送的请求数量。
-c 并发数:指定同时发送请求的并发数量。
-t 测试时间:指定测试的时间长度。
-p POST文件:使用POST方法时,指定包含POST数据的文件。
-T POST内容类型:指定POST数据的内容类型。
-k 保持连接:启用HTTP KeepAlive功能,允许单个连接发送多个请求。
-v 显示详细输出:显示每个请求的详细结果。
-i 忽略不可用的输出:在输出中忽略无效的请求。
-x 提供代理服务器信息:使用代理服务器进行测试。
-X 代理认证类型:指定代理服务器的认证类型。
-C 提供Cookie信息:发送Cookie到服务器。
-H 添加请求头:添加自定义的HTTP请求头。
-A 添加认证信息:添加认证信息到请求头中。
-m HTTP方法:指定HTTP方法,如GET、POST等。
-s 超时时间:指定每个请求的超时时间。
-g 生成gnuplot格式的输出:将结果保存为gnuplot格式的文件。
-e 输出CSV格式:将结果保存为CSV格式的文件。
-f HTML输出:将结果保存为HTML格式的文件。URL:测试的目标URL,[http[s]://]hostname[:port]/path。
Server Software:        Apache  		#服务器软件
Server Hostname:        json.im   #域名
Server Port:            80 				#请求端口号Document Path:          /   			#文件路径
Document Length:        40888 bytes 	#页面字节数Concurrency Level:      10   			#请求的并发数
Time taken for tests:   27.300 seconds  #总访问时间
Complete requests:      1000   			#请求成功数量
Failed requests:        0      			#请求失败数量
Write errors:           0
Total transferred:      41054242 bytes  #请求总数据大小(包括header头信息)
HTML transferred:       40888000 bytes  #html页面实际总字节数
Requests per second:    36.63 [#/sec] (mean)  #每秒多少请求,这个是非常重要的参数数值,服务器的吞吐量
Time per request:       272.998 [ms] (mean)     #用户平均请求等待时间 
Time per request:       27.300 [ms] (mean, across all concurrent requests)# 服务器平均处理时间,也就是服务器吞吐量的倒数 
Transfer rate:          1468.58 [Kbytes/sec] received  #每秒获取的数据长度Connection Times (ms)min  mean[+/-sd] median   max
Connect:       43   47   2.4     47      53
Processing:   189  224  40.7    215     895
Waiting:      102  128  38.6    118     794
Total:        233  270  41.3    263     945Percentage of the requests served within a certain time (ms)50%    263    #50%用户请求在263ms内返回66%    271    #66%用户请求在271ms内返回75%    279    #75%用户请求在279ms内返回80%    285    #80%用户请求在285ms内返回90%    303    #90%用户请求在303ms内返回95%    320    #95%用户请求在320ms内返回98%    341    #98%用户请求在341ms内返回99%    373    #99%用户请求在373ms内返回100%    945 (longest request)

 ab -n 2000 -c200 http://www.fxq.com/web1/index.html

 

 配置缓存设置

vim /apps/nginx/conf/nginx.conf···
http {include       mime.types;default_type  application/octet-stream;proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
···
[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf
server {listen 80;listen       443 ssl;ssl_certificate  /apps/nginx/certs/www.fxq.com.crt;ssl_certificate_key /apps/nginx/certs/www.fxq.com.key;ssl_session_cache shared:sslcache:20m;ssl_session_timeout 10m;server_name www.fxq.com;location / {root /data/nginx/html/pc;index index.html;}location = /favicon.ico {root /data/nginx/html/pc;}location /web1{proxy_pass http://192.168.33.149:80/;index index.html;proxy_cache proxycache;proxy_cache_key $request_uri;proxy_cache_valid 200 302 301 1h;proxy_cache_valid any 3m;}location /web2{proxy_pass http://192.168.33.171:80/;index index.html;proxy_hide_header Etag;} }

添加头部报文信息

Module ngx_http_headers_module

参数

添加自定义首部

add_header name value [always];
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
add_header X-Accel $server_name;

 添加自定义响应尾部

add_trailer name value [always];

 案例

  location /web1{proxy_pass http://192.168.33.149:80/;index index.html;proxy_cache proxycache;proxy_cache_key $request_uri;proxy_cache_valid 200 302 301 1h;proxy_cache_valid any 3m;add_header X-Via   $server_addr;add_header X-Cache $upstream_cache_status;  MISS没有命中  HIT命中缓存add_header X-Accel $server_name;}

 


http://www.ppmy.cn/server/34639.html

相关文章

dp 动态规划 力扣

64. 最小路径和 给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 说明:每次只能向下或者向右移动一步。 示例 1: 输入:grid [[1,3,1],[1,5,1],[4,2,1]] 输…

@click=“handleClick()“不会传递默认事件参数

当你使用click"handleClick()"这种形式绑定事件处理器时,Vue会将它视为一个函数调用,而不是一个事件监听器。在这种情况下,Vue不会自动传递原生事件对象作为默认参数。 如果你想让Vue自动传递原生事件对象作为默认参数&#xff0c…

轻松获取商机!淘宝商品关键词搜索电商API接口揭秘

利用科技手段来获得商机已经成为现代商业发展的重要途径之一。在电商领域,淘宝作为中国最大的网购平台之一,通过淘宝商品关键词搜索电商API接口,可以轻松获取商机,开拓市场。联讯数据将揭秘淘宝商品关键词搜索电商API接口&#xf…

美国加州65认证什么产品需要做

美国加州65认证是一种针对可能含有害化学物质的产品进行的测试,这些化学物质可能对环境和人体健康造成影响。加州65认证的实施基于《1986年安全饮用水和有毒物质执行法案》(Proposition 65),该法案要求企业在向加州销售或分销产品…

(优作)基于STM32 人群定位、调速智能风扇设计(程序、设计报告、视频演示)

引言 当今生活中,风扇已成为人们解暑的重要工具,然而使用风扇缓解夏日酷热的同时也存在着一些问题。比如,由于风扇的转动方向只能机械式的保持在一定范围内,而不能根据人群的位置做出具体的调整,即在一片区域内&#x…

银行操作风险名词解释及示例

名词解释及示例 一、操作风险事件 操作风险事件是指由操作风险引发,导致银行保险机构发生实际或者预计损失的事件。银行保险机构分别依据商业银行资本监管规则和保险公司偿付能力监管规则进行损失事件分类。 二、法律风险 法律风险包括但不限于下列风险&#xff…

input,el-input输入框正则验证输入的非数字转为空

<input οninput"this.valuethis.value.replace(/\D/g,)" maxlength"4" v-model"code" placeholder"请输入验证码" /> <el-input v-model"unboundTel" placeholder"请输入解绑手机号" clearable blur&q…

指数分布、瑞利分布和Nakagami-m的联系

指数分布: Y = X 1 2 + X 2 2 Y=X_1^{2}+X_2^2 Y=X12​+X22​,即(n=2)的卡方分布 Y ∼ exp ⁡ ( 2 σ 2 ) Y\sim \exp(2\sigma^2) Y∼exp(2σ2) PDF: f Y ( y ) = 1 2 σ 2 e − y 2 σ 2 , y > 0 f_{Y}(y)=\frac{1}{2\sigma^2}e^{-\frac{y}{2\sigma^2}},y>0 fY​(y…