Raz Buchnik
Raz Buchnik

Reputation: 8391

How to serve multiple web servers using nginx?

I have installed nginx and I want to serve two different web applications under the same user on the same server.

This is the config I already use:

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    return 301 https://www.example.com$request_uri;
}

# HTTPS — proxy all requests to the Node app
server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.example.com;

    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
      expires 30d;
      add_header Vary Accept-Encoding;
      access_log off;
    }
    root /home/myuser/main/dist;

    # Use the Let’s Encrypt certificates
    ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;

    # Include the SSL configuration from cipherli.st
    include snippets/ssl-params.conf;
}

As you can see, I have under /home/myuser directory a directory called main and a dist directory in it. There there are static files that are successfully served using nginx.

I want to add another directory under the myuser directory, called for example, test.

So I will have /myuser/test and there to server another web application. Using the very same nginx server.

I have tried to write many variants in the config file I mentioned above but it couldn't work.

The config file located in: /etc/nginx/sites-enabled/example.com.conf

I edit it using sudo.

Upvotes: 1

Views: 5731

Answers (1)

Timo Stark
Timo Stark

Reputation: 3071

If you want to host different static files from local directories a configuration could look like this:

Notice: Your location uri (/, /one) will be appended to the root directory path if using the root directive.

The root directive can be used in every location block to set the document root. http://nginx.org/en/docs/http/ngx_http_core_module.html#root

This is the reason why alias exists. With alias the location will not become a part of the directory path. Check this out: http://nginx.org/en/docs/http/ngx_http_core_module.html#alias

1. One Domain - Multi-Location

server {


  server_name example.com;
  listen 443 ssl;
  .....
  root /home/user/main/dist;
  location / {
     index index.html;
     # If you have some sort of React or Angular App you might want to use this
     # try_files $uri $uri/ /index.html;
     # If you just host a local files (css, js, html, png)...
     # try_files $uri $uri/ =404;
  }

  location /two {
     alias /home/main/example;
     index index.html;
     ..... 
  }


}

2. Two Domains - Single Location

server {


  server_name example.com;
  listen 443 ssl;
  .....
  root /home/user/main/dist;
  location / {
     index index.html;
     # If you have some sort of React or Angular App you might want to use this
     # try_files $uri $uri/ /index.html;
     # If you just host a local files (css, js, html, png)...
     # try_files $uri $uri/ =404;
  }

}

server {


  server_name example1.com;
  listen 443 ssl;
  .....
  root /home/user/main/test;
  location / {
     index index.html;
     # If you have some sort of React or Angular App you might want to use this
     # try_files $uri $uri/ /index.html;
     # If you just host a local files (css, js, html, png)...
     # try_files $uri $uri/ =404;
  }

}

Upvotes: 4

Related Questions