Skip to content
On this page




With docker, you can install nginx with something like:

version: "3"
    # image: nginx:latest
    image: nginx:stable
    container_name: boilerplate_web
    # restart: unless-stopped
      - ./ui/dist:/srv/boilerplate/dist
      - ./web/static:/srv/boilerplate/static
      - ./web/ssl:/etc/nginx/ssl
      - ./web/default.conf:/etc/nginx/conf.d/default.conf
      # mount any static / public content for efficient serving directly
      # ./web/public;/srv/boilerplate/files/
    working_dir: /srv/boilerplate/
      # for development
      # leave off '' if you want to expose the service beyond localhost
      # (useful when you want to access dev instance of boilerplate remotely
      #  from e.g. a phone)
      # - 8888:80
      # for production
      # - 80:80
      # - 443:443
    # if the container doesn't run, there may be a problem with the nginx.conf
    # try running `docker-compose log web` for clues
    # (usually SSL keys have not yet been generated in `web/ssl`)
    # keep the container running to debug or run interactively with
    #     docker-compose exec web bash
    # entrypoint: ["tail", "-f", "/dev/null"]
          - boilerplate.local

This is a good option to encapsulate all necessary settings for an application stack.


On some servers with multiple services running (e.g. a server instead of a development machine), it may help to run one nginx server at the host level and configure that to proxy to other services as needed.

sudo apt-get install nginx


The main config file is typically found:


On a fresh install, by default it is configured to point to :

Can have as many server contexts as needed:

# main context

http {

    # http context

    server {

        # first server context


    server {

        # second server context



Generally these are configured in separate files in



sudo vi /etc/nginx/sites-enabled/default

You should look at the following URL's in order to grasp a solid understanding of Nginx configuration files in order to fully unleash the power of Nginx.

Then set it to run on startup

sudo systemctl start nginx
sudo systemctl enable nginx

See also: Startup Services

Disable the default site and add in the desired configuration:

sudo rm /etc/nginx/sites-enabled/default 

Ok to use either /etc/nginx/conf.d/ or /etc/nginx/sites-available and link to the enabled ones in /etc/nginx/sites-enabled. Call the files something.conf

server {
  listen 80;
  listen [::]:80;


  location / {
      proxy_pass http://localhost:3000/;

Alias vs Root

be sure to include trailing slash with alias directives!

Static Content

When developing web applications, it's tempting to use the application server to serve static content:

This works for local development, but becomes a headache when it's time to deploy your application publicly.

Using a dedicated webserver like nginx for serving static files removes that burden from the application server.

Auto Indexes

If you have static content that would benefit from a dynamic index, nginx can do that. For example, want to get a list that includes new files without manually knowing the new file path. Alternatively, an application server could navigate and iterate through the file system.

    location /static {
        alias /srv/static/;
        autoindex on;
        autoindex_format json;
    } Configuring NGINX to Serve a Directory Listing Nginx list static files/directories as XML/Json - Stack Overflow nginx list directory contents as json at DuckDuckGo




For local development environments, self signed is good enough. It makes sure that the application is configured correctly to work behind an HTTPS connection, but doesn't require an external certificate / local DNS entry.

Use OpenSSL to create a self signed certificate

openssl req -subj '/CN=localhost' -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365

Let's Encrypt

Once it's time to publish your application, you'll need a domain name. If you have that ready to go, you can use Let's Encrypt for a certificate


Log files are typically stored in


With containers

docker-compose exec web bash


nginx -t 

is super helpful for debugging nginx configurations

To see if the nginx process is running:

service nginx status

(On the nginx docker container, ps is not available)

To reload nginx without reloading all of the containers:

docker-compose -p boilerplate exec web nginx -s reload

For netstat:

apt-get update
apt-get install net-tools

netstat -pan | grep LISTEN

See also dedicated section for nginx