Working NGINX webserver

Nginx and Apache are the most used open source web servers. However for larger web setups, Nginx outperforms Apache. Nginx hosting uses an Asynchronous approach which is event-driven for handling requests. This provides predictable performance under load, as against the Apache HTTP server which makes use of a threaded or process oriented approach for the same.

Though Nginx is smaller in size, it multiplies the performance remarkably by reducing the RAM and CPU usage for real time applications.

The features like load balancing, logical infrastructure, easy configuration, predicable performance and flexibility makes it a scalable solution for larger businesses and websites which handle millions of visitors every day.

Today over 10% of top 1 million sites are using Nginx. It is exclusively designed for heavy traffic websites with load balancing support as well as proxy support. If you are a system administration techy, Nginx is absolutely the best choice for you.

NGINX recommendations

For better performance we highly recommend use PHP-FPM with nginx web-server.

Basic Configuration

server {
    listen  10.0.48.100:80;
    server_name     example.com www.example.com
    root            /home/example.com/public_html;
    charset         utf-8;
    client_max_body_size       4m;
    client_body_buffer_size    128k;
    proxy_connect_timeout      90;
    proxy_send_timeout         90;
    proxy_read_timeout         90;
    proxy_buffer_size          16k;
    proxy_buffers              4 64k;
    proxy_busy_buffers_size    64k;
    proxy_temp_file_write_size 64k;
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/tmp/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
	}
    location / {
            index index.php;
    }
    if (!-e $request_filename) {
            rewrite  ^(.+)$  /index.php  last;
    }
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
            access_log        off;
            expires           30d;
    }
    location = /robots.txt { break; }
    location = /favicon.ico { break; }
    location ~*.(.htaccess|engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(.php)?|xtmpl)$ {
            deny all;
    }
}

PHP-FPM Environment Config

/usr/local/php/etc/env.conf
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 5
pm.max_requests = 500
pm.status_path = /status

listen.backlog = -1
listen.owner = www-data
listen.group = www-data
listen.mode = 0600

ping.path = /ping
ping.response = pong

request_terminate_timeout = 0
request_slowlog_timeout = 0

catch_workers_output = yes

php_flag[display_errors] = Off
php_flag[display_startup_errors] = Off
php_admin_value[memory_limit] = 64M
php_admin_value[upload_max_filesize] = 8M
php_admin_value[post_max_size] = 16M
		  

PHP-FPM Pool Configuration

/usr/local/php/etc/pools/example.com.conf
[examplecom]
include = etc/configuration/env.conf

user = www-data-example
group = www-data-example
listen = /home/example.com/var/run/php-fpm.sock

request_terminate_timeout = 30s ; 30 sec for work
request_slowlog_timeout = 5s ; add to slowlog after 5 sec

slowlog = /home/example.com/var/log/php-fpm-slow.log
#chroot = /home/example.com
#chdir = /public_html

php_flag[display_errors] = On
php_flag[display_startup_errors] = On

php_admin_value[error_log] = /home/example.com/var/log/php-fpm-error.log
php_admin_flag[log_errors] = on
php_admin_value[memory_limit] = 48M
php_admin_value[upload_tmp_dir] = /home/example.com/var/tmp

Nginx + PHP-FPM Configuration

Example for /etc/nginx/hosts-available/example.com.conf based on our production configuration.

upstream  BackEndID  {
    server unix:/home/example.com/var/run/php-fpm.sock;
}
server {
    listen  10.0.48.100:80;
    server_name     example.com www.example.com
    root            /home/example.com/public_html;
    charset         utf-8;
    client_max_body_size       4m;
    client_body_buffer_size    128k;
    proxy_connect_timeout      90;
    proxy_send_timeout         90;
    proxy_read_timeout         90;
    proxy_buffer_size          16k;
    proxy_buffers              4 64k;
    proxy_busy_buffers_size    64k;
    proxy_temp_file_write_size 64k;
    location ~ \.php$ {
            fastcgi_index  index.php;
            include /etc/nginx/fastcgi_params;
            fastcgi_intercept_errors on;
            fastcgi_param  SCRIPT_FILENAME  /home/example.com/public_html$fastcgi_script_name;
            if (-e $request_filename) {
                    fastcgi_pass BackEndID;
            }
    }
    location / {
            index index.php;
    }
    if (!-e $request_filename) {
            rewrite  ^(.+)$  /index.php  last;
    }

    # Don't cache static assets
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
            access_log        off;
            expires           30d;
    }
    location = /robots.txt { break; }
    location = /favicon.ico { break; }
    # Restricted files / directories and extensions (for end-user)
    location ~*.(.htaccess|engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(.php)?|xtmpl)$ {
            deny all;
    }
}

IPv6 Support

listen   [::]:80 default ipv6only=on;