Nginx – serving multiple website applications on port 80

It could be a little tricky for a less experienced Linux Administrator to setup multiple nginx configuration files in order to serve more than one website or one application for that matter. In this tutorial, I will walk you through the configuration setup and hopefully it will help you understand how nginx works.


Multiple applications being served on port 80

First Scenario:

I have two local python applications that are being served on localhost:5001 and localhost:5002. Now, I want both of these applications to be served on port 80.

Here is what the nginx configuration file would look like for first application that is live on “localhost:5001”.

upstream my_app1 {
 server localhost:5001;
}

server {
    listen 80;
    server_name my-app1.com;
    access_log  /var/logs/nginx/my_app1-access.log;
    error_log  /var/logs/nginx/my_app1-error.log;
    client_max_body_size 50M;

    location / {
        proxy_pass         http://my_app1/;
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_read_timeout 300;
        #auth_basic           "Authentication";
        # auth_basic_user_file /etc/nginx/secrets/nonprod-es.pswd;
        }

    location /nginx_status {
        stub_status on;
        access_log off;
    }
}

Here is the second configuration file for the second application on the same server that is live on “localhost:5002”

upstream my_app2 {
 server localhost:5002;
}

server {
    listen 80;
    server_name my-second-app.com;
    access_log  /var/logs/nginx/my_app2-access.log;
    error_log  /var/logs/nginx/my_app2-error.log;
    client_max_body_size 50M;

    location / {
        proxy_pass         http://my_app2/;
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_read_timeout 300;
        #auth_basic           "Authentication";
        # auth_basic_user_file /etc/nginx/secrets/nonprod-es.pswd;
        }

    location /nginx_status {
        stub_status on;
        access_log off;
    }
}

We are basically –
1. Using the “upstream” initially to inform nginx of the port where our application is live
2. and then passing the same upstream in the “location”
3. We’re also defining the log locations as well as “port 80” as an incoming port.
4. You would notice I have commented out two lines of “auth_basic”. You can enable them if you wish to place a simple HTTP auth when you don’t want the app to be public and share the credentials with your team for internal testing.

Scenario Two:

I have two different website files stored on my server and now I want to connect them to my domain names (two domains).

1. First website
Domain – first-website.com
Web Files – /var/www/first-website/html

2. Second website
Domain – second-website.com
Web Files – /var/www/second-website/htm

Here is the configuration file of the first-website.com.

Here is the configuration file of the second-website.com which is also being served from the same domain name.

The only different between an application such as Python, Node etc and a website is that you would be upstreaming tp the port for an application while you would simply be setting the website files root in case of a website. Hope you found this tutorial useful and feel free to comment if you have any questions.

Published by Nishant

With over 9 years in the industry, initially started as a Linux administrator and transitioned into DevOps Engineer. I work with deployment and infrastructure automation as well as application release management processes.

Leave a comment

Your email address will not be published. Required fields are marked *

Hooman? *