Lan Yiyun: Nginx common skills

When it comes to common Nginx tips, we can discuss some practical configurations and functions to improve the performance, security, and reliability of the Nginx server. Here are some commonly used Nginx tips:

  1. Configure gzip compression: Enabling gzip compression can reduce the amount of data transmitted, speed up website loading, and improve user experience. Gzip compression can be enabled with the following configuration:
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  1. Configure cache: Use the proxy_cache directive to enable the Nginx reverse proxy cache function, reduce the pressure on the back-end server, and improve response speed. For example:
proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=100m inactive=60m;
server {
    ...
    location / {
        proxy_cache my_cache;
        proxy_pass http://backend_server;
    }
}
  1. Load balancing: Use the upstream module to achieve simple load balancing, distribute requests to multiple back-end servers, and improve system stability and throughput. For example:
upstream backend_servers {
    server backend1.example.com;
    server backend2.example.com;
}

server {
    ...
    location / {
        proxy_pass http://backend_servers;
    }
}
  1. Configure firewall and security restrictions: Limits on request frequency and number of connections can be set through Nginx's limit_req and limit_conn modules to prevent malicious requests and denial of service attacks.
limit_req_zone $binary_remote_addr zone=my_limit:10m rate=10r/s;
limit_conn_zone $binary_remote_addr zone=my_conn_limit:10m;

server {
    ...
    location / {
        limit_req zone=my_limit burst=20 nodelay;
        limit_conn my_conn_limit 10;
        ...
    }
}
  1. Customized error page: The error_page directive can be used to customize the error page, provide a more friendly user experience, and handle specific errors.
server {
    ...
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location /404.html {
        root /path/to/error_pages;
    }

    location /50x.html {
        root /path/to/error_pages;
    }
    ...
}
  1. Configure HTTP/2: Enabling HTTP/2 can improve the loading speed and performance of the website. You can enable HTTP/2 through the following configuration:
server {
    ...
    listen 443 ssl http2;
    ssl_certificate /path/to/ssl_certificate.crt;
    ssl_certificate_key /path/to/ssl_certificate.key;
    ...
}
  1. Configuring redirection: URL redirection can be implemented through the rewrite directive to redirect user requests to the specified URL.
server {
    ...
    location /old-url {
        rewrite ^/old-url(.*)$ /new-url$1 permanent;
    }
    ...
}

The above are some commonly used Nginx tips that can help optimize and enhance the functionality of the Nginx server. According to specific needs and business scenarios, Nginx can be further customized and configured to meet specific requirements. Using these tips, you can improve the performance, security, and stability of your site and provide a better experience for your users.

Guess you like

Origin blog.csdn.net/tiansyun/article/details/133387426