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

Aug 08, 2016 am 09:31 AM
header nbsp proxy server tomcat

一,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.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to deploy jar project in tomcat How to deploy jar project in tomcat Apr 21, 2024 am 07:27 AM

To deploy a JAR project to Tomcat, follow these steps: Download and unzip Tomcat. Configure the server.xml file, set the port and project deployment path. Copies the JAR file to the specified deployment path. Start Tomcat. Access the deployed project using the provided URL.

How to allow external network access to tomcat server How to allow external network access to tomcat server Apr 21, 2024 am 07:22 AM

To allow the Tomcat server to access the external network, you need to: modify the Tomcat configuration file to allow external connections. Add a firewall rule to allow access to the Tomcat server port. Create a DNS record pointing the domain name to the Tomcat server public IP. Optional: Use a reverse proxy to improve security and performance. Optional: Set up HTTPS for increased security.

How to deploy multiple projects in tomcat How to deploy multiple projects in tomcat Apr 21, 2024 am 09:33 AM

To deploy multiple projects through Tomcat, you need to create a webapp directory for each project and then: Automatic deployment: Place the webapp directory in Tomcat's webapps directory. Manual deployment: Manually deploy the project in Tomcat's manager application. Once the project is deployed, it can be accessed by its deployment name, for example: http://localhost:8080/project1.

Where is the tomcat installation directory? Where is the tomcat installation directory? Apr 21, 2024 am 07:48 AM

Tomcat installation directory: Default path: Windows: C:\Program Files\Apache Software Foundation\Tomcat 9.0macOS:/Library/Tomcat/Tomcat 9.0Linux:/opt/tomcat/tomcat9 Custom path: You can specify it during installation. Find the installation directory: use whereis or locate command.

Where is the root directory of the tomcat website? Where is the root directory of the tomcat website? Apr 21, 2024 am 09:27 AM

The Tomcat website root directory is located in Tomcat's webapps subdirectory and is used to store web application files, static resources, and the WEB-INF directory; it can be found by looking for the docBase attribute in the Tomcat configuration file.

How to check the number of concurrent connections in tomcat How to check the number of concurrent connections in tomcat Apr 21, 2024 am 08:12 AM

How to check the number of concurrent Tomcat connections: Visit the Tomcat Manager page (http://localhost:8080/manager/html) and enter your user name and password. Click Status->Sessions in the left navigation bar to see the number of concurrent connections at the top of the page.

How to check the port number of tomcat How to check the port number of tomcat Apr 21, 2024 am 08:00 AM

The Tomcat port number can be viewed by checking the port attribute of the <Connector> element in the server.xml file. Visit the Tomcat management interface (http://localhost:8080/manager/html) and view the "Status" tab. Run "catalina.sh version" from the command line and look at the "Port:" line.

How to run two projects with different port numbers in tomcat How to run two projects with different port numbers in tomcat Apr 21, 2024 am 09:00 AM

Running projects with different port numbers on the Tomcat server requires the following steps: Modify the server.xml file and add a Connector element to define the port number. Add a Context element to define the application associated with the port number. Create a WAR file and deploy it to the corresponding directory (webapps or webapps/ROOT). Restart Tomcat to apply changes.

See all articles