Starting with nginx

How to begin with nginx?

First steps

Easy, just start at the beginners' guide [1].

user www-data;
pid /run/nginx.pid;

http {
        # Basic Settings
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;

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

        # Logging Settings
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
}

Documentation of each keywords is available at [2]. For specific details about sendfile, tcp_nopush, tcp_nodelay see [3].

autoindex on; to enable directory listings.

My configuration with several static location, URL rewrite, and directory listings everywhere:

http {
    server {
        autoindex on;
        rewrite ^/n0/?(.*) /$1

        location / {
            root /mnt/data/inapp;
        }

        location /logs {
            root /tmp/inalogs;
        }

        location /i0/docs {
            root /tmp/inapp/i0/docs;
        }

        location /i1/docs {
            root /tmp/inapp/i1/docs;
        }

        location /i2/docs {
            root /tmp/inapp/i2/docs;
        }

        location /i3/docs {
            root /tmp/inapp/i3/docs;
        }
    }
}

For using systemd, debian has this:

events {
        worker_connections 768;
        # multi_accept on;
}

Adding a websocket proxy

A websocket proxy (copied from [4]):

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    upstream websocket {
        server 192.168.100.10:8010;
    }

    server {
        listen 8020;
        location / {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }
    }
}

This configuration will do this:

----------           ---------           ----------
| client | --8020--> | nginx | --8010--> | server |
----------           ---------           ----------

Adding multiple websocket proxies on the same port

Locations can be different from /, allowing to have multiple websocket services on the same TCP port, and everything can be mixed with normal http. For instance:

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    upstream websocket1 {
        server 192.168.100.10:8010;
    }

    upstream websocket2 {
        server 192.168.100.10:8020;
    }

    server {
        listen 80;

        location / {
            root /var/www
        }

        location /my-first-location {
            proxy_pass http://websocket1;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }

        location /my-other-location {
            proxy_pass http://websocket2;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }
    }
}

This configuration will do this:

----------           ---------           -----------
| client | ---80---> | nginx | --8010--> | server1 |
|        | ---80---> |       |           -----------
|        | ---80---> |       | --8020--> | server2 |
----------           ---------           -----------
Three channels on the same 80 port:
  • normal static http is served by nginx itself.
  • websocket to location /my-first-location are forwarded to 192.168.100.10:8010.
  • websocket to location /my-other-location are forwarded to 192.168.100.10:8020.

References

[1]nginx.org, Beginners guide, http://nginx.org/en/docs/beginners_guide.html
[2]nginx.org docs, Module ngx_http_core_module, http://nginx.org/en/docs/http/ngx_http_core_module.html
[3]Frédéric De Villamil, Optimisations Nginx : bien comprendre sendfile, tcp_nodelay et tcp_nopush, https://t37.net/optimisations-nginx-bien-comprendre-sendfile-tcp-nodelay-et-tcp-nopush.html
[4]nging.org, websocket, https://www.nginx.com/blog/websocket-nginx/