nginx different name for different port

I have two web applications (webapp1 port 7000 & webapp2 port 8000) for internal use hosted on my server VM with IP address 192.168.15.10 (ubuntu 20.04)

I want to see if it is possible to assign different names for the two web applications.

How I want things to work is:

If an internal user enters webapp1.random-company.com in the browser, it will direct the user to webapp1 with port 7000 (192.168.15.10:7000)

If an internal user enters webapp2.random-company.com in the browser, it will direct the user to webapp2 with port 8000 (192.168.15.10:8000)

I tried using nginx on 192.168.15.10 and was able to direct users to webapp1.random-company.com (192.168.15.10:7000). However, I failed to direct users to webapp2.random-company. In fact, when an user types webapp1.random-company.com:8000, it directs the user to webapp2.

Is this something that is possible?

One application is built in django and the other one is built in flask

1 Answer

Don't address ports in URLs, the power of nginx is its reverse proxy capability.

First, create different config files for each web application, do not just smash everything in one server configuration - or even worse, in the nginx.conf file. Set an upstream above your main server block for each application:

upstream webapp1 { server 127.0.0.1:7000 weight=1 fail_timeout=0; #the timeout and weight settings are optional }

Inside the server blocks, when nginx accesses the location "/", call the upstream:

server { listen 443 ssl http2; #if you go with HTTPS - which you should server_name webapp1.random-company.com;
[...]
location / { [...] proxy_pass }

Repeat for webapp2 accordingly:

upstream webapp2 { server 127.0.0.1:8000; }
server { listen 443 ssl http2; #if you go with HTTPS - which you should server_name webapp2.random-company.com; [...]
location / { [...] proxy_pass }

Concerning the Django / Flask applications, you don't actually need to call the applications via TCP/IP, you could have nginx directly listen to their UNIX sockets. What do you use to deliver the application? uWSGI, Gunicorn, (...)?

Further reading for Flask on uWSGI

In general this is not necessarily an Ubuntu topic, perhaps you would want to check StackOverflow or Serverfault instead.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like