How to use Nginx for high-performance static file caching
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.
- 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; } }
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.
- 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; }
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.
- 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 theCache-Control
andExpires
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 ofCache-Control
andExpires
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
- 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 filestyle.css
, we can rename it tostyle.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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP
