Skip to main content

Command Palette

Search for a command to run...

Proxying to your app running on custom port in NGINX

Published
1 min read
Proxying to your app running on custom port in NGINX

More than ever, if you're managing a server either as an EC2 instance on AWS, or as a VM instance on GCP, or just working on your local machines, proxying to your app's custom port comes in handy if:

  • Your app uses a protocol other than HTTP/HTTPS
  • You are working towards balancing the load across multiple containers
  • You want to test new features as canary deployments
  • and so on...

Here's a quick .conf snippet to proxy pass a request coming on port 80 to port 8081 where your custom app is running:

server {
    listen 80;
    access_log /path/to/log/access.log;
    error_log /path/to/log/error.log;

    location / {
        proxy_pass http://localhost:8081;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection upgrade;
        proxy_set_header Accept-Encoding gzip;
    }
}

Of course, you can replace the ports with your own values.

614 views
H

Great article. Can you tell me how to make nginx for sticky sessions. If I have multiple instances of same service running on different port.

1
H

Hey, I think sticky sessions is something that NGINX Plus offers at the moment. However, I've not really worked with NGINX Plus (as it's not free).

For simple load balancing between multiple instances of the same service running on different ports, here's a configuration that I use:

http {
upstream backend {
   server localhost:3000;
   server localhost:3001;
}

server {
        location / {
            proxy_pass http://backend;
        }
    }
}

NanoBits

Part 2 of 2

This series includes quick nano-sized articles, hacks and experiences, typically helping you solve an issue or learn something new, in less than 5 minutes.

Start from the beginning

Port a website to the Dark Web in under 2 minutes

It's as easy as running a tiny docker container using tor-nginx-proxy.

More from this blog

P

Prodengg by Harshit Budhraja - The Official Blog

12 posts

Experimenting my way through & writing about a plethora of "geeky" stuff - from open-source & Javascript to APIs & Dark Web - I'm a product and engineering enthusiast 🚀