Optimize Nginx Configuration

Posted on February 4, 2014 at 11:35 pm

Edit the /etc/nginx/nginx.conf file as follow:

user nginx;
 
# Set the number of worker processes
# Type the command "grep processor /proc/cpuinfo | wc -l" to get the correct number
# You can also set it to "auto" to let Nginx decide the right number
worker_processes auto;
 
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
 
# Increase workers rlimit nofile
worker_rlimit_nofile 150000;
 
events {
    # Increase worker connections
    worker_connections 150000;
 
    # Accept() as many connections as possible
    # More information http://wiki.nginx.org/EventsModule
    multi_accept on;
 
    # Serve many clients with each thread (Linux)
    use epoll;
}
 
http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    # Disable access log to boost I/O on HDD
    access_log off;
 
    # Tweak TCP connections handling
    sendfile on;
    tcp_nopush on;
 
    # Increase keepalive timeout
    keepalive_timeout 65;
 
    # Reset timedout connections and free up some memory
    reset_timedout_connection on;
 
    # Hide Nginx version
    server_tokens off;
 
    # Other buffers/timeouts optimizations
    server_names_hash_bucket_size 256;
    types_hash_max_size 2048;
    client_max_body_size 32k;
    client_body_buffer_size 32k;
    client_body_in_single_buffer on;
    client_body_timeout 180s;
    client_header_timeout 180s;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
 
    # Enable GZIP compression to save bandwidth
    gzip on;
    gzip_disable "msie6";
    gzip_comp_level 6;
    gzip_min_length 1100;
    gzip_buffers 16 8k;
    gzip_proxied any;
    gzip_types text/plain application/xml text/css text/js text/xml application/x-javascript text/javascript application/json application/xml+rss;
 
    # Cache information about file descriptors
    open_file_cache max=200000 inactive=20s; 
    open_file_cache_valid 30s; 
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
 
    include /etc/nginx/conf.d/*.conf;
}

Some of my tests with Blitz.io and Nginx:
1 Billion hits/day with Nginx + PHP5-FPM + MySQL
500 Million hits/day with Nginx + PHP-FPM + MySQL

References and useful links:

Nginx Tuning For Best Performance
Optimizing web server performance with Nginx and PHP
Implementing Full-Page caching with Nginx and PHP
12,000 Requests per second with Nginx, PHP and Memcached
Optimizing Nginx for High Traffic Loads
nginx-push-stream-module configuration for >1MM concurrent subscribers

Updated on March 27, 2014 at 10:58 am

Other Posts

Updated Posts