: This article mainly 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. if you are interested in the PHP Tutorial, please refer to it. 1. nginx Reverse proxy configuration
# Tomcat
Java code
- Upstream tomcat_server {
- Server 127.0.0.1: 8080;
- }
-
-
-
- Erver {
- Listen 80;
- Server_name www.codes51.com;
-
- Location /{
-
- Proxy_redirect off;
- Proxy_set_header Host $ host;
- Proxy_set_header X-Real-IP $ remote_addr;
- Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
- Proxy_pass http: // tomcat_server;
-
- }
Obviously, when you access www.codes51.com (you need to set a local localhost to direct www.codes51.com to the IP address of nginx) (or write www.codes51.com directly to the IP address of nginx ), send the request to the backend tomcat server, that is, 127.0.0.1: 8080, and forward the requested data to the client.
2. Separation of dynamic and static requests
What does Shenma mean? Images, JS, HTML, and other static things access a dedicated server, while dynamic requests access another server. This is simple. the example above:
Java code
- Server {
- Listen 192.168.154.128: 80;
- Server_name image.codes51.com;
- Index index.html;
-
-
- # Proxy_pass http: // tomcat_server;
-
- # Charset KOI8-R;
-
- # Access_log logs/host. access. log main;
-
-
-
- Location /{
- Root html;
- # Index index.html index.htm;
- Proxy_redirect off;
- Proxy_set_header Host $ host;
- Proxy_set_header X-Real-IP $ remote_addr;
- Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
-
-
- }
-
-
-
- Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf) $
- {
-
- Valid_referers none blocked 192.168.154.128 192.168.154.1;
- If ($ invalid_referer)
- {
- Rewrite ^/403.jpg break;
- }
-
- If (! -F $ request_filename ){
- Rewrite ^/404.jpg last;
- }
-
- Expires 30d;
-
- }
- # Error_page 404/404 .html;
-
- # Redirect server error pages to the static page/50x.html
- #
- Error_page 500 502 503 x.html;
- Location =/404.jpg {
- Root html;
- }
-
- }
-
- Tomcat
- Upstream tomcat_server {
- Server 127.0.0.1: 8080;
- }
-
-
- Server {
- Listen 192.168.154.128;
- Server_name www.codes51.com;
-
- Location /{
-
-
- Proxy_redirect off;
- Proxy_set_header Host $ host;
- Proxy_set_header X-Real-IP $ remote_addr;
- Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
- Proxy_pass http: // tomcat_server;
-
- }
-
-
-
- }
The above method is to set different domain names. can dynamic and static requests be separated by determining the suffix in the same domain name?
Java code
- # Tomcat
- Upstream tomcat_server {
- Server 127.0.0.1: 8080;
- }
-
-
- Server {
- Listen 192.168.154.128;
- Server_name www.codes51.com;
-
- Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf) $
- {
-
- Root html;
-
- }
-
-
- Location ~ . * \. (Jsp | do) $ {
-
- Proxy_redirect off;
- Proxy_set_header Host $ host;
- Proxy_set_header X-Real-IP $ remote_addr;
- Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
- Proxy_pass http: // tomcat_server;
-
- }
OK! The same domain name requests different services based on different suffixes to achieve dynamic and static request separation. Think about it. if another static file, such as *. abc, is there another way to modify the configuration file? Obviously not quite reasonable, so you can consider placing all the statement files under the same root directory. for example, you can modify the above static page request:
Java code
- Location/static
- {
-
- Root html/static;
-
- }
Isn't it better, and the storage of files is also standardized.
III. nginx cache application
Nginx has a web cache service and proxy_cache, but one problem is that proxy_cache cannot clear the specified URL cache and can only set the URL expiration time. However, if there is a problem, someone will quickly solve the problem, nginx third-party module ngx_cache_purge can clear the specified URL.
During nginx installation, you must load ngx_cache_purege.
Java code
- ./Configure -- user = www -- group = www -- add-module =/root/dxm/nginx/ngx_cache_purge-1.2.
The/root/dxm/nginx/ngx_cache_purge-1.2 is the ngx_cache_purge decompression path (the ngx_cache_purge tar package download is provided in the attachment)
Now we can use an instance to cache images:
In other words, proxy_tem_path must be in the same partition as proxy_cache_path!
Java code
- Proxy_temp_path/usr/local/nginx/proxy_temp;
- Proxy_cache_path/usr/local/nginx/proxy_cache_path levels = keys_zone = cache_one: 200 m inactive = 1d max_size = 1g;
Java code
- Upstream tomcat_server {
- Server 127.0.0.1: 8080;
- }
-
-
- Server {
- Listen 192.168.154.128;
- Server_name www.codes51.com;
-
- Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf) $
- {
-
- Proxy_cache cache_one;
- Proxy_cache_methods get head post;
- Proxy_cache_min_uses 1;
- Proxy_cache_valid 200 302 10 m;
- Proxy_cache_valid 404 1 MB;
- Proxy_cache_valid any 1 m;
- Proxy_cache_key "$ host: $ server_port $ uri $ is_args $ args ";
-
- Proxy_redirect off;
- Proxy_set_header Host $ host;
- Proxy_set_header X-Real-IP $ remote_addr;
- Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
- Proxy_pass http: // tomcat_server;
-
-
- }
-
-
- Location ~ . * \. (Jsp) $ {
-
- Proxy_redirect off;
- Proxy_set_header Host $ host;
- Proxy_set_header X-Real-IP $ remote_addr;
- Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
- Proxy_pass http: // tomcat_server;
-
- }
-
-
- Location ~ /Purge (/.*)
- {
- Allow 192.168.154.128;
- Allow 192.168.154.1;
- Deny all;
- Proxy_cache_purge cache_one $ host: $ server_port $1 $ is_args $ args;
- }
Well, static page cache, dynamic requests are not cached!
Let's take a look at the purege configuration in the last section. Obviously, it indicates which IP addresses can be manually cleared by the specified URL.
The above describes nginx Reverse proxy, dynamic and static request separation, and nginx cache applications, as well as using ngx_cache_purge to clear the specified URL, including content, if you are interested in PHP tutorials.