nginx--反向代理

14 篇文章 0 订阅

反向代理

指的是代理外网用户的请求到内部的指定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 httpd
 echo "web2 192.168.33.149" > /var/www/html/index.html
 systemctl 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     945

Percentage 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;
    }
  

nginx高级反向代理

Nginx可以基ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能

Module ngx_http_upstream_module

参数配置

upstream name {
}自定义一组服务器,配置在http内

weight=number 设置权重,默认为1。
max_conns=number 给当前server设置最大活动链接数,默认为0表示没有限制。
fail_timeout=time 对后端服务器的失败监测超时间,默认为10秒。
max_fails=number 在fail_timeout 时间对后端服务连续监测失败多少次就标记为不可用。
proxy_next_upstream=error timeout; 指定在哪种检测状态下将请求转发器其他服务器。
backup 设置为备份服务器,当所有服务器不可用时将重新启用次服务器。
down 标记为down状态。
resolve 当server定义的是主机名的时候,当A记录发生变化会自动应用新IP而不用重启Nginx

hash KEY consistent;基于指定key做hash计算,使用consistent参数,将使用ketama一致性hash算法,适用于后端是Cache服务器器(如varnish)时使用,consistent定义使用一致性hash运算,一致性hash基于取模运算

hash $request_uri consistent; 基于用户请求的uri做hash

ip_hash;源地址hash调度方法,基于的客户端的remote_addr(源地址)做hash计算,以实现会话保持,

least_conn; 最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器

案例

多台web服务器

权重不一样
upstream webserver {
  server 192.168.33.171:80 weight=1 fail_timeout=15s max_fails=3;
  server 192.168.33.149:80 weight=2 fail_timeout=15s max_fails=3;
}
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 /web {
     index index.html;
     proxy_pass http://webserver/; 
   }
 }

 加入hash KEY consistent
upstream webserver {
  server 192.168.33.171:80 weight=1 fail_timeout=15s max_fails=3;
  server 192.168.33.149:80 weight=2 fail_timeout=15s max_fails=3;
}
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 /web {
     index index.html;
     proxy_pass http://webserver/; 
   }
 } 

加入 ip_hash
upstream webserver {
  #hash $request consistent;
  ip_hash;
  server 192.168.33.171:80 weight=1 fail_timeout=15s max_fails=3;
  server 192.168.33.149:80 weight=2 fail_timeout=15s max_fails=3;
}
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 /web {
     index index.html;
     proxy_pass http://webserver/; 
   }
 }

 

 加入least_conn
upstream webserver {
  least_conn;
  server 192.168.33.171:80 weight=1 fail_timeout=15s max_fails=3;
  server 192.168.33.149:80 weight=2 fail_timeout=15s max_fails=3;
}
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 /web {
     index index.html;
     proxy_pass http://webserver/; 
   }
 }

客户端IP透传

nginx

upstream webserver {
  server 192.168.33.171:80 weight=1 fail_timeout=15s max_fails=3;
  server 192.168.33.149:80 weight=2 fail_timeout=15s max_fails=3;
}
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 /web {
     index index.html;
     proxy_pass http://webserver/;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
   }
 }

apache

vim /etc/httpd/conf/httpd.conf 
 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{X-Forwarded-For}i\"" combined

结果

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值