Install nginx

sudo apt install nginx
Copy the code

File location

  • /usr/sbin/nginx: main program
  • /etc/nginx: stores the configuration file
  • /usr/share/nginx: stores static files
  • /var/log/nginx: stores logs

Start the command

Service nginx start nginx service nginx reloadCopy the code

Two other commands

Nginx -s reopen # nginx -s stop #Copy the code

To be clear, nginx as a static resource server does not cache, only in reverse proxy caching

Nginx. conf configuration file

## # Proxy Setting ## 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_busY_buffers_size 96K; If the write buffer reaches a certain size, nginx will send the response to the client until the buffer is smaller than this value. proxy_temp_file_write_size 96k; Proxy_temp_path /etc/nginx/temp; Proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone= cache_ONE: 100M Inactive = 1D max_size=10g; Set the cache path and other parameters. Cached data is removed from the cache if it is not accessed within the time specified by the Inactive parameter (currently 1 day)Copy the code

Nginx only has caching for reverse proxies

The /etc/nginx/temp and /etc/nginx/cache files must be created and assigned write permissions in advance

The simulation assumes that the resource is at /var/www/example.com and the entry is at /var/www/html

server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name _; To cache the file's suffix, you can set it in the following way. The location ~. * \. (GIF | JPG | PNG | jpeg | | js) CSS (. *) {proxy_pass http://127.0.0.1:90; Proxy_redirect off: if you can't redirect a new resource to nginx, redirect it to that address. Proxy_set_header Host $Host; Proxy_cache cache_one allows redefining or adding request headers to back-end servers Proxy_cache_valid 200 302 24h; proxy_cache_valid 301 30d; proxy_cache_valid any 5m; expires 3m; Add_header wall "hey! guys! give me a star."; } location / { } } server { listen 90; listen [::]:90; root /var/www/example.com; location / { } }Copy the code

Test, a second refresh appears

Server cache file

Delete server cache file, can be identified as browser cache

Select Disable Cancel to force the refresh

HTML disables browser caching

<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragm" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache" content="no-cache">
Copy the code

The server does not cache only HTML files

Cache build package JS, CSS and other files with time stamps (HTML introduced update timely)

server { listen 80 default_server; listen [::]:80 default_server; server_name _; #add_header X-Via $server_addr; To cache the file's suffix, you can set it in the following way. The location ~. * \. (GIF | JPG | PNG | jpeg | | js) CSS (. *) {# on different HTTP status code set different cache time proxy_pass http://127.0.0.1:90; proxy_redirect off; proxy_set_header Host $host; proxy_cache cache_one; Proxy_cache_key $host$URI $is_args$args; #proxy_cache_valid 200 10s; proxy_cache_valid any 100s; # Cache file expiration time, 304 if not expired, 200 if expired, retry cache expires 30s; Add_header nginx-cache "$upstream_cache_status"; } location / {proxy_pass http://127.0.0.1:90; } } server { listen 90; listen [::]:90; root /var/www/not_save/; index index.html; $request_filename ~*.*\.($request_filename ~*.*\.(? :htm|html)$) { add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate"; } try_files $uri $uri/ /index.html =404; }}Copy the code

When nginx sets expires, for example: Expires 10d; The user will only access the cache in the browser and will not request Nginx for any request within 10 days.

Response Headers

Cache-control: max-age=” Expiration time/second “; Expires: Indicates the expiration time

Changing only server static resources during this time cannot be updated in a timely manner

First visit

Access resources, direct download

Server static resource cache file

Second access (Expires cache not expired)

Refresh the page directlymemory cache

200 Form Memory Cache Memory cache

If the server is not accessed, the cache is directly read from the memory after the resource has been loaded and cached in the memory. When the browser closes, the data will not exist (the resource has been freed), and when the same page is opened again, the from Memory cache will not appear.

Close the browser and open it againdisk cache

200 From Disk cache Hard drive cache

If the browser is closed, the data still exists. The resource will not be released when the page is closed. If the page is opened next time, the resource will still be from disk cache.

Second access (Expires cache expired)

If the server cache expires, it will revisit the background data and return 200. If the server cache does not expire, it will return 304

*304 Not Modified

The server returns this status code after accessing the server and finding that the data has not been updated. The data is then read from the cache.

Enforce an update (Expires or not, HTML introduces static resource changes)

The front-end Webpack packages build, HTML files. The front-end HTML is forbidden to cache, and the server does not cache, so it will directly return the latest HTML file to read the latest static file

add_header Nginx-Cache “$upstream_cache_status”;

MISS missed, request sent to back end HIT cache HIT EXPIRED Cache EXPIRED request sent to back end UPDATING cache, STALE reply will be used back end will get STALE replyCopy the code

Expires

expires 30s; Cache30 expires 30m; # Cache expires 2h for 30 minutes; # Cacheexpires 30d for 2 hours Cache for 30 daysCopy the code

Extend the knowledge cache concrete type

  • Cache-control: Max – age = XXXX, public

Both client and proxy servers can cache this resource; If the client has a request for the resource within XXX seconds, it directly reads the cache,statu code:200, and sends an HTTP request to the server if the user refreshes the resource

  • Cache-control: Max – age = XXXX, private

Only the client can cache the resource; The proxy server does not cache the client directly reads the cache in XXX seconds,statu code:200

  • Cache-control: Max – age = XXXX, immutable

If the client needs to request the resource within XXX seconds, it directly reads the cache,statu code:200, and does not send HTTP requests to the server even if the user refreshes the resource

  • cache-control: no-cache

Skip setting strong cache, but do not prevent setting negotiated cache; If you have a strong cache, you will only use the negotiation cache if the strong cache fails. If you set no-cache, you will not use the negotiation cache.

  • cache-control: no-store

No caching, this will make the client and server do not cache, there is no so-called strong cache, negotiated cache.

  • Max-age: indicates the relative expiration time of the cache in seconds.

– private, positive max-age: the server will not be accessed during the rollback. – no-cache, positive max-age: the server will be accessed during the rollback.

  • must-revalidate

The cache must verify the state of the old resource before using it, and it must not use expired resources. If the page is expired, go to the server to obtain it.

  • proxy-revalidate

It has the same effect as must-revalidate, but applies only to shared caches (such as proxies) and is ignored by private caches.

Reference article:

Nginx cache:www.cnblogs.com/tugenhua070…
Do not cache HTML:www.cnblogs.com/cntzyw/p/14…
Negotiated cache and mandatory cache:www.jianshu.com/p/9c95db596…
Private, no-store, no-cache, must-revalidate, proxy-revalidateSegmentfault.com/a/119000000…