Home > Backend Development > PHP Tutorial > nginx reverse proxy, separation of dynamic and static requests, and nginx cache application, and use ngx_cache_purge to clear the specified URL

nginx reverse proxy, separation of dynamic and static requests, and nginx cache application, and use ngx_cache_purge to clear the specified URL

WBOY
Release: 2016-08-08 09:31:18
Original
1379 people have browsed it

一,nginx反向代理配置

 

     #tomcat

Java代码  

  1.    upstream tomcat_server{  
  2. server 127.0.0.1:8080;    
  3.    }  
  4.   
  5.   
  6.   
  7. erver{  
  8. listen 80;  
  9. server_name www.codes51.com;  
  10.   
  11. location / {  
  12.   
  13.       proxy_redirect off;  
  14.       proxy_set_header Host $host;  
  15.       proxy_set_header X-Real-IP $remote_addr;  
  16.       proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;  
  17.       proxy_pass http://tomcat_server;  
  18.   
  19. }  

 

显然就是用户访问www.codes51.com(需要设置本地localhost,将www.codes51.com指向nginx所在IP)的时候(或将www.codes51.com直接写在nginx所在的IP地址),将请求转到到后台的tomcat服务器,即127.0.0.1:8080,并将请求到的数据转发给client

 

 

二,动静态请求相分离

 

   神马意思?图片,JS,HTML等静态的东西去访问一台专门的服务器,而动态的请求去访问另一台服务器。就这么简单,上例子:

 

 

 

Java代码  

  1. server {  
  2.        listen       192.168.154.128:80;  
  3.        server_name  image.codes51.com;  
  4.  index index.html;  
  5.    
  6.    
  7.  #proxy_pass http://tomcat_server;     
  8.        
  9.        #charset koi8-r;  
  10.   
  11.        #access_log  logs/host.access.log  main;  
  12.    
  13.           
  14.   
  15.        location / {  
  16.            root   html;  
  17.            #index  index.html index.htm;  
  18.              proxy_redirect off;  
  19.       proxy_set_header Host $host;  
  20.       proxy_set_header X-Real-IP $remote_addr;  
  21.       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  22.   
  23.   
  24.         }  
  25.   
  26.   
  27.         
  28. location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$  
  29. {  
  30.       
  31.     valid_referers none blocked 192.168.154.128 192.168.154.1;  
  32.     if ($invalid_referer)  
  33.     {  
  34.         rewrite ^  /403.jpg break;  
  35.     }  
  36.                  
  37.               if (!-f $request_filename) {  
  38.                  rewrite ^ /404.jpg last;  
  39.                }   
  40.                 
  41.                expires 30d;  
  42.   
  43. }  
  44.        #error_page  404              /404.html;  
  45.   
  46.        # redirect server error pages to the static page /50x.html  
  47.        #  
  48.        error_page   500 502 503 504  /50x.html;  
  49.        location = /404.jpg {  
  50.            root   html;  
  51.        }  
  52.   
  53.     }  
  54.   
  55. tomcat  
  56.    upstream tomcat_server{  
  57. server 127.0.0.1:8080;    
  58.    }  
  59.      
  60.      
  61.    server{  
  62. listen 192.168.154.128;  
  63. server_name www.codes51.com;  
  64.   
  65. location / {  
  66.          
  67.   
  68.       proxy_redirect off;  
  69.       proxy_set_header Host $host;  
  70.       proxy_set_header X-Real-IP $remote_addr;  
  71.       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  72.       proxy_pass http://tomcat_server;  
  73.   
  74. }  
  75.   
  76.   
  77.   
  78.    }  
 

 

 

上面这种方式是通过设定不同的域名,可不可以在同一个域名中,通过判断后缀来将动态与静态请求相分离呢?

 

 

Java代码  

  1. #tomcat  
  2.    upstream tomcat_server{  
  3. server 127.0.0.1:8080;    
  4.    }  
  5.      
  6.      
  7.    server{  
  8. listen 192.168.154.128;  
  9. server_name www.codes51.com;  
  10.   
  11. location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$  
  12. {  
  13.          
  14.       root html;  
  15.   
  16. }  
  17.   
  18.   
  19. location ~ .*.(jsp|do)$ {  
  20.          
  21.       proxy_redirect off;  
  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.       proxy_pass http://tomcat_server;  
  26.   
  27. }  

 

 

OK!同一个域名,根据后缀不同,请求不同的服务,实现动态静态请求相分离。仔细想一想,如果又出现了一种静态的文件,比如*.abc ,那难道又去修改那个配制文件吗?显然不太合理,所以可以考虑将所有的表态文件放在同一个根目录下面,比如/static那么可以将上面的静态页面请求修改一下:

 

 

Java代码  

  1. location  /static  
  2.     {  
  3.              
  4.           root html/static;  
  5.   
  6.     }  

 

 

咦这样是不是就好一些了,而且文件的存放也比较有规范了。

 

 

三,nginx缓存应用

nginx具有web缓存服务,proxy_cache,但是有一个问题就是,proxy_cache不能清除指定的URL缓存,只能设置URL过期时间,但是有问题,有人就会很快解决问题,nginx第三方模块ngx_cache_purge能清除指定URL。

 

  nginx安装时需要将ngx_cache_purege加载进去。

 

Java代码  

  1. ./configure --user=www --group=www --add-module=/root/dxm/nginx/ngx_cache_purge-1.2   

 

其中,/root/dxm/nginx/ngx_cache_purge-1.2为ngx_cache_purge解压路径(附件中提供ngx_cache_purge tar包下载)

 

现在来一段实例,实现图片缓存:

By the way, proxy_tem_path and proxy_cache_path must be under the same partition!

Java code

  1. proxy_temp_path /usr/local/nginx/proxy_temp;
  2. proxy_cache_path /usr/local/nginx/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=1g;


Java code

  1. upstream tomcat_server{  
  2.     server 127.0.0.1:8080;    
  3.     }  
  4.       
  5.       
  6.     server{  
  7.     listen 192.168.154.128;  
  8.     server_name www.codes51.com;  
  9.       
  10.     location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$  
  11.     {  
  12.              
  13.            proxy_cache cache_one;  
  14.            proxy_cache_methods GET HEAD POST;  
  15.                proxy_cache_min_uses 1;  
  16.               proxy_cache_valid 200 302 10m;  
  17.               proxy_cache_valid 404 1m;  
  18.            proxy_cache_valid any 1m;  
  19.               proxy_cache_key "$host:$server_port$uri$is_args$args";  
  20.   
  21.           proxy_redirect off;  
  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.           proxy_pass http://tomcat_server;  
  26.            
  27.   
  28.     }  
  29.       
  30.   
  31.     location ~ .*.(jsp)$ {  
  32.              
  33.           proxy_redirect off;  
  34.           proxy_set_header Host $host;  
  35.           proxy_set_header X-Real-IP $remote_addr;  
  36.           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  37.           proxy_pass http://tomcat_server;  
  38.   
  39.     }
  40. location ~ /purge(/.*)
  41.                                     
  42. Allow 192.168.154.128;
  43. Allow 192.168.154.1;
  44.                                                                                                                                                                         deny   all;
  45.              proxy_cache_purge cache_one $host:$server_port$
  46. 1$is_args$args; 
  47.           } 

Well, static pages are cached, but dynamic requests are not cached!

Take a look at the purege configuration in the last paragraph. Obviously, it indicates which IPs can manually clear the specified URL.



The above introduces nginx reverse proxy, dynamic and static request separation, and nginx cache application, as well as using ngx_cache_purge to clear the specified URL, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template