Skip to content

Serving multiple applications with nginx

Learn how to serve multiple applications with nginx. Nginx can serve multiple applications’ static content that is built using any web technology. All you need to do is to point nginx to the root path, index file, host name and port of your application.

Nginx can handle many different websites/apps on a single node through multiple server blocks, one for each site. Each server block is typically stored as a separate file within the directory /etc/nginx/sites-available.

For example, the server block for your static site with the domain name “yourapp.com” might be located in /etc/nginx/sites-available/yourapp.com and it would look like:

server {
    listen         80;
    listen         [::]:80;
    server_name    example.com www.example.com;
    root           /home/appname/www;
    index          index.html;
    try_files $uri /index.html;
}

Regardless of the installation source, server configuration files will contain a server block (or blocks) for a website.

If you want to add a new application to your nginx, open nginx.conf which is typically located in /etc/nginx.

sudo vi /etc/nginx/nginx.conf

Then add the server block for your application.

server {

  listen 8080;
  #        listen       [::]:80 default_server;
  server_name localhost;
  root /usr/share/nginx/dist/your-app-ui;
  index index.html index.htm;

  # Load configuration files for the default server block.
  include /etc/nginx/default.d/*.conf;

  location /api {

    proxy_pass http://localhost:3300/api;
  }

  location / {

    try_files $uri $uri/ /index.html;
  }


  error_page 404 /404.html;
  location = /40x.html {

  }

  error_page 500 502 503 504 /50x.html;
  location = /50x.html {

  }
}

server {

  listen 8020;
  server_name localhost;
  root /usr/share/nginx/dist/dist/projectsam;
  index index.html index.htm;

  location /api {

    proxy_pass http://localhost:3300/api;
  }

}

#New block
server {

  listen 8030;
  server_name localhost;
  root /usr/share/nginx/dist/new_app/html;
  index SOTIHowTosUserManagement.html;
}

Restart nginx after adding your server block.

sudo service nginx restart

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.