nginx 将所有流量代理到远程 nginx

发布于 2024-10-26 23:41:50 字数 558 浏览 3 评论 0原文

我有 2 台服务器,

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

  2. IP yy.yy.yy.yy,位于意大利...< /p>

目前所有请求都发送到 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七禾 2024-11-02 23:41:50

对于其他人。主题的答案是配置 Nginx,如下所示:

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

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;
    }
}
那小子欠揍 2024-11-02 23:41:50

您可以使用 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 或 header 来设置请求,例如:

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

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文