Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active July 14, 2022 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilangvperdana/d33a9fc2014b266bc6dd7f9c0f722922 to your computer and use it in GitHub Desktop.
Save gilangvperdana/d33a9fc2014b266bc6dd7f9c0f722922 to your computer and use it in GitHub Desktop.
Multi Proxy Nginx with 443 Redirect and Hostname

Multi Proxy Pass Endpoint with One Domain on Nginx

This tutorial is suitable for you if you want to access some apps with one domain, just different path /

Goals

  • You can access on localhost to access localhost:5000
  • You can access on localhost/nodejs to access localhost:3000

Configuration

nano /etc/nginx/sites-enabled/default
server {
  listen 80 default_server;
  listen [::]:80 default_server;
  server_name res.bignetlab.com www.res.bignetlab.com;
  return 301 https://$host$request_uri;
}

server {
    listen 80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    if ($host != "res.bignetlab.com") {
        return 412;
    }
    
    server_name res.bignetlab.com www.res.bignetlab.com;
    ssl_certificate /etc/ssl/certs/res/res.crt;
    ssl_certificate_key /etc/ssl/certs/res/res.key;
    ssl_protocols TLSv1.2 TLSv1.1 TLSv1;

location / {
       proxy_pass http://127.0.0.1:5000;
    }
location /nodejs/ {
      proxy_pass http://127.0.0.1:3000/;
   }
}
service nginx reload

Redirect Proxy Configuration

If you want to redirect from https://localhost.com to https://another-localhost.com you can use :

 # backend.wants.this.server.com
 # browser.shows.this.server.com

server {
  listen 80;
  server_name browser.shows.this.server.com;

  location / {
     proxy_set_header Host backend.wants.this.server.com;
     proxy_redirect http://backend.wants.this.server.com/ http://browser.shows.this.server.com/; 
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment