92

我试图使用应用服务器并且遇到了一个问题。

当 nginx使用应用程序将请求代理到 Thin(或 Unicorn)proxy_pass http://my_app_upstream;时,会收到 nginx ( http://my_app_upstream) 发送的修改后的 URL。

我想要的是传递原始 URL 和来自客户端的原始请求而无需修改,因为应用程序严重依赖它。

nginx 的文档说:

如果需要以未处理的形式传输 URI,则应使用指令 proxy_pass 而不使用 URI 部分。

但我不明白如何准确配置它,因为相关示例实际上是使用 URI:

location  /some/path/ {
  proxy_pass   http://127.0.0.1;
}

那么您能否帮我弄清楚如何保留来自客户端的原始请求 URL

4

8 回答 8

154

我认为该proxy_set_header指令可能会有所帮助:

location / {
    proxy_pass http://my_app_upstream;
    proxy_set_header Host $host;
    # ...
}
于 2011-04-29T16:25:44.763 回答
11

就我而言,只是 proxy_set_header Host $host 错过了端口。解决者:



    location / {
     proxy_pass http://BACKENDIP/;
     include /etc/nginx/proxy.conf;
    }

然后在 proxy.conf



    proxy_redirect off;
    proxy_set_header Host $host:$server_port;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

于 2013-06-12T19:37:49.820 回答
7

如果某些东西修改了您尝试服务的位置,例如try_files,这会保留对后端的请求:

location / {
  proxy_pass http://127.0.0.1:8080$request_uri;
}
于 2018-10-03T04:27:02.240 回答
7

注意其他人发现这一点:使 nginx 不操纵 URL 的解决方案的核心是删除 Copy: proxy_pass 指令末尾的斜杠。http://my_app_upstreamhttp://my_app_upstream/ – 雨果约瑟夫森

我在评论中发现了这一点,但我认为这确实应该是一个答案。

于 2019-05-16T19:28:00.817 回答
5

nginx 还提供了 $http_host 变量,它将为您传递端口。它是主机和端口的串联。

所以你只需要这样做:

proxy_set_header Host $http_host;
于 2018-01-18T04:49:20.640 回答
3

absoluteURI在不切断请求和标头中的情况下完美转发Host

server {
    listen 35005;

    location / {
        rewrite            ^(.*)$   "://$http_host$uri$is_args$args";
        rewrite            ^(.*)$   "http$uri$is_args$args" break;
        proxy_set_header   Host     $host;

        proxy_pass         https://deploy.org.local:35005;
    }
}

在这里找到:https ://opensysnotes.wordpress.com/2016/11/17/nginx-proxy_pass-with-absolute-url/

于 2017-07-13T07:58:42.387 回答
2

在我的场景中,我通过 nginx vhost 配置中的以下代码进行了此操作

server {
server_name dashboards.etilize.com;

location / {
    proxy_pass http://demo.etilize.com/dashboards/;
    proxy_set_header Host $http_host;
}}

$http_host 将在 Header 中设置与请求相同的 URL

于 2018-05-03T21:39:53.050 回答
-1

对于我的身份验证服务器...这有效。我喜欢为我自己的人性化可读性提供 /auth 选项......或者我也通过端口/上游配置它以供机器到机器。

.

在conf开始时

####################################################
upstream auth {
    server 127.0.0.1:9011 weight=1 fail_timeout=300s;
    keepalive 16;
  }

在我的 443 服务器块内

          if (-d $request_filename) {
          rewrite [^/]$ $scheme://$http_host$uri/ permanent;
      }

  location /auth {
          proxy_pass http://$http_host:9011;
          proxy_set_header Origin           http://$host;
          proxy_set_header Host             $http_host:9011;
          proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
          proxy_set_header Upgrade          $http_upgrade;
          proxy_set_header Connection       $http_connection;
          proxy_http_version 1.1;
      }

在conf的底部

#####################################################################
#                                                                   #
#     Proxies for all the Other servers on other ports upstream     #
#                                                                   #
#####################################################################


#######################
#        Fusion       #
#######################

server {
    listen 9001 ssl;

#############  Lock it down  ################

# SSL certificate locations
    ssl_certificate /etc/letsencrypt/live/allineed.app/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/allineed.app/privkey.pem;

# Exclusions

    include snippets/exclusions.conf;

# Security

    include snippets/security.conf;
    include snippets/ssl.conf;

# Fastcgi cache rules

    include snippets/fastcgi-cache.conf;
    include snippets/limits.conf;
    include snippets/nginx-cloudflare.conf;

###########  Location upstream ##############

    location  ~ / {
        proxy_pass http://auth;
        proxy_set_header Origin           http://$host;
        proxy_set_header Host             $host:$server_port;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade          $http_upgrade;
        proxy_set_header Connection       $http_connection;
        proxy_http_version 1.1;
    }
        if (-d $request_filename) {
        rewrite [^/]$ $scheme://$http_host$uri/ permanent;
    }
}
于 2018-11-06T22:33:37.390 回答