AnswerBun.com

Nginx: How do I forward an HTTP request to another port?

Server Fault Asked by WoooHaaaa on October 13, 2020

What I want to do is: When someone visits http://localhost/route/abc the server responds exactly the same as http://localhost:9000/abc

Now I configure my Nginx server like this:

location /route {
    proxy_pass  http://127.0.0.1:9000;
}

The HTTP request is dispatched to port 9000 correctly, but the path it receives is http://localhost:9000/route/abc not http://localhost:9000/abc.

Any suggestions?

4 Answers

vim nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

  server {
    listen 8080;

    location / {
      proxy_pass http://compute-1-36:8787;
      proxy_redirect http://compute-1-36:8787/ $scheme://$host:8080/;
    }
  }

This code listen on 8080 and redirect to port 8787 on compute-1-36. You can select other path in location /

Answered by user3266656 on October 13, 2020

I hate the subtlety here, but try adding a / at the end of 9000 like below. It will no longer append "route" to the forwarded request now.

location /route {
    proxy_pass  http://127.0.0.1:9000/;
}

Answered by Shark on October 13, 2020

I believe you can use rewrite to remove the extra part of the URL. In your case I think you could use:

location /route/ {
    rewrite ^/route/?(.*)$ /$1 break;    
    proxy_pass  http://127.0.0.1:9000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

However if your app has internal links in it, they may still point to /abc/foo , and if you do this they instead need to point to /route/abc/foo so that the raw request comes in correctly. You may be better off leaving the nginx config as it is and instead configuring your app to be aware it lives at a subdirectory, if you can.

I know this is an old question, but it was the top google hit for me when I was trying to solve the same issue!

Answered by user2428107 on October 13, 2020

Try the following

location /route/ {
        proxy_pass  http://127.0.0.1:9000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Answered by ALex_hha on October 13, 2020

Add your own answers!

Related Questions

Enabling jumbo frame on tg3

0  Asked on December 15, 2020 by gli-illuminati

     

Postfix does not retry after 451 error / greylisting

2  Asked on December 14, 2020 by assylias

       

OpenVPN over dynamic IPv6

1  Asked on December 14, 2020 by hellstorm

   

autossh does not kill ssh when link down

1  Asked on December 13, 2020

   

Is it safe to write batch files with Unix line endings?

4  Asked on December 13, 2020 by cmcginty

   

On demand new IPv6 using privacy extension?

1  Asked on December 12, 2020 by wannabecoder

         

nginx, gitlab, subdomain

1  Asked on December 10, 2020 by emidee

       

Move Access 2016 Database to Azure?

0  Asked on December 9, 2020 by user601108

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir