razzi.abuissa.net

Razzi's guide to nginx

2023-10-04

nginx and apache are the 2 most common http servers in my experience. The gunicorn docs recommend nginx so I’m learning how to use it, otherwise I’d use apache since that’s what the Wikimedia foundation uses.

installation

$ sudo apt install nginx

Right off the bat, it’ll be running on port 80:

$ curl -I localhost:80
HTTP/1.1 200 OK
Server: nginx/1.22.0 (Ubuntu)
...

If it’s not working, try checking the logs:

$ sudo journalctl -u nginx

You can also check nginx config with sudo nginx -t:

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Another useful command is to dump the config with sudo nginx -T, that way you can see all the config it is loading. It checks the nginx config first, so it has the same first 2 lines as the nginx -t output.

$ sudo nginx -T
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# configuration file /etc/nginx/nginx.conf:
user www-data;
...

running nginx based on a local config file

As we’ve seen above, usually nginx loads /etc/nginx/nginx.conf. If you’d like to experiment with a different config in a local directory, you can use nginx -c $conf passing an absolute path to the config file:

$ sudo nginx -c $(pwd)/nginx.conf

You can reload the config like so:

$ sudo nginx -c $(pwd)/nginx.conf -s reload
2025/02/17 00:15:07 [notice] 15190#15190: signal process started

Here’s a minimum viable config:

pid /home/razzi.linux/hack/nginx.pid;

events {
        worker_connections 768;
}

http {
        access_log /home/razzi.linux/hack/access.log;

        server {
                listen 80 default_server;

                root /home/razzi.linux/hack;

                index index.html;

                location / {
                        try_files $uri $uri/ =404;
                }
        }
}

Note that the permissions on the folder given (and its parents) have to be readable by the nginx user, otherwise you’ll get 404 not found.

project page

https://nginx.org/

source code

depends on