Usage

Newest Stable Version: 1.8.0 (Apr 21, 15)

Initial Release:    Oct 2004

http://www.nginx.org

http://nginx.org/en/docs/

Installation

sudo apt-get update
sudo apt-get install nginx
sudo service nginx start

Optional

sudo apt-get install php5-fpm
sudo ufw allow 80
sudo ufw enable

Main Configuration

/etc/nginx/nginx.conf
/etc/nginx/sites-available/
/etc/nginx/sites-enabled/

Paths

default nginx.conf

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

events {
        worker_connections 768;
        # multi_accept on;                                                                                                                                                                                                                          
}

http {

        ##                                                                                                                                                                                                                                          
        # Basic Settings                                                                                                                                                                                                                            
        ##                                                                                                                                                                                                                                          

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;                                                                                                                                                                                                         
        # server_name_in_redirect off;                                                                                                                                                                                                              

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##                                                                                                                                                                                                                                          
        # Gzip Settings                                                                                                                                                                                                                             
        ##                                                                                                                                                                                                                                          

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;                                                                                                                                                                                                                             
        # gzip_proxied any;                                                                                                                                                                                                                         
        # gzip_comp_level 6;                                                                                                                                                                                                                        
        # gzip_buffers 16 8k;                                                                                                                                                                                                                       
        # gzip_http_version 1.1;                                                                                                                                                                                                                    
        # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

Optimized nginx.conf

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

events {
        worker_connections 2048;
        multi_accept       on;
        use                epoll;
}

http {
        client_body_buffer_size 10K;
        client_header_buffer_size 1k;
        client_max_body_size 100M;
        server_names_hash_bucket_size 64;
        large_client_header_buffers 2 1k;

        client_body_timeout 12;
        client_header_timeout 12;
        send_timeout 10;

        ##                                                                                                                                                                                                                                          
        # Basic Settings                                                                                                                                                                                                                            
        ##                                                                                                                                                                                                                                          

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 15;
        types_hash_max_size 2048;
        server_tokens off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        gzip  on;
        gzip_vary on;
        gzip_comp_level 2;
        gzip_min_length 1000;
        gzip_proxied expired no-cache no-store private auth;
        gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
        gzip_disable "MSIE [1-6]\.";

        server {
               location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
                   expires 30d;
                }
        }

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

php5-fpm

sudo emacs /etc/php5/fpm/pool.d/www.conf
listen = /var/run/php5-fpm.sock
sudo service php5-fpm restart
/etc/php5/fpm/php.ini
/etc/php5/fpm/pool.d/www.conf

Config Files

Server Block / vHost

server {
    listen 80;
    server_name nginx.com www.nginx.com;

    set $root_path '/var/www/vhosts/nginx/app/webroot';
    root $root_path;

    index index.html index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }

    location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }

    location ~ /\.ht {
        deny all;
    }
}
/etc/nginx/sites-available/nginx.com.vhost
sudo service nginx restart

Misc

Password Protect

location ^~ /secret_folder {
         auth_basic            "Restricted Area";
         auth_basic_user_file  /var/www/nginx/.htpasswd;
}

location ^~ /secret_file.txt {
         auth_basic            "Restricted Area";
         auth_basic_user_file  /var/www/nginx/.htpasswd2;
}

Other ports (phpMyAdmin)

server {
    listen 7173;
    server_name nginx.com;

    root /usr/share/phpmyadmin/;
    index index.php index.html index.htm;

    location ~ ^/(.+\.php)$ {
        try_files $uri =404;
        root /usr/share/phpmyadmin/;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
    
    location ~* ^/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
        root /usr/share/phpmyadmin/;
    }
}

SSL

server {
    listen 80;
    listen 443 ssl;
    server_name nginx.com www.nginx.com;

    set $root_path '/var/www/vhosts/nginx/app/webroot';
    root $root_path;

    index index.html index.php;

    ssl_certificate /etc/ssl/nginx.crt;
    ssl_certificate_key /etc/ssl/nginx.key;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
}

Pros / Cons

Pros

  • Running apache2 and nginx on the same server resulted in much better uptime/speeds in my own experience. Initial, basic, performance testing was showing this as well. (ab)
  • Greater flexibility and overall easier to use compared to apache2
  • Large sites are increasingly gravitating to it. Same with PHP developers.

Cons

  • More knowledge about apache2, using nginx requires learning an entirely new piece of software.
  • Less support for nginx due to more people using apache2.
  • With the newest version of apache2, the gap between the two appears to be smaller.
  • Less modules/extensions than apache.

nginx

By Charlie Page

nginx

  • 790