Nginx

From MattWiki
Jump to: navigation, search
Nginx
Nginx.gif
Developer(s) Nginx dev team
Stable release
1.13.0 / April 25, 2017;
4 days ago
 (2017-04-25)
Operating system Cross-platform
Type Web server
License 2-clause BSD-like license
Website nginx.org

nginx (pronounced “engine-x”) is an open source Web server and a reverse proxy server for HTTP, SMTP, POP3 and IMAP protocols, with a strong focus on high concurrency, performance and low memory usage. It is licensed under a BSD-like license and it runs on Unix, Linux, BSD, Mac OS X, Solaris, AIX and Microsoft Windows.[1]

Nginx Configuration Options

  • expires - Controls whether the response should be marked with an expiry time, and if so, what time that is.
  • gzip - This module allows for on-the-fly gzip compression. See also Gzip pre-compression module.
  • gzip pre-compression - Before serving a file from disk to a gzip-enabled client, this module will look for a precompressed file in the same location that ends in ".gz".

Nginx Proxy Configuration Options

A Very Simple Nginx Proxy Setup

In the below example, you need to set nginx-ip-here & ip-address-of-your-server.

server { 
    #Turns access log off
    access_log off;

    #Set this to the IP on the nginx server you want to represent this proxy
    listen nginx-ip-here:80;

    #Matches the requested hostname
    server_name your-hostname.com www.your-hostname.com;

    location / {
        # Tells the destination server the IP of the visitor (some take X-Real-IP, 
        # some take X-Forwarded-For as their header of choice
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        # Tells the destination server which hostname you want
        proxy_set_header Host $host;
        # Performs a Proxy Pass to that server
        proxy_pass http://ip-address-of-your-server;
    }
}

Multiple Server Configuration (with two domains)

upstream  www.domain1.com  {
       server   192.168.10.11:80;
       server   192.168.10.12:80;
}

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

       location / {
                proxy_pass         http://www.domain1.com;
                proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
       }
}

upstream  www.domain2.com  {
       server   192.168.10.21:80;
       server   192.168.10.22:80;
}

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

       location / {
                proxy_pass         http://www.domain2.com;
                proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
       }
       access_log  /usr/local/nginx/logs/domain2.log  www;
}

Controlling Nginx

Changing Configuration

After updating the configuration of Nginx, you can send a HUP signal to the master process to have Nginx reload the configuration. [2]

nginx -s reload

Rotating Nginx's Log files

Upgrading Nginx's binaries

References

  1. nginx.org - Retrieved 2011-10-15.
  2. Nginx.org - Controlling nginx - Reconfiguration - Retrieved 2012-02-18.