阿里云代理商-阿里云服务器-阿里云数据库-重庆典名科技

Nginx正反代理

发布时间: 2020-07-17 09:43:57文章作者: 网站编辑阅读量: 346
    一Nginx代理
    1.1Nginx代理概述
    nginx是一款自由的、开源的、高性能的HTTP服务器和反向代理服务器。同时也是一个IMAP、POP3、SMTP代理服务器。nginx可以作为一个HTTP服务器进行网站的发布处理,同时nginx可以作为反向代理进行负载均衡的实现。
    1.2Nginx代理模式
    Nginx通常有如下三种代理模式:
    正向代理(forwardproxy)
    反向代理(reverseproxy)
    透明代理
    1.3正向代理

    正向代理(forward)是一个位于客户端和原始服务器(originserver)之间的服务器,即代理服务器。为了从原始服务器取得内容,客户端向代理服务器发送一个请求并指定目标原始服务器,然后代理服务器向原始服务器转交请求并将获得的内容返回给客户端。

正向代理(forward)是

    场景一:客户端访问本来无法访问的原始服务器的资源

服务器的资源

    假设最初客户端要访问原始服务器需要经过R1和R2路由器这样一个路由节点,如果路由器R1或者路由器R2发生故障(或存在禁止访问的安全策略等),那么就无法访问原始服务器了。
    但是如果客户端让代理服务器去代替自己访问原始服务器,由于代理服务器没有在路由器R1或R2节点中,而是通过其它的路由节点访问的原始服务器,那么客户端就可以得到原始服务器的数据了。
    场景二:加速访问原始服务器的资源

加速访问原始服务器的资源

    假设客户端要访问原始服务器,经过R1路由器和R2路由器,而R1到R2路由器的链路是一个低带宽链路。而客户端到代理服务器,从代理服务器到原始服务器都是高带宽链路。那么使用正向代理则可以加速访问。
    场景三:Cache作用

Cache作用

    Cache(缓存)技术和代理服务技术是紧密联系的(不光是正向代理,反向代理也使用了Cache(缓存)技术)。
    如上图所示,假设客户端要访问原始服务器某数据A之前,代理服务器已经通过访问原始服务器数据A,那么代理服务器会把数据A保存一段时间,此时再次通过代理服务器访问数据A,那么代理服务器不再访问原始服务器,而把缓存的数据A直接发给客户端。这一技术在Cache中术语就叫Cache命中。
    场景四:代理服务器实现授权控制

代理服务器实现授权控制

    如上图所示,防火墙作为网关,用来过滤外网对其的访问。假设局域网内部客户端A和客户端B都设置了代理服务器。可通过代理服务器配置客户端A允许访问互联网,而客户端B不允许访问互联网,从而实现不通客户端的访问代理授权控制。
    场景五:隐藏客户端踪迹

隐藏客户端踪迹

    1.4反向代理
    反向代理正好与正向代理相反,对于客户端而言代理服务器就像是原始服务器,并且客户端不需要进行任何特别的设置。客户端向反向代理的命名空间(name-space)中的内容发送普通请求,接着反向代理将判断向何处(原始服务器)转交请求,并将获得的内容返回给客户端。
    场景一:隐藏原始服务器踪迹
隐藏原始服务器踪迹
    客户端始终认为它访问的是原始服务器而不是代理服务器,但实用际上反向代理服务器接受客户端的应答,从原始资源服务器中取得客户端的需求资源,然后发送给客户端。由于防火墙的作用,只允许代理服务器访问原始资源服务器。对于此环境下,防火墙和反向代理的共同作用保护了原始资源服务器,对于客户端而言是透明的。
    场景二:负载均衡器
负载均衡器
    当反向代理服务器存在多个,从而部署为集群,当多个客户端访问原始服务器(原始服务器也可以是集群)的时候,不同的代理服务器应答不同的客户端,然后发送不同的客户端所需的资源,从而实现负载均衡效果。
    提示:Nginx基于反向代理实现负载均衡配置参考《012.Nginx负载均衡》。
    场景三:Cache作用
    同时反向代理服务器类似正向代理服务器一样拥有Cache的作用,可以缓存原始资源服务器的资源,而不是每次都要向原始资源服务器组请求数据,特别对于一些静态的数据,比如图片和普通文件,如果这些反向代理服务器能够做到和客户端来自同一个网络,那么客户端访问反向代理服务器,就会得到很高质量的速度。这正是CDN技术的核心。场景四:正方代理混合场景
正方代理混合场景
    实际项目操作时,正向代理和反向代理很有可能会存在在一个应用场景中,正向代理代理客户端的请求去访问目标服务器,目标服务器是一个反向代理服务器,反向代理了多台真实的业务处理服务器。
    1.5透明代理
    透明代理的意思是客户端根本不需要知道有代理服务器的存在,它改编你的requestfields(报文),并会传送真实IP。注意,加密的透明代理则是属于匿名代理,意思是不用设置使用代理了。
    1.6常见代理软件
    通常大多数开源代理软件,都能实现正反代理两种方式。开源软件中如squid,既可以做正向代理,也可以实现反向代理。MSISA也可以用来在Windows平台下做正向代理。反向代理中最主要的实践就是WEB服务,如Nginx。
    二代理配置项
    2.1配置语法
    语法:proxy_bufferingon|off;
    默认值:proxy_bufferingon;
    可配置段:http,server,location
    作用:配置proxy缓冲区。
    扩展:
    proxy_buffer_size:设置缓冲区大小(内存页大小)
    proxy_buffers:设置缓冲区数量和大小(内存页数量和大小)
    proxy_busy_buffers_size:设置最大缓冲区大小
    语法:proxy_redirectdefault;proxy_redirectoff;proxy_redirectredirectreplacement;
    默认值:proxy_redirectdefault;
    可配置段:http,server,location
    作用:配置proxy重定向。
    扩展:
    语法:proxy_bufferingon|off;
    默认值:proxy_bufferingon;
    可配置段:http,server,location
    作用:配置proxy缓冲区。
    扩展:
    语法:proxy_set_headerfieldvalue;
    默认值:proxy_set_headerHost$proxy_host;proxy_set_headerConnectionclose;
    可配置段:http,server,location
    作用:配置proxy头信息。
    扩展:
    proxy_hide_header:设置隐藏头信息字段;
    proxy_set_body:设置请求体返回信息。
    语法:proxy_connect_timeouttime;
    默认值:proxy_connect_timeout60s;
    可配置段:http,server,location
    作用:配置proxy超时。
    扩展:
    proxy_hide_header:设置隐藏头信息字段;
    proxy_set_body:设置请求体返回信息。
    三配置正向代理
    3.1正向代理配置
 1 [root@proxy ~]# vi /etc/nginx/conf.d/reverse.conf
  2 server{
  3     resolver 8.8.8.8;				#配置DNS解析IP地址
  4     resolver_timeout 30s;				#超时时间(5秒)
  5     listen 8080;
  6     access_log  /var/log/nginx/reverse.access.log  main;
  7     error_log   /var/log/nginx/reverse.error.log  warn;
  8     location / {
  9         proxy_pass http://$http_host$request_uri;	#配置正向代理参数
 10         proxy_set_header Host $http_host;		#解决如果URL中带"."后Nginx 503错误
 11         proxy_buffers 256 4k; 			#配置缓存大小
 12         proxy_max_temp_file_size 0;			#关闭磁盘缓存读写减少I/O
 13         proxy_connect_timeout 30;			#代理连接超时时间
 14         proxy_cache_valid 200 302 10m;
 15         proxy_cache_valid 301 1h;
 16         proxy_cache_valid any 1m;			#配置代理服务器缓存时间
 17     }
 18 }
复制代码
  1 [root@proxy ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件
  2 [root@proxy ~]# nginx -s reload				#重载配置文件
    配置释义:
    不能有hostname。
    必须有resolver,即dns,超时时间(30秒)可选。
    配置正向代理参数,均是由Nginx变量组成。
    提示:配置好后,重启nginx,以浏览器为例,若需要使用这个代理服务器,则只需将浏览器代理设置为http://+服务器ip地址+:+80即可使用了。
以浏览器为例
    四反向代理配置
    4.1环境预设






主机 作用 备注



www.321.net 代理服务器 反向代理服务器

www.321.net 原始服务器 模拟原始服务器


    4.2配置反向代理
1 [root@proxy ~]# vi /etc/nginx/conf.d/forward.conf
  2 server {
  3     listen  80;
  4     server_name  forward.linuxds.com;
  5     access_log  /var/log/nginx/forward.access.log  main;
  6     error_log   /var/log/nginx/forward.error.log  warn;
  7     location / {
  8         proxy_pass https://www.landiannews.com/;
  9         index index.html;
 10         proxy_redirect     off;
 11 #        proxy_set_header   Host             $host;
 12         proxy_set_header   X-Real-IP        $remote_addr;
 13         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
 14         client_max_body_size       10m;		#允许客户端请求的最大单文件字节数
 15         client_body_buffer_size    128k;	        #缓冲区代理缓冲用户端请求的最大字节数
 16         proxy_connect_timeout      300;		#nginx跟后端服务器连接超时时间(代理连接超时)
 17         proxy_send_timeout         300;		#后端服务器数据回传时间(代理发送超时)
 18         proxy_read_timeout         300;		#连接成功后,后端服务器响应时间(代理接收超时)
 19         proxy_buffer_size          4k;		#设置代理服务器(nginx)保存用户头信息的缓冲区大小
 20         proxy_buffers              4 32k;	        #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
 21         proxy_busy_buffers_size    64k;		#高负荷下缓冲大小(proxy_buffers*2)
 22         proxy_temp_file_write_size 64k;		#设定缓存文件夹大小,大于这个值,将从upstream服务器传
 23     }
 24 }
  1 [root@proxy ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件
  2 [root@proxy ~]# nginx -s reload				#重载配置文件
    配置释义:
    proxy_set_headerX-Real-IP$remote_addr:把源IP【$remote_addr,建立HTTP连接header里面的信息】赋值给X-Real-IP,从而通过$X-Real-IP来获取源IP;
    proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for:在nginx作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来,用【,】隔开。
    4.3测试反向代理
    浏览器访问:https://www.runoob.com/w3cnote/nginx-proxy-balancing.html

    4.4其他代理配置语句
    proxy_pass:设置代理服务器的地址,可以是主机名称、IP地址加端口号等形式。
    proxy_passURL
    提示:
    1:当代理的是一组服务器时可以使用upstream指令来设置。
    2:当URL中含有uri时,(例如"http://127.0.0.1:8080/"、"http://127.0.0.1:8080/demo.html")不管客户端访问的是地址中的uri是什么,代理服务器都会代理到URL的地址;当URL中不包含uri时(例如:"http://127.0.0.1:8080"),那么当客户端访问服务器时,代理服务器会根据客户端请求的uri来访问具体的URL地址。
    proxy_pass_request_bodyon|off:用于配置是否将客户端请求的请求体发送给代理服务器。
    proxy_pass_request_headerson|off:用于配置是否将客户端请求的头信息发送给代理服务器。
    proxy_set_headerfieldvalue:可以更改nginx接收到的客户端请求的请求头信息,然后将新的请求头信息发送给被代理的服务器。
    proxy_set_bodyvalue:ngin接收到客户端的请求后使用该指令可以修改request中的body体,然后将请求转发给代理服务器。
    proxy_connect_timeouttime:nginx服务器与被代理服务器之间尝试建立连接的的超时时间,默认为60s。
    proxy_read_timeottime:nginx服务器接收被代理服务器数据时最大的等待时间,默认为60s。
    proxy_send_timeouttime:nginx服务器发送数据至被代理服务器的最大等待时间,例如60s内没有发出一个字节则默认断开连接,默认60s。
    proxy_http_version1.0|1.1:nginx服务器提供代理服务的http协议版本。
    proxy_methodmethod:nginx服务器设置请求被代理服务器时使用的请求方法,一般为POST或者GET。
    proxy_ignore_client_abort:当客户端中断网络请求时,nginx服务是否中断对代理服务器的请求,默认off。
    五四层代理配置
    5.1四层代理
    nginx-1.9.0开始支持TCP代理,即4层代理,编译安装默认不会支持,需要加上–with-stream参数编译。
    5.2环境预设






IP:端口 后端RS 备注

172.24.10.21:8888(nginx01) 172.24.10.22:81(nginx02) 反向代理81端口Web

172.24.10.23:81(nginx03)

172.24.10.21:2222(nginx01) 172.24.10.24:22(nginx04) 反向代理ssh






  1 [root@nginx02 ~]# mkdir /usr/share/nginx/rs/
  2 [root@nginx02 ~]# echo '

Rs172.24.10.22-81

' > /usr/share/nginx/rs/index.html 3 [root@nginx02 ~]# cat > /etc/nginx/conf.d/rs.conf < 4 server { 5 listen 81; 6 server_name 172.24.10.22; 7 location / { 8 root /usr/share/nginx/rs; 9 index index.html; 10 access_log /var/log/nginx/rs.access.log main; 11 error_log /var/log/nginx/rs.error.log warn; 12 } 13 } 14 EOF
 1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件
  2 [root@nginx02 ~]# nginx -s reload			#重载配置文件
 1 [root@nginx03 ~]# mkdir /usr/share/nginx/rs/
  2 [root@nginx03 ~]# echo '

Rs172.24.10.23-81

' > /usr/share/nginx/rs/index.html 3 [root@nginx03 ~]# cat > /etc/nginx/conf.d/rs.conf < 4 server { 5 listen 81; 6 server_name 172.24.10.23; 7 location / { 8 root /usr/share/nginx/rs; 9 index index.html; 10 access_log /var/log/nginx/rs.access.log main; 11 error_log /var/log/nginx/rs.error.log warn; 12 } 13 } 14 EOF
  1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件
  2 [root@nginx02 ~]# nginx -s reload			#重载配置文件
    5.3配置四层代理
1 [root@nginx01 ~]# cat >> /etc/nginx/nginx.conf << eof="" span="">
  2 stream {
  3     upstream myweb {
  4         server 172.24.10.22:81 weight=5 max_fails=1 fail_timeout=10s;
  5         server 172.24.10.23:81;
  6     }
  7     upstream myssh {
  8         hash $remote_addr consistent;
  9         server 172.24.10.24:22;
 10     }
 11 
 12     server {
 13         listen  8888;
 14         proxy_connect_timeout 2s;
 15         proxy_timeout 900s;
 16         proxy_pass myweb;
 17     }
 18 
 19     server {
 20         listen  2222;
 21         proxy_connect_timeout 2s;
 22         proxy_timeout 900s;
 23         proxy_pass myssh;
 24     }
 25 }
 26 EOF
    提示:stream和http同等级,因此如上配置需要追加至主配置文件最后。
  1 [root@proxy ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件
  2 [root@proxy ~]# nginx -s reload				#重载配置文件
    5.4确认验证
    浏览器访问:http://113.31.111.246:8888/。
浏览器访问
    [root@proxy~]#ssh-p2222172.24.9.247   #测试ssh
#测试ssh
    六按类型反向代理配置
    6.1环境预设







IP 备注

主机

nginx01 172.24.10.21 反向代理服务器

nginx02 172.24.10.22 chrmoe类型资源

firefox类型资源

nginx03 172.24.10.23 iPhone类型资源

android类型资源

nginx04 172.24.10.24 IE类型资源






  1 [root@nginx02 ~]# mkdir /usr/share/nginx/basebrowser/
  2 [root@nginx02 ~]# echo '

Chrmoe-172.24.10.22

' > /usr/share/nginx/basebrowser/index01.html 3 [root@nginx02 ~]# echo '

Firefox-172.24.10.22

' > /usr/share/nginx/basebrowser/index02.html
1 [root@nginx02 ~]# cat > /etc/nginx/conf.d/basebrowser.conf <
  2 server {
  3     listen  88;
  4     server_name 172.24.10.22;
  5     location / {
  6         root   /usr/share/nginx/basebrowser;
  7         index  index01.html;
  8         access_log  /var/log/nginx/chrmoebrowser.access.log  main;
  9         error_log   /var/log/nginx/chrmoebrowser.error.log  warn;
 10     }
 11 }
 12 server {
 13     listen  89;
 14     server_name 172.24.10.22;
 15     location / {
 16         root   /usr/share/nginx/basebrowser;
 17         index  index02.html;
 18         access_log  /var/log/nginx/firefoxbrowser.access.log  main;
 19         error_log   /var/log/nginx/firefoxbrowser.error.log  warn;
 20     }
 21 }
 22 EOF
 1 [root@nginx03 ~]# mkdir /usr/share/nginx/cellphone/
  2 [root@nginx03 ~]# echo '

iPhone-172.24.10.23

' > /usr/share/nginx/cellphone/index01.html 3 [root@nginx03 ~]# echo '

Android-172.24.10.23

' > /usr/share/nginx/cellphone/index02.html
 1 [root@nginx03 ~]# cat > /etc/nginx/conf.d/cellphone.conf <
  2 server {
  3     listen  88;
  4     server_name 172.24.10.23;
  5     location / {
  6         root   /usr/share/nginx/cellphone;
  7         index  index01.html;
  8         access_log  /var/log/nginx/iphone.access.log  main;
  9         error_log   /var/log/nginx/iphone.error.log  warn;
 10     }
 11 }
 12 server {
 13     listen  89;
 14     server_name 172.24.10.23;
 15     location / {
 16         root   /usr/share/nginx/cellphone;
 17         index  index02.html;
 18         access_log  /var/log/nginx/android.access.log  main;
 19         error_log   /var/log/nginx/android.error.log  warn;
 20     }
 21 }
 22 EOF
  1 [root@nginx04 ~]# mkdir /usr/share/nginx/iebrowser/
  2 [root@nginx04 ~]# echo '

IE Browser-172.24.10.24

' > /usr/share/nginx/iebrowser/index.html
 1 [root@nginx04 ~]# cat > /etc/nginx/conf.d/iebrowser.conf <
  2 server {
  3     listen  88;
  4     server_name 172.24.10.24;
  5     location / {
  6         root   /usr/share/nginx/iebrowser;
  7         index  index.html;
  8         access_log  /var/log/nginx/iebrowser.access.log  main;
  9         error_log   /var/log/nginx/iebrowser.error.log  warn;
 10     }
 11 }
 12 EOF
 1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件
  2 [root@nginx02 ~]# nginx -s reload			#重载配置文件
  3 [root@nginx03 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件
  4 [root@nginx03 ~]# nginx -s reload			#重载配置文件
  5 [root@nginx04 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件
  6 [root@nginx04 ~]# nginx -s reload			#重载配置文件
1 [root@client ~]# curl 172.24.10.22:89
  2 <h1>Firefox-172.24.10.22</h1>
  3 [root@client ~]# 
  4 [root@client ~]# curl 172.24.10.22:88
  5 <h1>Chrmoe-172.24.10.22</h1>
  6 [root@client ~]# curl 172.24.10.22:89
  7 <h1>Firefox-172.24.10.22</h1>
  8 [root@client ~]# curl 172.24.10.23:88
  9 <h1>iPhone-172.24.10.23</h1>
 10 [root@client ~]# curl 172.24.10.23:89
 11 <h1>Android-172.24.10.23</h1>
 12 [root@client ~]# curl 172.24.10.24:88
 13 <h1>IE Browser-172.24.10.24</h1>
    6.2反向代理配置
  1 [root@nginx01 ~]# mkdir /usr/share/nginx/type/
  2 [root@nginx01 ~]# echo '<h1>Type-172.24.10.21</h1>' > /usr/share/nginx/type/index.html
1 [root@nginx01 ~]# vi /etc/nginx/conf.d/type.conf
  2 upstream chrome {
  3     server 172.24.10.22:88;
  4 }
  5 upstream firefox {
  6     server 172.24.10.22:89;
  7 }
  8 upstream iphone {
  9     server 172.24.10.23:88;
 10 }
 11 upstream android {
 12     server 172.24.10.23:89;
 13 }
 14 upstream iebrowser {
 15     server 172.24.10.24:88;
 16 }
 17 
 18 server {
 19     listen  80;
 20     server_name  type.linuxds.com;
 21     access_log  /var/log/nginx/type.access.log  main;
 22     error_log   /var/log/nginx/type.error.log  warn;
 23     proxy_set_header   Host             $host;
 24     proxy_set_header   X-Real-IP        $remote_addr;
 25     proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
 26     client_max_body_size       10m;	#允许客户端请求的最大单文件字节数
 27     client_body_buffer_size    128k;	#缓冲区代理缓冲用户端请求的最大字节数
 28     proxy_connect_timeout      300;	#nginx跟后端服务器连接超时时间(代理连接超时)
 29     proxy_send_timeout         300;	#后端服务器数据回传时间(代理发送超时)
 30     proxy_read_timeout         300;	#连接成功后,后端服务器响应时间(代理接收超时)
 31     proxy_buffer_size          4k;		#设置代理服务器(nginx)保存用户头信息的缓冲区大小
 32     proxy_buffers              4 32k;	#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
 33     proxy_busy_buffers_size    64k;	#高负荷下缓冲大小(proxy_buffers*2)
 34     proxy_temp_file_write_size 64k;	#设定缓存文件夹大小,大于这个值,将从upstream服务器传
 35     location / {
 36         root  /usr/share/nginx/type/;
 37         index index.html;
 38 
 39         if ($http_user_agent ~* "chrome"){
 40                 proxy_pass http://chrome;
 41                 }
 42         if ($http_user_agent ~* "firefox"){
 43                 proxy_pass http://firefox;
 44                 }
 45         if ($http_user_agent ~* "android"){
 46                 proxy_pass http://android;
 47                 }
 48         if ($http_user_agent ~* "iphone"){
 49                 proxy_pass http://iphone;
 50                 }
 51         if ($http_user_agent ~* "MSIE"){
 52                 proxy_pass http://iebrowser;
 53                 }
 54     }
 55 }
  1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件
  2 [root@nginx01 ~]# nginx -s reload			#重载配置文件
    6.3确认验证
    不同浏览器分别访问:http://type.linuxds.com/
不同浏览器分别访问
    七反向代理及缓存
    7.1环境预设


IP 备注

主机

nginx01 172.24.10.21 反向代理及缓存服务器

nginx02 172.24.10.22 后端RS01

nginx03 172.24.10.23 后端RS01

 1 [root@nginx02 ~]# mkdir /usr/share/nginx/cache/
  2 [root@nginx02 ~]# echo '<h1>Cache-172.24.10.22</h1>' > /usr/share/nginx/cache/index.html
  3 
1 [root@nginx02 ~]# cat > /etc/nginx/conf.d/cache.conf <<EOF
  2 server {
  3     listen  90;
  4     server_name 172.24.10.22;
  5     location / {
  6         root   /usr/share/nginx/cache;
  7         index  index.html;
  8         access_log  /var/log/nginx/cache.access.log  main;
  9         error_log   /var/log/nginx/cache.error.log  warn;
 10     }
 11 }
 12 EOF
1 [root@nginx02 ~]# ll /usr/share/nginx/cache
  2 total 16K
  3 -rw-r--r-- 1 root root  28 Jun 23 22:33 index.html
  4 -rw-r--r-- 1 root root 11K Jun 23 22:34 nginx.jpg	#上传一张测试图片
  5 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件
  6 [root@nginx02 ~]# nginx -s reload			#重载配置文件
1 [root@nginx03 ~]# mkdir /usr/share/nginx/cache/
  2 [root@nginx03 ~]# echo '<h1>Cache-172.24.10.23</h1>' > /usr/share/nginx/cache/index.html
 1 [root@nginx03 ~]# cat > /etc/nginx/conf.d/cache.conf <<EOF
  2 server {
  3     listen  90;
  4     server_name 172.24.10.23;
  5     location / {
  6         root   /usr/share/nginx/cache;
  7         index  index.html;
  8         access_log  /var/log/nginx/cache.access.log  main;
  9         error_log   /var/log/nginx/cache.error.log  warn;
 10     }
 11 }
 12 EOF
1 [root@nginx03 ~]# ll /usr/share/nginx/cache
  2 total 16K
  3 -rw-r--r-- 1 root root  28 Jun 23 22:33 index.html
  4 -rw-r--r-- 1 root root 11K Jun 23 22:34 nginx.jpg	#上传一张测试图片
  5 [root@nginx03 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件
  6 [root@nginx03 ~]# nginx -s reload			#重载配置文件
    7.2配置代理及缓存
1 [root@nginx01 ~]# mkdir -p /data/cache			#创建缓存目录
  2 [root@nginx01 ~]# mkdir -p /data/cache_temp		#创建缓存临时目录
1 [root@nginx01 ~]# vi /etc/nginx/nginx.conf		#追加如下代码日志记录
  2 ……
  3 log_format  main  '[$remote_addr]-[$remote_user]-[$time_local]-["$request"]'
  4                       '[$status]-[$body_bytes_sent]-["$http_referer"]'
  5                       '["$http_user_agent"]-["$http_x_forwarded_for"]';
  6                       '[$upstream_addr]-[$upstream_status]-[$request_time]-[$upstream_response_time]'
  7 ……
 1 [root@nginx01 ~]# vi /etc/nginx/conf.d/cache.conf
  2 upstream cache {
  3 server 172.24.10.22:90;
  4 server 172.24.10.23:90;
  5     }
  6 
  7 proxy_temp_path /data/cache_temp;
  8 proxy_cache_path /data/cache levels=1:2 keys_zone=mycache:20m max_size=10g inactive=60m use_temp_path=off;
  9 
 10 server {
 11     listen          80;
 12     server_name     cache.linuxds.com;
 13     access_log  /var/log/nginx/cache.access.log  main;
 14     error_log   /var/log/nginx/cache.error.log  warn;
 15 
 16     location / {
 17         expires 3d;
 18         add_header  Nginx-Cache "$upstream_cache_status";	#增加一个头信息
 19 
 20         proxy_cache mycache;			        #调用定义的cache zone
 21         proxy_pass  http://cache;		                #配置反向代理
 22         proxy_set_header  Host      $host;
 23         proxy_set_header  X-Real-IP $remote_addr;
 24         proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
 25 
 26         proxy_cache_valid  200 302 304 2h;	               #200和302及304头信息过期时间为2小时
 27         proxy_cache_valid  any 10m;		               #其他过期时间10分钟
 28         proxy_cache_key    $host$request_uri$uri$is_args$args;	#定义缓存的key
 29         proxy_cache_bypass $cookie_nocache $arg_comment;	        #配置不缓存
 30         proxy_no_cache $arg_nocache;				#配置不缓存
 31         proxy_cache_lock on;
 32         proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;	#一个服务报错请求下一个
 33     }
 34     location ~ /purge(/.*) {
 35         allow       127.0.0.1;
 36         allow       172.24.10.0/24;
 37         deny        all;
 38         proxy_cache_purge mycache $1$is_args$args;
 39     }
 40 }
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件
  2 [root@nginx01 ~]# nginx -s reload			#重载配置文件
    配置释义:
    proxy_cache_path:配置缓存目录,文件目录层级2级,空间名字20m大小,目录最大大小10g(超过启动nginx自己的淘汰规则),在60分钟的时间内没有被访问就会被清理,并且不使用临时目录。
    proxy_no_cache:部分不设置缓存,cookie_nocache上面配置的参数,cookie_nocache不为0或者空,那么是不会进行缓存的。
    proxy_cache_lockon:如果多个客户端请求的文件不在缓存(MISS),只有第一个这些请求是通过原始服务器的。
    提示:proxy_no_cache和proxy_cache_bypass类似,其差异是proxy_no_cache用于控制什么情况下响应不被缓存。比如配置“proxy_no_cache$args_nocache”,如果带的nocache参数值至少有一个不为空或者为0,则响应将不被缓存。而proxy_cache_bypass,控制什么情况不使用缓存的内容,而是直接到后端获取最新的内容。如果命中,则$upstream_cache_status为BYPASS。
    延伸:
    upstream_cache_status状态如下:
    MISS:未命中缓存,即在缓存中找不到响应,因此从原始服务器获取响应。然后缓存响应;
    HIT:命中缓存,响应将直接来自有效的缓存;
    EXPIRED:缓存已经过期,响应包含来自原始服务器的新内容;
    STALE:命中了陈旧的缓存,因为源服务器未正确响应但proxy_cache_use_stale已配置。
    UPDATING:内容陈旧,因为条目当前正在更新以响应先前的请求,并且proxy_cache_use_staleupdating已配置;
    REVALIDATED:Nginx验证了陈旧的内容依然有效;
    BYPASS:响应是从原始服务器获得。
    注意:使用proxy_cache_purge清除缓存,必须提前安装ngx_cache_purge模块,安装模块必须基于编译安装的Nginx。
    7.3确认验证
    客户端访问测试:
 1 [root@client ~]# curl -I http://cache.linuxds.com/nginx.jpg
客户端访问测试
    结论:第一次访问会出现MISS,没有命中缓存,第二次访问即可直接命中缓存。

联系客服免费领取更多阿里云产品新购、续费升级折扣,叠加官网活动折上折更优惠