How to use Nginx for high-performance static file caching
Nginx is a lightweight open source web server that has received widespread attention and use for its high performance and high concurrency capabilities. In addition to being a web server, Nginx also has an important function, which is to provide static file caching function, which can greatly optimize the access speed and performance of the website. This article will introduce how to use Nginx for high-performance static file caching and provide corresponding code examples.
server { listen 80; server_name example.com; root /path/to/static/files; location ~* .(jpg|jpeg|png|gif|css|js)$ { expires 30d; access_log off; } }
In the above configuration, listen
specifies the port Nginx listens on, and server_name
specifies the domain name of the server . root
specifies the root directory where the static files are located. The location
directive specifies the matching URL pattern and the corresponding processing parameters. In the above configuration, the regular expression ~* .(jpg|jpeg|png|gif|css|js)$
matches the suffix of jpg, jpeg, png, gif, css or js file, expires
specifies that the cache validity period is 30 days, and access_log off
disables access logging to static files.
location ~* .(jpg|jpeg|png|gif|css|js)$ { expires 30d; access_log off; add_header Cache-Control "public"; add_header Pragma public; etag off; }
In the above example, the add_header
directive adds two header information, namely Cache-Control
and Pragma
. These two headers tell clients and other caching servers that the cached copy is available for a certain period of time. etag off
The Etag header information is disabled because in some cases, Etag may cause some compatibility issues.
Cache-Control
and Expires
fields of the HTTP response header, as well as the size of the response content. If the cache takes effect, after accessing the static file for the first time, you will see that the values of Cache-Control
and Expires
are consistent with the above configuration when you request again, and the size of the response content will be become very small. In addition, you can use command line tools such as curl to view HTTP response header information. For example, you can execute the following command to view the HTTP response header information of an image file:
$ curl -I example.com/path/to/image.jpg
If caching is effective, you will see lines similar to the following in the results:
Cache-Control: public, max-age=2592000 Expires: Thu, 10 Aug 2023 08:16:50 GMT
style.css
, we can rename it to style.v1.css
and update Nginx’s configuration file to match the new file name. This way, every time the CSS file is modified, you only need to update the version number in the file name. In addition, Nginx also provides a reload command that can reload the configuration file without stopping the server. For example, you can execute the following command to reload the Nginx configuration file:
$ nginx -s reload
In this way, Nginx will re-read the configuration file, and the updated configuration will take effect immediately.
Summary
By using Nginx for static file caching, the performance and access speed of the website can be significantly improved. In this article, we introduce how to configure Nginx to enable static file access and caching functions, and provide corresponding code examples and verification methods. Hopefully this content will help you optimize your website performance.
The above is the detailed content of How to use Nginx for high-performance static file caching. For more information, please follow other related articles on the PHP Chinese website!