nginx optimization

top-level configuration

user www-data;

pid /var/run/nginx.pid; 

worker_processes auto; 

worker_rlimit_nofile 100000;

user and pid default settings

worker_processes defines the number of worder processes when nginx provides web services to the outside world. When not sure, setting it to the number of CPU cores available will be a good start (setting it to "auto" will try to detect it automatically)

worker_rlimit_nofile Changes the maximum open file limit for worker processes. After setting the operating system and Nginx can handle more files than "ulimit -a", set this value high, so that nginx will not have "too many open files" problem

 

Events module

The events module contains all the settings for handling connections in nginx.

events {

    worker_connections 2048;

    multi_accept on;

    use epoll;

}

worker_connections sets the maximum number of connections that can be opened simultaneously by a worker process

multi_accept tells nginx to accept as many connections as possible after receiving a new connection notification

use sets the polling method used to reuse client threads. If you use Linux 2.6+ you should use epoll

 

HTTP module

http {

    server_tokens off;

    sendfile on;

    tcp_nopush on;

    tcp_nodelay on;

    ...

}

server_tokens doesn't make nginx run faster, but it turns off nginx version numbers in error pages, which is good for security

tcp_nopush tells nginx to send all headers in one packet instead of one by one

tcp_nodelay tells nginx not to cache data, but to send it piece by piece – when data needs to be sent in time, this property should be set for the application, so that when a small piece of data information is sent, the return value cannot be obtained immediately

 

reset_timeout_connection on;

Tell nginx to close unresponsive client connections. This will free the memory space occupied by that client

send_timeout 10;

Specifies the client's response timeout. This setting is not used for the entire repeater, but between client read operations. If the client does not read any data during this time, nginx will close the connection

limit_conn addr 100;

limit_conn sets the maximum number of connections for the given key. The key here is addr, and the value we set is 100, which means that we allow each IP address to open up to 100 connections at the same time.

 

 

gzip on;

gzip_disable "msie6";

# gzip_static on;

gzip_proxied any;

gzip_min_length 1000;

gzip_comp_level 4;

gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

gzip tells nginx to send data in gzip compressed form. This will reduce the amount of data we send

gzip_disable disables gzip functionality for the specified client

gzip_static tells nginx to look for pre-gzipped resources before compressing them

gzip_min_length sets the minimum number of bytes to enable compression on data. If a request is less than 1000 bytes, we better not compress it, because compressing these small data will slow down all processes handling this request

gzip_comp_level sets the compression level of the data. This level can be any number between 1-9, with 9 being the slowest but the most compressed. We set it to 4, which is a rather compromised setting

 

 

# cache informations about file descriptors, frequently accessed files # can boost performance, but you need to test those values 

open_file_cache max=100000 inactive=20s;

open_file_cache_valid 30s;

open_file_cache_min_uses 2; 

open_file_cache_errors on;

open_file_cache also specifies the maximum number of caches and the duration of the cache when opening the cache. We can set a relatively high max time so we can clear them after they are inactive for more than 20 seconds

open_file_cache_valid specifies the interval for checking correct information in open_file_cache

open_file_cache_min_uses defines the minimum number of files during the period of inactivity of the command parameter in open_file_cache

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326260291&siteId=291194637