Happyaa Server Deployment Note 2 (nginx static resource cache configuration)

Source: Internet
Author: User
Tags epoll nginx reverse proxy

I recently made a small number of improvements to the server, although the previous use of Nginx reverse proxy performance has improved, but still not enough, need to use caching to significantly improve the access speed of static resources.

The static resources on the server mainly have these: PNG, JPG, SVG, JS, CSS and so on. Below, I implement the cache with the new nginx configuration. I'll explain the words in red.

Worker_processes 1;Events {Worker_connections1024;    multi_accept on; Use Epoll;}http {include mime.types;Default_type Application/octet-stream;Sendfile on;Tcp_nopush on;Keepalive_timeout 65;    proxy_cache_path/svcwork/nginxcache levels=1:2 keys_zone=staticcache:10m inactive=1h max_size=2g;Server {Listen80;server_name localhost;Location /{Proxy_set_header Host $host;Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;Proxy_set_header X-forwarded-proto $scheme;Proxy_pass http://localhost:8080/;} location ^~ /resource {alias/resource;Expires 1h;} location ^~/hpaasvc {Rewrite ^ (. *) $ https://$host $1 Permanent;} location ~* \. ( Js|css|ico|gif|jpg|png|svg) $ {log_not_found off ;            Access_log off;            Expires 7d;            Proxy_pass http://localhost:8080;            Proxy_cache Staticcache; Proxy_cache_valid any 10m; } } server {Listen443 SSL;server_name localhost;Ssl_certificate Cert/cert.pem;Ssl_certificate_key Cert/cert.key;Ssl_session_cache shared:ssl:10m;Ssl_session_timeout 10m;Ssl_ciphers high:!anull:! MD5;Ssl_prefer_server_ciphers on;Location /{Proxy_set_header Host $host;Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;Proxy_set_header X-forwarded-proto $scheme;Proxy_pass http://localhost:8080/;} location ^~/resource {Alias/resource;Expires 1h;} location ~* \. (js|css|ico|gif|jpg|png|svg) $ {log_not_found off;Access_log off;Expires 7d;Proxy_pass http://localhost:8080;Proxy_cache Staticcache;Proxy_cache_valid any 10m;        }    }}

1, "Multi_accept on" and "use Epoll" can improve server throughput

2, "Proxy_cache_path/svcwork/nginxcache levels=1:2 keys_zone=staticcache:10m inactive=1h max_size=2g;" A server cache is configured, the cached server response (not necessarily a file or a response like 302,304) is stored in the/svcwork/nginxcache directory, and a two level directory is created based on the MD5 summary information of the cached response ( In general, storing too many files in a directory can result in degraded access to the file system. The cache name is Staticcache, and the name is used in the back; the cache uses 10M of memory (I see the other person's example is 100M, so I started with 100M, but found out that there is not enough memory, Nginx error, so it is a lot smaller); if there is no new action, the cached response will be deleted in 1 hours, the buffer can use up to 2G of disk space.

3, the location behind the "^~" means that once a match will not match the regular expression. If the URI that the user wants to access is "/resource/test/abc.png", then the "Location ^~/resource" rule will be used, ignoring the subsequent regular expression matches.

4, access_log off; In fact, I think Access_log has little meaning.

5, expires 7d; this thing is actually the client cache, through the HTTP header tells the browser this access resource is valid for 7 days, 7 days can not be to the server, but this depends on the specific browser implementation, such as IE press <F5> Will force all resources to be flushed to the server regardless of whether there is a cache.

6, Proxy_cache Staticcache; Specifies the cache to use. Well, that's the Staticcache defined earlier.

7, proxy_cache_valid any 10m;10 minutes to access the data will be "not fresh" ... What do you say? For the first time,/website/image/abc.png is cached, and the access to the resource is returned directly from the cache for 10 minutes without pass the request to the background. Try to get it back in 10 minutes. Note the difference between "inactive" and "Proxy_cache_path", inactive is the "life time" of the cached response, and each access to the resource refreshes the cached "Life time" to keep it alive in the cache. This means that the cached response is refreshed over such a long interval.

This article is a bit hasty and sloppy, there are questions to say ...

Happyaa Server Deployment Note 2 (nginx static resource cache configuration)

Related Article

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.