[英]Nginx does redirect, not proxy
I want to set up Nginx as a reverse proxy for a https service, because we have a special usecase where we need to "un-https" a connection:我想将 Nginx 设置为 https 服务的反向代理,因为我们有一个特殊的用例,我们需要“取消 https”连接:
http://nginx_server:8080/myserver ==> https://mysecureservice
But what happens is that the actual https service isn't proxied.但是实际的 https 服务没有被代理。 Nginx does redirect me to the actual service, so the URL in the browser changes.
Nginx 确实将我重定向到实际服务,因此浏览器中的 URL 会发生变化。 I want to interact with Nginx as it was the actual service, just without https.
我想与 Nginx 交互,因为它是实际的服务,只是没有 https。
This is what I have:这就是我所拥有的:
server {
listen 0.0.0.0:8080 default_server;
location /myserver {
proxy_pass https://myserver/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
}
}
You have to use the proxy_redirect
to handle the redirection.您必须使用
proxy_redirect
来处理重定向。
Sets the text that should be changed in the “Location” and “Refresh” header fields of a
proxied server response. Suppose a proxied server returned the header field
“Location:https://myserver/uri/”. The directive
will rewrite this string to “Location: http://nginx_server:8080/uri/”.
Example:例子:
proxy_redirect https://myserver/ http://nginx_server:8080/;
Source: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect来源: http : //nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
You can setup nginx like this if you do not want the server to do redirects:如果您不希望服务器进行重定向,您可以像这样设置 nginx:
server
{
listen 80;
server_name YOUR.OWN.DOMAIN.URL;
location / {
proxy_pass http://THE.SITE.URL.YOU.WANT.TO.DELEGAGE/;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
For me, this config was sufficient:对我来说,这个配置就足够了:
events {
}
http {
server {
location / {
resolver 8.8.8.8;
proxy_pass https://www.example.com$request_uri;
}
}
}
(Note that the resolver
directive has nothing to do with the problem in the OP, I just needed it to be able to proxy an external domain such as example.com
) (请注意,
resolver
指令与 OP 中的问题无关,我只需要它能够代理外部域,例如example.com
)
The problem for me was just that I was missing the www.
对我来说,问题只是我错过了
www.
in www.example.com
.在
www.example.com
。 In the Firefox developer's console, I could see the GET request to localhost
coming back with a 301, and so I thought that NGINX was issuing 301s instead of just mirroring example.com
.在 Firefox 开发人员的控制台中,我可以看到对
localhost
的 GET 请求以 301 返回,因此我认为 NGINX 正在发出 301 而不只是镜像example.com
。 Not so: in fact the problem was that example.com
was returning 301s to redirect to www.example.com
, NGINX was dutifully mirroring those 301s, and then Firefox "changed the URL" (followed the redirect) straight from localhost
to www.example.com
.不是这样:实际上问题是
example.com
返回 301s 以重定向到www.example.com
,NGINX 尽职尽责地镜像这些 301s,然后 Firefox 直接“更改了 URL”(跟随重定向)从localhost
到www.example.com
。
I was having a similar issue.我遇到了类似的问题。 In my case, I was able to resolve the issue by added a trailing slash to the
proxy_pass
URL:就我而言,我能够通过向
proxy_pass
URL 添加尾部斜杠来解决该问题:
server {
location / {
proxy_pass http://example.com/path/to/some/folder;
}
}
server {
location / {
# added trailing slash
proxy_pass http://example.com/path/to/some/folder/;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.