Home > Operation and Maintenance > Nginx > How to use Nginx for high-performance static file caching

How to use Nginx for high-performance static file caching

王林
Release: 2023-08-02 16:13:11
Original
1926 people have browsed it

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.

  1. Configuring Nginx for static file access
    In order to enable the static file caching function, you first need to configure Nginx to identify and process requests for static files. Usually, static files include images, CSS files, JavaScript files, etc. The following is a simple Nginx configuration example:
server {
    listen 80;
    server_name example.com;

    root /path/to/static/files;

    location ~* .(jpg|jpeg|png|gif|css|js)$ {
        expires 30d;
        access_log off;
    }
}
Copy after login

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.

  1. Configuring Nginx for static file caching
    In order to enable Nginx's static file caching function, we can add some additional configuration instructions. Here is an example:
location ~* .(jpg|jpeg|png|gif|css|js)$ {
    expires 30d;
    access_log off;
    add_header Cache-Control "public";
    add_header Pragma public;
    etag off;
}
Copy after login

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 offThe Etag header information is disabled because in some cases, Etag may cause some compatibility issues.

  1. Verify whether the static file cache is effective
    In order to verify whether the static file cache is effective, you can use the browser's developer tools or command line tools to check. In the browser's developer tools, you can view the 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
Copy after login

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
Copy after login
  1. Dynamic Update Cache
    Sometimes we may need to dynamically update the cache, such as when a static file is modified. You can solve this problem by adding the version number to the file name or path. For example, assuming there is a CSS file 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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template