nginx代理远程nginx的所有流量

时间:2011-03-25 12:19:48

标签: proxy nginx traffic

我有2台服务器,

  1. IP xx.xx.xx.xx,位于德国...(运行前端:nginx(静态内容),后端:Apache2)

  2. 位于意大利的IP yy.yy.yy.yy ......

  3. 目前所有请求都是通过IP xx.xx.xx.xx发送到服务器, 如何使用nginx代理从xx.xx.xx.xx到yy.yy.yy.yy的所有流量...

              request                           proxy, request
    Internet     ->       xx.xx.xx.xx(nginx)         ->             yy.yy.yy.yy(nginx, Apache)
                 <-                                  <-
              response                          proxy, response
    

    谢谢...

2 个答案:

答案 0 :(得分:90)

对于其他人。主题的答案是配置nginx,如:

server {
  listen 80;
  server_name mydomain.com;
    location / {
      access_log off;
      proxy_pass http://mydomain.com: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;
    }
}

答案 1 :(得分:1)

您可以使用upsteream:

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;
}

然后您可以使用cookie或标头来设置请求,如:

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;
    }