Home  >  Article  >  Operation and Maintenance  >  Static file caching and compression optimization under Nginx Proxy Manager

Static file caching and compression optimization under Nginx Proxy Manager

PHPz
PHPzOriginal
2023-09-26 09:54:201367browse

Nginx Proxy Manager下的静态文件缓存与压缩优化

Static file caching and compression optimization under Nginx Proxy Manager

Nginx is a high-performance web server and reverse proxy server, and Nginx Proxy Manager is a Nginx-based management tool that can easily manage multiple virtual hosts and reverse proxies. When using Nginx Proxy Manager to build a website, it is very important to optimize the caching and compression of static files, which can improve the performance and loading speed of the website. This article will introduce in detail how to optimize the caching and compression of static files under Nginx Proxy Manager, and provide some actual code examples for reference.

  1. Static file caching optimization

Static files include pictures, JavaScript files, CSS files, etc. They do not change frequently, so they can be cached to reduce the load on the server and Improve user access speed. Configuring the cache of static files in Nginx Proxy Manager is very simple. Just add the following code to the Nginx configuration file:

location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 1d;
}

The above code will set the cache time to 1 day. You can adjust the value of expires as needed, for example, expires 1h means 1 hour, expires 1w means 1 week, and so on.

  1. Static file compression optimization

The compression of static files can reduce the file size, thereby reducing the amount of data transmitted over the network and improving the loading speed of the website. Under Nginx Proxy Manager, you can use the gzip module to compress static files. First, confirm that the gzip module has been installed on the server, and then add the following code in the Nginx configuration file:

gzip on;
gzip_min_length 1000;
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/javascript;

The above code will enable gzip compression and set the minimum compressed file size to 1000 bytes. gzip_types specifies the file type that needs to be compressed. The above code includes common text files and JavaScript files. It can be adjusted according to the actual situation.

  1. Complete example of static file caching and compression optimization

The following is a complete Nginx Proxy Manager configuration file example, including optimization settings for static file caching and compression:

server {
    listen 80;
    server_name example.com;

    location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 1d;
    }

    location / {
        proxy_pass http://backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    gzip on;
    gzip_min_length 1000;
    gzip_types text/plain text/css application/javascript application/json application/x-javascript text/javascript;
}

In the above example, listen specifies the listening port, and server_name specifies the domain name. The caching and compression configuration of static files are placed in the corresponding location block. At the same time, the example also includes the configuration of a reverse proxy, which can forward requests from the client to the backend server.

Summary:

By optimizing the caching and compression of static files in Nginx Proxy Manager, the performance and loading speed of the website can be effectively improved. Static file caching can reduce the load on the server and speed up user access, while static file compression can reduce file size and reduce the amount of data transmitted over the network. Using the powerful features of Nginx, we can easily implement these optimization measures. I hope the above content is helpful to you, and I wish your website will be more efficient and optimized!

The above is the detailed content of Static file caching and compression optimization under Nginx Proxy Manager. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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