54

I have 2 servers,

  1. with IP xx.xx.xx.xx, situated in Germany ... (running frontend: nginx(static content), backend: Apache2)

  2. with IP yy.yy.yy.yy, situated in Italy...

All requests at the moment is sending to server with IP xx.xx.xx.xx, How can I proxy all traffic from xx.xx.xx.xx to yy.yy.yy.yy using nginx ...

 request proxy, request Internet -> xx.xx.xx.xx(nginx) -> yy.yy.yy.yy(nginx, Apache) <- <- response proxy, response 
3
  • Did you try read documentation? proxy_pass _http://yy.yy.yy.yy Commented Mar 25, 2011 at 12:29
  • sure! but how to read the X-Real-IP on remote side(ip: yy.yy.yy.yy) ?? Commented Mar 25, 2011 at 18:25
  • All of the solutions did not work for me. I went with the (closed source) ngrok. Commented Feb 7, 2018 at 14:58

2 Answers 2

126

For others. Answer for subject is configure Nginx like:

server { listen 80; server_name mydomain.example; location / { access_log off; proxy_pass http://mydomain.example:8080; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 
Sign up to request clarification or add additional context in comments.

5 Comments

Does location / match all paths? or only the root path '\'?
@pomo according guidlines here nginx.org/en/docs/http/ngx_http_core_module.html#location it will match all nested queries such as site.com/index.html but not for root query aka site.com/
@VladimirShmidt it matches root location too.
This works great! Is pretty much the same as when setting up a private node server and using nginx as the public server.
What about Authentication in upstream proxy for example server : mydomain.example:8080 user : xxxx pass : yyyy ?
0

You can use upsteream like:

upstream xx.xx.xx.xx:8080{ #ip_hash; server xx.xx.xx.xx:8080 max_fails=2 fail_timeout=2s; server yy.yy.yy.yy:8181 max_fails=2 fail_timeout=2s; } 

then you can use the cookie or header to set the request like:

location /app { if ($cookie_proxy_override = "proxy-target-A") { rewrite . http://xx.xx.xx.xx:8080/app; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; break; } if ($cookie_proxy_override = "proxy-target-B") { rewrite . http://yy.yy.yy.yy:8181/webreg; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; break; } proxy_pass http://xx.xx.xx.xx:8080/webreg; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } 

1 Comment

This is wrong syntax and it won't work. You should set any name to upstream, like upstream myservers { ... } and then you can use this name in proxy_pass http://myservers;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.