Our nginx cheat sheet


An nginx config file. All config files are in /etc/nginx/sites-available and must be named with the suffic .conf.

To actually activate them you must also create a symbolic link to each of them in /etc/nginx/sites-enabled. This Debian-specific convention is okay for us. It has the advantage that we can easily disable individual sites or do temporary magics.

Our additional convention is to name them xxx.conf, where xxx matches the subdomain they serve.

Static websites

The main website serves at two subdomains www.example.com and example.com. When somebody types just example.com (without www) in their browser, we permanently redirect them to the canonical www.example.com. Here is a template for a typical www.conf file:

server {
  server_name www.example.com example.com;
  if ($host = example.com) { return 301 https://www.$host$request_uri; }
  root /path/to/www;
  index index.html;
  listen 80;

For additional subdomains the .conf file is even shorter:

server {
  server_name foo.example.com;
  root /path/to/foo;
  index index.html;
  listen 80;

How to add a new subdomain

You have already a website foo.example.com and now want to have another website bar.example.com:

$ sudo nano /etc/nginx/sites-available/bar.conf

Paste content from above templates and edit as needed:

$ sudo su
# cd /etc/nginx/sites-enabled
# ln -s /etc/nginx/sites-available/bar.conf .
# service nginx restart
# certbot --nginx -d bar.example.com

Note that the first filename given to ln -s should be the full path.