51

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
Nakilon
  • 34,866
  • 14
  • 107
  • 142
user676674
  • 519
  • 1
  • 5
  • 3

2 Answers2

122

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;
    }
}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Vladimir Shmidt
  • 2,651
  • 1
  • 19
  • 21
  • 10
    Does `location /` match all paths? or only the root path '\'? – pomo Sep 08 '16 at 19:30
  • 2
    @pomo according guidlines here http://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/ – Vladimir Shmidt Sep 10 '16 at 14:40
  • @VladimirShmidt it matches root location too. – dikkini Jun 13 '20 at 16:38
  • This works great! Is pretty much the same as when setting up a private node server and using nginx as the public server. – Crysfel Jun 10 '21 at 18:14
  • What about Authentication in upstream proxy for example server : http://mydomain.example:8080 user : xxxx pass : yyyy ? – MrSalesi Aug 27 '23 at 08:45
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;
    }
  • 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;` – Sergey Martynov Jul 23 '19 at 22:29