use cases:

  • efficient reverse proxies, or
  • fast, efficient serving of static content

It is acclaimed

  • low memory usage
  • recommended for sites running on a VPS

Lighttpd runs as a single process with a single thread and non-blocking I/O. nginx works as one master process but delegates its work unto worker processes.

The main advantage of the asynchronous approach is scalability. In a process-based server, each simultaneous connection requires a thread which incurs significant overhead. An asynchronous server, on the other hand, is event-driven and handles requests in a single (or at least, very few) threads. serving 10,000 simultaneous connections would probably only cause Nginx to use a few megabytes of RAM whereas Apache would probably consume hundreds of megabytes

The difference in speed between lighttpd and nginx is really negligible, as you see nginx might outperform lighttpd on some workloads and vice versa. What really matters is stability, CPU consumption, and ease of use. And nginx really beats lighttpd there - by far.

Nginx è più leggero per servire file statici quali immagini, file javascript, file css. Questi file non vengono generalmente renderizzati in modo dinamico da php e quindi Nginx li serve direttamente senza caricare in memoria interpreti. quindi, Nginx potrebbe essere usato per servire file statici scaricando di questo compito apache e guadagnando memoria.

definizioni nel file:

/etc/nginx/sites-available

NOTA:legge le pagine generate dalla webapp all'indirizzo locale:

test01.{$client}ad.local

PHP

vi /etc/nginx/conf.d/default.conf
service nginx restart
server {
    location / {
        rewrite ^/(.*)$ /index.php?q=$1;
    }
}
 
# Configure nginx to point at our public/ directory and set APPLICATION_ENV
server {
    listen       80;
    server_name  localhost;
    root         /vagrant/public;
    index        index.php index.html index.htm;
    access_log   /var/log/nginx/default-access.log  main;
    error_log    /var/log/nginx/default-error.log;
 
    location / {
        try_files $uri $uri/ @rewrite;
    }
    location @rewrite {
        index index.php;
        rewrite ^(.*)$ /index.php;
    }
 
    location ~ \.php {
        include                  fastcgi_params;
        fastcgi_keep_conn        on;
        fastcgi_index            index.php;
        fastcgi_split_path_info  ^(.+\.php)(/.+)$;
        fastcgi_param            PATH_INFO $fastcgi_path_info;
        fastcgi_param            SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param            APPLICATION_ENV php7dev;
        fastcgi_intercept_errors on;
        fastcgi_pass             unix:/var/run/php-fpm.sock;
    }
}

FastCGI:

server {
        
listen       80;
        
server_name  zf2-app.localhost;
        
root         /path/to/zf2-app/public;

        
location / {
            
index index.php;
            
try_files $uri $uri/ @php;
        }

        
location @php {
            
# Pass the PHP requests to FastCGI server (php-fpm) on 127.0.0.1:9000
            fastcgi_pass   127.0.0.1:9000;
            
fastcgi_param  SCRIPT_FILENAME /path/to/zf2-app/public/index.php;
            include 
fastcgi_params;
        }
    }

PHP/Java/Golang behind Nginix

server {
        listen 80;
        server_name go.dev;
        root /var/www/test_go;
        index index.html;
        #gzip off;
        #proxy_buffering off;
 
        location / {
                 try_files $uri $uri/;
        }
 
        location ~ /app.* {
                include         fastcgi.conf;
                fastcgi_pass    127.0.0.1:9090;
        }
 
        try_files $uri $uri.html =404;
}

configurazioni comuni

Proxy:

resolver 8.8.4.4;
server {
    listen 3128;
    location / {
        proxy_pass http://$http_host$request_uri;
    }
}

Reverse Proxy:

server {
    listen 80;
    server_name new-site.com;
 
    location / {
        proxy_pass http://origin-site.com/;
        proxy_redirect default;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        port_in_redirect    on;
        server_name_in_redirect off;
        proxy_connect_timeout 300;
    }
}

Cached Reverse Proxy:

Make directories for cache:

mkdir -p /var/cache/nginx/cache
mkdir -p /var/cache/nginx/temp
Paste below log_format directive in nginx.conf:
 
client_body_buffer_size  512k;
proxy_connect_timeout    5;
proxy_read_timeout       60;
proxy_send_timeout       5;
proxy_buffer_size        16k;
proxy_buffers            4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path   /var/cache/nginx/temp;
proxy_cache_path  /var/cache/nginx/cache levels=1:2 keys_zone=cache_one:500m inactive=7d max_size=30g;
Add to vhost configuration:
 
proxy_cache cache_one;
proxy_cache_valid  200 304 3d;
proxy_cache_key $host$uri$is_args$args;
expires 10d;

Set Expires Headers For Static Content

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires 1y;
    log_not_found off;
}

Increase HTTP Post Size Limit

http {
    #...
    client_max_body_size 20m;
    #...
}

Set Correct Files/Folders Permissions

chown -R www-data:www-data .
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;

References: