Using nginx to cache static files on the server

Time:2021-3-3

1、 Advantages of nginx cache

Using nginx to cache static files on the server

As shown in the figure, nginx caching can reduce the processing pressure of the source server to a certain extent. Because many static files (such as CSS, JS, pictures) are not updated frequently. Nginx uses proxy_ Cache caches the user’s request to a local directory. The next same request can call the cache file directly, so there is no need to request the server. After all, the processing of IO intensive services is the strength of nginx.

2、 How to set up

First, a chestnut:

http{
    proxy_connect_timeout 10;
    proxy_read_timeout 180;
    proxy_send_timeout 5;
    proxy_buffer_size 16k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 96k;
    proxy_temp_file_write_size 96k;
    proxy_temp_path /tmp/temp_dir;
    proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=10g;


    server {
        listen       80 default_server;
        server_name  localhost;
        root /mnt/blog/;

        location / {

        }

        #To cache the suffix of a file, you can set it below.
        location ~ .*.(gif|jpg|png|css|js)(.*) {
                proxy_ pass  http://ip Address: 90;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_cache cache_one;
                proxy_cache_valid 200 302 24h;
                proxy_cache_valid 301 30d;
                proxy_cache_valid any 5m;
                expires 90d;
                add_header wall  "hey!guys!give me a star.";
        }
    }

    #Blog port without nginx cache
    server {
        listen  90;
        server_name localhost;
        root /mnt/blog/;

        location / {

        }
    }
}
Copy code

Because I amExperiment on a server, so two ports are used80and90Simulate the interaction between two servers.

80Port docking is a common domain name(http://wangxiaokai.vip)Visit.
90The port is responsible for processing80Resource access from port proxy.
amount to90The port is the source server,80The port is nginx reverse caching proxy server.


Next, let’s talk about configuration items

2.1 HTTP Layer Settings

proxy_connect_timeout 10;
    proxy_read_timeout 180;
    proxy_send_timeout 5;
    proxy_buffer_size 16k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 96k;
    proxy_temp_file_write_size 96k;
    proxy_temp_path /tmp/temp_dir;
    proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=10g;
Copy code
  • proxy_connect_timeout Timeout for server connection
  • proxy_read_timeout After successful connection, wait for the back-end server response time
  • proxy_send_timeout Back end server data return time
  • proxy_buffer_size The size of the buffer
  • proxy_buffers The number of buffers for each connection is number, and the size of each buffer is size
  • proxy_busy_buffers_size After the buffer response function is turned on, nginx will send a response to the client when the write buffer reaches a certain size without reading all the responses until the buffer is less than this value.
  • proxy_temp_file_write_size Set the size limit for nginx to write data to temporary files every time
  • proxy_temp_path The storage path of the temporary files received from the back end server
  • proxy_cache_path Set the path and other parameters for the cache. If the cached data is not accessed within the time specified by the inactive parameter (currently 1 day), it will be removed from the cache

2.2 server layer settings

2.2.1 reverse caching proxy server

server {
        listen       80 default_server;
        server_name  localhost;
        root /mnt/blog/;

        location / {

        }

        #To cache the suffix of a file, you can set it below.
        location ~ .*.(gif|jpg|png|css|js)(.*) {
                proxy_ pass  http://ip Address: 90;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_cache cache_one;
                proxy_cache_valid 200 302 24h;
                proxy_cache_valid 301 30d;
                proxy_cache_valid any 5m;
                expires 90d;
                add_header wall  "hey!guys!give me a star.";
        }
    }
Copy code
  • proxy_pass Nginx cache can not get resources, forward the request to the address, get new resources, and cache
  • proxy_redirect Set the replacement text of "location" response header and "Refresh" response header of back-end server
  • proxy_set_header Allows you to redefine or add request headers to back-end servers
  • proxy_cache Specifies the shared memory used for page caching, corresponding to the keys set by the HTTP layer_ zone
  • proxy_cache_valid Set different cache time for different response status codes
  • expires Cache time
    • *

Here I set itpicturecssjsStatic resources. When the user entershttp://wangxiaokai.vipDomain name, the resolution is obtainedip:portAccess address of.portThe default is 80. Therefore, the page request will be intercepted by the current server for request processing. When the static resource is resolved to the end of the file name, the static resource will be obtained from the cache. If the corresponding resource is obtained, the data will be returned directly. If not, forward the request toproxy_passPoint to the address of.

2.2.2 source server

server {
        listen  90;
        server_name localhost;
        root /mnt/blog/;

        location / {

        }
    }
Copy code

It’s dealt with directly here90Port to receive the request, to the server local directory/mnt/blogThe system grabs resources to respond.

3、 How to verify whether the cache is valid

Careful readers should find that I left a colored egg in the chestnut in the second paragraphadd_header wall "hey!guys!give me a star."add_headerIs used to set custom information in the header. Therefore, if the cache is valid, the header returned by the static resource must carry this information.

visithttp://wangxiaokai.vipThe results are as follows

Using nginx to cache static files on the server

4、 Reference

[1] Nginx document
[2] Detailed explanation of nginx reverse cache proxy
[3] Nginx cache server static files