PS: I’ve been using Nginx for two or three years now, and I often encounter new users asking very basic questions that I don’t have time to answer one by one. This afternoon, I spent some time combining my own experience with some online references to share explanations of the main Nginx configuration parameters. This is currently the most complete Chinese-language explanation of Nginx configuration parameters. For more detailed module parameters, please refer to: http://wiki.nginx.org/Main
#Define the user and user group that Nginx runs as
user www www;
#Number of Nginx processes, recommended to set equal to the total number of CPU cores.
worker_processes 8;
#Global error log definition type, [ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log info;
#Process file
pid /var/run/nginx.pid;
#The maximum number of file descriptors a single Nginx process can open. The theoretical value should be the maximum number of open files (system value from ulimit -n) divided by the number of Nginx processes, but Nginx request allocation is not even, so it is recommended to keep it consistent with the ulimit -n value.
worker_rlimit_nofile 65535;
#Working mode and connection limit
events
{
#Reference event model, use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; the epoll model is a high-performance network I/O model in Linux kernel versions 2.6 and above. If running on FreeBSD, use the kqueue model.
use epoll;
#Maximum connections per process (Maximum connections = connections * number of processes)
worker_connections 65535;
}
#Configure the HTTP server
http
{
include mime.types; #File extension and file type mapping table
default_type application/octet-stream; #Default file type
#charset utf-8; #Default encoding
server_names_hash_bucket_size 128; #Hash table size for server names
client_header_buffer_size 32k; #Upload file size limit
large_client_header_buffers 4 64k; #Set request buffer
client_max_body_size 8m; #Set request body limit
sendfile on; #Enable efficient file transfer mode. The sendfile directive specifies whether Nginx calls the sendfile function to output files. For normal applications, set to on. If used for download applications or other heavy disk I/O scenarios, set to off to balance disk and network I/O processing speed and reduce system load. Note: If images display abnormally, change this to off.
autoindex on; #Enable directory listing access, suitable for download servers, default is off.
tcp_nopush on; #Prevent network congestion
tcp_nodelay on; #Prevent network congestion
keepalive_timeout 120; #Keep-alive connection timeout, in seconds
#FastCGI-related parameters are for improving website performance: reducing resource usage and increasing access speed. The parameters below are self-explanatory.
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#Gzip module settings
gzip on; #Enable gzip compression output
gzip_min_length 1k; #Minimum file size for compression
gzip_buffers 4 16k; #Compression buffer
gzip_http_version 1.0; #Compression HTTP version (default is 1.1; use 1.0 if the frontend is squid2.5)
gzip_comp_level 2; #Compression level
gzip_types text/plain application/x-javascript text/css application/xml;
#Compression types, text/html is included by default, so you don’t need to write it below. Writing it won’t cause issues, but it will produce a warning.
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m; #Needed when enabling IP connection limit
upstream blog.ha97.com {
#Upstream load balancing. weight determines the priority based on machine configuration. The weight parameter represents the weight value; a higher weight means a greater chance of being assigned.
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}
#Virtual host configuration
server
{
#Listening port
listen 80;
#Domain names can be multiple, separated by spaces
server_name www.ha97.com ha97.com;
index index.html index.htm index.php;
root /data/www/ha97;
location ~ .*.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
#Image cache time setting
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 10d;
}
#JS and CSS cache time setting
location ~ .*.(js|css)?$
{
expires 1h;
}
#Log format setting
log_format access '$remote_addr – $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#Define the access log for this virtual host
access_log /var/log/nginx/ha97access.log access;
#Enable reverse proxy for "/"
location / {
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#Backend web server can get the user’s real IP via X-Forwarded-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#Following are some reverse proxy configurations, optional.
proxy_set_header Host $host;
client_max_body_size 10m; #Max single file bytes allowed for client request
client_body_buffer_size 128k; #Buffer proxy max bytes buffering client request body
proxy_connect_timeout 90; #Nginx connection timeout to backend server (proxy connection timeout)
proxy_send_timeout 90; #Backend server data return time (proxy send timeout)
proxy_read_timeout 90; #Backend server response time after successful connection (proxy read timeout)
proxy_buffer_size 4k; #Buffer size for proxy server (nginx) to save user header information
proxy_buffers 4 32k; #proxy_buffers buffer, setting for average webpage size below 32k
proxy_busy_buffers_size 64k; #Buffer size under high load (proxy_buffers*2)
proxy_temp_file_write_size 64k;
#Set cache folder size, data will be transferred from upstream server if larger than this value
}
#Set the address to view Nginx status
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
#The content of the htpasswd file can be generated using the htpasswd tool provided by Apache.
}
#Local dynamic/static separation reverse proxy configuration
#All jsp pages are handled by tomcat or resin
location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}
#All static files are read directly by nginx without passing through tomcat or resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
}
}
For more detailed module parameters, please refer to: http://wiki.nginx.org/Main
Source : http://www.ha97.com/5194.html