Question: My Nginx.conf is Full of Garbage, Do I Need All This and Does It Work?

Hi,

Compared to the clean nginx.conf file you posted in your informative blog post, mine looks like the control center for a spaceship built in an insane asylum. I am running on Ubuntu 11.10, with PHP-FPM, APC, Batcache plugin and APC plugin on Wordpress subdomain multisite, on a test server but sometimes on some subdomains the posts are missing, or work intermittently. Are these rewrite rules correct , your plugin just throws as few warnings but seems to work with them:

user www-data;  
worker_processes 2;  
pid /var/run/nginx.pid;  

events  
{  
worker_connections 1500;  
use epoll;  
}  

http  
{  
include mime.types;  
charset utf-8;  
sendfile on;  
tcp_nopush on;  
tcp_nodelay off;  
server_tokens off;  

default_type  application/octet-stream;  
index index.php index.htm index.html redirect.php;  
keepalive_timeout 15;  
keepalive_requests 2000;  
types_hash_max_size 2048;  
server_name_in_redirect off;  
port_in_redirect off;  
server_names_hash_bucket_size 64;  
open_file_cache max=1000 inactive=300s;  
open_file_cache_valid 360s;  
open_file_cache_min_uses 2;  
open_file_cache_errors off;  
client_body_buffer_size 128K;  
client_header_buffer_size 1k;  
client_max_body_size 2m;  
large_client_header_buffers 4 8k;  
client_body_timeout   10m;  
client_header_timeout 10m;  
send_timeout          10m;  
error_log /var/log/nginx/error.log;  
access_log off;  
##########---gzip settings  
gzip on;  
gzip_vary on;  
gzip_proxied any;   
gzip_comp_level 5;  
gzip_min_length  512;  
gzip_buffers 32 8k;   
gzip_http_version 1.0;  
gzip_disable        "MSIE [1-6]\.";  
application/x-javascript text/xml application/xml application/xml+rss text/javascript;  
gzip_types  
    text/css   
    text/javascript  
    text/xml  
    text/plain  
    text/x-component  
    application/javascript  
    application/x-javascript  
    application/json  
    application/xml  
    application/rss+xml  
    font/truetype  
    font/opentype  
    application/vnd.ms-fontobject  
    image/svg+xml;  
geoip_country  /etc/nginx/geoip/GeoIP.dat; # the country IP database  
include /etc/nginx/conf.d/*.conf;  
include /etc/nginx/sites-enabled/*;  
}  

and this is the contents of the default file in etc/nginx/sites-enabled/. Strangely I can no logner find the etc/nginx/conf.d/ directory and var/run/nginx.pid is ther but neither a directory nor a file.. maybe symbolic link...

map $host $wp_dir_id  
{  
    domain1-main.com 1;  
    domain2.com 2;  
    domain3.com 3;  
    domain4.com 4;  
}  

server  
{  
    listen                  80;  
    server_name     .domain-main.com;  
    root                    /var/www/;  

    ##########---wp network site file  
    if ( $wp_dir_id != 1 )  
    {  
        rewrite ^/files/(.*)$ /wp-content/blogs.dir/$wp_dir_id/files/$1 last;  
    }  

    ##########---fallback if wp network site not mapped  
    if ( $wp_dir_id = 1 )  
    {  
           rewrite ^(/[^/]+/)?files/(.+) /wp-includes/ms-files.php?file=$2 last;  
    }  

    location /   
    {  
        add_header "X-UA-Compatible" "IE=Edge,chrome=1";   
        location ~* \.(?:manifest|appcache|html|xml|json)$   
        {  
            expires -1;  
            add_header Pragma public;  
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";  
        }  
        ##########---Feed  
        location ~* \.(?:rss|atom)$   
        {  
            expires 1h;  
            add_header Pragma public;  
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";  
        }  
        ##########---Favicon  
        location ~* \.ico$  
        {  
            expires 1w;  
            access_log off;  
            add_header Pragma public;  
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";  
        }  
        ##########---Media  
        location ~* \.(?:jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm)$   
        {  
            expires 1M;  
            access_log off;  
            add_header Pragma public;  
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";  
        }  
        ##########---CSS and Javascript  
        location ~* \.(?:css|js)$  
        {  
            expires 1y;  
            access_log off;  
            add_header Pragma public;  
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";  
        }  
        try_files $uri $uri/ @rewrites;  
    }   
    ##########---end  block: location /  
    location @rewrites   
    {  
          ##########---Match only one section  
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;  
        rewrite ^(/[^/]+)?(/.*\.php) $2 last;  
        ##########---Send everything else to index.php (permalinks)  
        rewrite ^/(.*)$ /index.php?q=$1 last;  
    }  
    ##########--- hide files starting with .  
    location ~ /\.   
    {  
        deny all;  
        log_not_found off;  
    }  
    ##########---enable nginx status screen  
    location /nginx_status   
    {  
        stub_status on;  
    }  
    ##########---pass to PHP5-FPM  
    location ~ .php   
    {  
        fastcgi_param   QUERY_STRING            $query_string;  
        fastcgi_param   REQUEST_METHOD          $request_method;  
        fastcgi_param   CONTENT_TYPE            $content_type;  
        fastcgi_param   CONTENT_LENGTH          $content_length;  
        fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;  
        fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;  
        fastcgi_param   REQUEST_URI             $request_uri;  
        fastcgi_param   DOCUMENT_URI            $document_uri;  
        fastcgi_param   DOCUMENT_ROOT           $document_root;  
        fastcgi_param   SERVER_PROTOCOL         $server_protocol;  
        fastcgi_param   GATEWAY_INTERFACE       CGI/1.1;  
        fastcgi_param   SERVER_SOFTWARE         nginx;  
        fastcgi_param   REMOTE_ADDR             $remote_addr;  
        fastcgi_param   REMOTE_PORT             $remote_port;  
        fastcgi_param   SERVER_ADDR             $server_addr;  
        fastcgi_param   SERVER_PORT             $server_port;  
        fastcgi_param   SERVER_NAME             $server_name;  
        fastcgi_param   GEOIP_COUNTRY_CODE   $geoip_country_code;  
            fastcgi_param   GEOIP_COUNTRY_CODE3      $geoip_country_code3;  
            fastcgi_param   GEOIP_COUNTRY_NAME   $geoip_country_name;  

        ##########---PHP only, required if PHP was built with --enable-force-cgi-redirect  
        fastcgi_param   REDIRECT_STATUS         200;  
        fastcgi_index                           index.php;  
        fastcgi_connect_timeout                 60;  
        fastcgi_send_timeout                    180;  
        fastcgi_read_timeout                    180;  
        fastcgi_buffer_size                     128k;  
        fastcgi_buffers                         4       256k;  
        fastcgi_busy_buffers_size               256k;  
        fastcgi_temp_file_write_size            256k;  
        fastcgi_intercept_errors                on;  
        fastcgi_ignore_client_abort             on;  
        fastcgi_split_path_info ^(.+\.php)(/.+)$;  
        fastcgi_pass unix:/var/run/php5-fpm.sock;  
    } ##########---end block: location ~ .php   
} ##########---end block: server  

Hi Nasty,

Its really time-consuming to debug/optimize a config like you pasted above. If it works for you then you can continue with it.

Generally speaking, you have added too many things in 1/2 files. You can refactor config by following conventions we use here - http://rtcamp.com/tutorials/wordpress-nginx-setup-conventions/

Refactoring will help you when something will go wrong and you will need to debug things!

Another big issue, I see is you are too many unwanted rules. For example, most of fastcgi_param lines might be present in /etc/nginx/fastcgi_params files. If that file is there, you just need to write include fastcgi_params line in php location-block.

By the way refactoring is different that performance benefits. Refactoring saves human-time. Performance enhancement will save CPU-time and other machine resources. :slight_smile: