Nginx Reverse proxy, dynamic and static request separation, nginx cache application, and use ngx_cache_purge to clear the specified URL

Source: Internet
Author: User
Tags dedicated server nginx reverse proxy
: 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

  1. Upstream tomcat_server {
  2. Server 127.0.0.1: 8080;
  3. }
  4. Erver {
  5. Listen 80;
  6. Server_name www.codes51.com;
  7. Location /{
  8. Proxy_redirect off;
  9. Proxy_set_header Host $ host;
  10. Proxy_set_header X-Real-IP $ remote_addr;
  11. Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
  12. Proxy_pass http: // tomcat_server;
  13. }

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

  1. Server {
  2. Listen 192.168.154.128: 80;
  3. Server_name image.codes51.com;
  4. Index index.html;
  5. # Proxy_pass http: // tomcat_server;
  6. # Charset KOI8-R;
  7. # Access_log logs/host. access. log main;
  8. Location /{
  9. Root html;
  10. # Index index.html index.htm;
  11. Proxy_redirect off;
  12. Proxy_set_header Host $ host;
  13. Proxy_set_header X-Real-IP $ remote_addr;
  14. Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
  15. }
  16. Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf) $
  17. {
  18. Valid_referers none blocked 192.168.154.128 192.168.154.1;
  19. If ($ invalid_referer)
  20. {
  21. Rewrite ^/403.jpg break;
  22. }
  23. If (! -F $ request_filename ){
  24. Rewrite ^/404.jpg last;
  25. }
  26. Expires 30d;
  27. }
  28. # Error_page 404/404 .html;
  29. # Redirect server error pages to the static page/50x.html
  30. #
  31. Error_page 500 502 503 x.html;
  32. Location =/404.jpg {
  33. Root html;
  34. }
  35. }
  36. Tomcat
  37. Upstream tomcat_server {
  38. Server 127.0.0.1: 8080;
  39. }
  40. Server {
  41. Listen 192.168.154.128;
  42. Server_name www.codes51.com;
  43. Location /{
  44. Proxy_redirect off;
  45. Proxy_set_header Host $ host;
  46. Proxy_set_header X-Real-IP $ remote_addr;
  47. Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
  48. Proxy_pass http: // tomcat_server;
  49. }
  50. }

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

  1. # Tomcat
  2. Upstream tomcat_server {
  3. Server 127.0.0.1: 8080;
  4. }
  5. Server {
  6. Listen 192.168.154.128;
  7. Server_name www.codes51.com;
  8. Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf) $
  9. {
  10. Root html;
  11. }
  12. Location ~ . * \. (Jsp | do) $ {
  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. }

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

  1. Location/static
  2. {
  3. Root html/static;
  4. }

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

  1. ./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

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


Java code

  1. Upstream tomcat_server {
  2. Server 127.0.0.1: 8080;
  3. }
  4. Server {
  5. Listen 192.168.154.128;
  6. Server_name www.codes51.com;
  7. Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf) $
  8. {
  9. Proxy_cache cache_one;
  10. Proxy_cache_methods get head post;
  11. Proxy_cache_min_uses 1;
  12. Proxy_cache_valid 200 302 10 m;
  13. Proxy_cache_valid 404 1 MB;
  14. Proxy_cache_valid any 1 m;
  15. Proxy_cache_key "$ host: $ server_port $ uri $ is_args $ args ";
  16. Proxy_redirect off;
  17. Proxy_set_header Host $ host;
  18. Proxy_set_header X-Real-IP $ remote_addr;
  19. Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
  20. Proxy_pass http: // tomcat_server;
  21. }
  22. Location ~ . * \. (Jsp) $ {
  23. Proxy_redirect off;
  24. Proxy_set_header Host $ host;
  25. Proxy_set_header X-Real-IP $ remote_addr;
  26. Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
  27. Proxy_pass http: // tomcat_server;
  28. }
  29. Location ~ /Purge (/.*)
  30. {
  31. Allow 192.168.154.128;
  32. Allow 192.168.154.1;
  33. Deny all;
  34. Proxy_cache_purge cache_one $ host: $ server_port $1 $ is_args $ args;
  35. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.