Home > Backend Development > PHP Tutorial > Detailed explanation of nginx high-performance configuration

Detailed explanation of nginx high-performance configuration

WBOY
Release: 2016-07-29 09:09:40
Original
1066 people have browsed it

Detailed explanation of

Copy after login
nginx configuration
#Global configuration
#High-level configuration
user www-data#Running user default configuration
pid /var/run/nginx.pid;#Port number default configuration
worker_processes 8;#Defines when nginx provides web services to the outside world The number of worker processes
#The optimal value depends on many factors, including (but not limited to) the number of CPU cores, the number of hard disks storing data, and the load mode
#Usually set to be equal to the number of cpu (setting to "auto" will Try to automatically detect it)
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;# Assign 8 processes to 8 cpu, of course you can write multiple, or assign one process to multiple cpu
worker _rlimit_nofile 200000;#Change worker process The maximum number of open files limit
#If not set, this value is the limit of the operating system
#After setting, your operating system and Nginx can handle more files than "ulimit -a", so set this value high. In this way, nginx will not have the "too many open files" problem
#If you fill in 10240, when the total concurrency reaches 30,000-40,000, the number of processes may exceed 10240, and a 502 error will be returned
#This command refers to when The maximum number of file descriptors opened by an nginx process. The theoretical value should be the maximum number of open files (ulimit -n) divided by the number of nginx processes. However, nginx allocation requests are not so uniform, so it is best to keep the value of ulimit -n Consistent
events {
worker_connections 102400; #Set the maximum number of connections that can be opened simultaneously by a worker process
#If the worker_rlimit_nofile mentioned above is set, we can set this value very high.
#Key point: The maximum number of clients is also limited by the number of available socket connections in the system (~ 64K), so there is no benefit in setting an unrealistically high number
#Theoretically, the maximum number of connections for each nginx server is worker_processes*worker_connections
multi_accept on; #Tell nginx to accept as many connections as possible after receiving a new connection notification
use epoll; #Set the polling method for reusing client threads
}
http {
server_tokens off; #Will not let nginx execute It's faster, but it can turn off the nginx version number in the error page, which is good for security
sendfile on; #Let sendfile() work
tcp_nopush on; #Tell nginx to send in a packet All header files, instead of sending them one by one
tcp_nodelay on; #Tell nginx not to cache the data, but to send it piece by piece - when data needs to be sent in time, this attribute should be set to the application, so that a small piece is sent The return value cannot be obtained immediately when data information is obtained
access_log off; #Set whether nginx will store access logs. Turning off this option can make read disk IO operations faster (aka, YOLO)
error_log /var/log/nginx/error.log crit; #Tell nginx to only log serious errors
keepalive_timeout 10; #Assign keep to the client -alive link timeout. The server will close the link after this timeout. We set it lower to allow ngnix to continue working longer
client_header_timeout 10; #Set the timeout for the request header. We can also set this lower.
client_body_timeout 10; #Set the timeout for the request body. We can also set this lower.
reset_timedout_connection on; #Tell nginx to close unresponsive client connections. This will free up the memory space occupied by that client.
send_timeout 10;#Specify the client's response timeout. This setting is not applied to the entire forwarder, but between client read operations. If the client does not read any data during this period, nginx will close the connection
limit_conn_zone $binary_remote_addr z>#Set the parameters of the shared memory used to save various keys (such as the current number of connections).
#5m is 5 megabytes, this value should be set large enough to store (32K*5) 32byte state or (16K*5) 64byte state.
limit_conn #Set the maximum number of connections for a 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
include /etc/nginx/mime.types; #Just one that includes another file in the current file Content instructions. Here we use it to load a series of MIME types that will be used later
default_type text/html; #Set the default MIME-type used by the file
charset UTF-8; #Set the default character set in the header file
gzip on; # tells nginx to send data in gzip compression. This will reduce the amount of data we send
gzip_disable "msie6"; # Disable gzip functionality for the specified client. We set it to IE6 or lower to make our solution widely compatible
# gzip_static on; #Tell nginx to look for pre-gzip processed resources before compressing them
#This requires you to pre-compress your files. This allows you to use the highest compression ratio so that nginx no longer has to compress these files.
gzip_proxied any; #Allow or disable compression of response streams based on requests and responses. We set it to any, which means all requests will be compressed.
gzip_min_length 1000; #Set the minimum number of bytes to enable compression for data.If a request is less than 1000 bytes,
#We'd better not compress it, because compressing these small data will slow down all processes handling this request
gzip_comp_level 4; #Set the compression level of the data. This level can be any value between 1-9, with 9 being the slowest but with the highest compression ratio.
#We set it to 4, which is a more compromise setting
gzip_buffers 16 8k;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript ;#Set the data format that needs to be compressed
open_file_cache max=100000 inactive=20s; #When opening the cache, you also specify the maximum number of caches and the cache time.
#We can set a relatively high maximum time so that we can clear them after they are inactive for more than 20 seconds
open_file_cache_valid 30s; #Specify the interval for detecting correct information in open_file_cache
open_file_cache_min_uses 2; #Defines the instructions in open_file_cache The minimum number of files during the parameter inactivity period
open_file_cache_errors on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

The above introduces the detailed explanation of nginx high-performance configuration, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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