PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, June 25, 2022

[FIXED] How to use NGINX reverse proxy, and proxy the port and path to the upstream url?

 June 25, 2022     nginx, reverse-proxy     No comments   

Issue

I've set up nginx reverse proxy as per the instructions here: https://medium.com/@mightywomble/how-to-set-up-nginx-reverse-proxy-with-lets-encrypt-8ef3fd6b79e5

My sites-enabled/mysite.conf looks like

server {      
 server_name mynginxreverseproxy.com; 
 set $upstream serverip:$server_port/; 
 location / {  
    proxy_pass_header Authorization;  
    proxy_pass http://$upstream;  
    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_http_version 1.1;  
    proxy_set_header Connection “”;  
    proxy_buffering off;  
    client_max_body_size 0;  
    proxy_read_timeout 36000s;  
    proxy_redirect off;  }

    listen 443 ssl; # paths to ssl cert files

} server {
    if ($host = mynginxreverseproxy.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


 server_name mynginxreverseproxy.com; listen 80;
    return 404; 

}

I would like to forward the port and path to my server, such as https://mynginxreverseproxy.com:portnumber/myurl -> to serverip:com:portnumber/myurl. I've been able to get this working for a single port which can forward port 80 to my desired port, but how to I scale it out to work for many ports?

I have many port mapping that I'd like to set up, so it would be ideal to do this using a variable, rather than hardcoding each port mapping.

How do I pass the port and path through using a variable in the conf file?


Solution

I eventually got this working after a lot of debugging and configurations.

The

set $upstream serverip:$server_port

was correct, but I was missing the

listen directive which allows the NGINX server to listen on a particular port, it listens on port 80 by default. However, port 80 was not the port which I wanted to get passed through to the upstream server.



Answered By - fuzzi
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing