如何使用 nginx proxy_pass 保留请求 url
我尝试使用 Thin 应用服务器,但遇到了一个问题。
当 nginx 代理使用 proxy_pass http://my_app_upstream 向 Thin(或 Unicorn)发出请求时;
应用程序接收 nginx 发送的修改后的 URL (http://my_app_upstream
)。
我想要的是传递原始 URL 和来自客户端的原始请求而不进行任何修改,因为应用程序严重依赖它。
nginx' doc 说:
如果需要传输URI 未处理的表单 then 指令 proxy_pass 应该在没有 URI 的情况下使用 部分。
但我不明白如何准确配置它,因为相关示例实际上使用 URI:
location /some/path/ {
proxy_pass http://127.0.0.1;
}
所以您能帮我弄清楚如何保留来自客户端的原始请求 URL 吗?
I was trying to use Thin app server and had one issue.
When nginx proxies the request to Thin (or Unicorn) using proxy_pass http://my_app_upstream;
the application receives the modified URL sent by nginx (http://my_app_upstream
).
What I want is to pass the original URL and the original request from client with no modification as the app relies heavily on it.
The nginx' doc says:
If it is necessary to transmit URI in
the unprocessed form then directive
proxy_pass should be used without URI
part.
But I don't understand how exactly to configure that as the related sample is actually using URI:
location /some/path/ {
proxy_pass http://127.0.0.1;
}
So could you please help me figuring out how to preserve the original request URL from the client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
只是
proxy_set_header 主机 $host
我的箱子错过了港口。解决方法:
然后在 proxy.conf 中
根据 nginx 1.8.x 的 @iwein 注释进行更新(参见 https://stackoverflow.com/ a/57350306/548473):
iso
proxy_set_header 主机 $host:$server_port;
使用proxy_set_header 主机 $http_host
Just
proxy_set_header Host $host
miss port for my case. Solved by:
and then in the proxy.conf
Update according @iwein comment for nginx 1.8.x (see https://stackoverflow.com/a/57350306/548473):
iso
proxy_set_header Host $host:$server_port;
useproxy_set_header Host $http_host
nginx 还提供了 $http_host 变量,它将为您传递端口。
它是主机和端口的串联。
所以你只需要这样做:
nginx also provides the $http_host variable which will pass the port for you.
its a concatenation of host and port.
So u just need to do:
如果某些内容修改了您尝试提供服务的位置,例如
try_files
,这会保留对后端的请求:In case something modifies the location that you're trying to serve, e.g.
try_files
, this preserves the request for the back-end:我在上面的评论中发现了这一点,但我认为这确实应该是一个答案。
I found this above in the comments but I think it really should be an answer.
要完美转发而不需要截断请求的
absoluteURI
和标头中的Host
:可在此处找到:https://opensysnotes.wordpress.com/2016/11/17/nginx-proxy_pass-with-absolute-url/
To perfectly forward without chopping the
absoluteURI
of the request and theHost
in the header:Found here: https://opensysnotes.wordpress.com/2016/11/17/nginx-proxy_pass-with-absolute-url/
在我的场景中,我通过 nginx vhost 配置中的以下代码进行此操作
$http_host 将在标头中设置与请求相同的 URL
In my scenario i have make this via below code in nginx vhost configuration
$http_host will set URL in Header same as requested
对于我的身份验证服务器...这有效。我喜欢为 /auth 提供选项以实现我自己的人性化可读性...或者我也通过端口/上游为机器到机器配置它。
。
在conf的开头
在我的443服务器块内
在conf的底部
for my auth server... this works. i like to have options for /auth for my own humanized readability... or also i have it configured by port/upstream for machine to machine.
.
at the beginning of conf
Inside my 443 server block
At the bottom of conf
我认为
proxy_set_header
指令可以提供帮助:I think the
proxy_set_header
directive could help: