NGINX:使用http重写而不重定向



Stackoverflowers, 我需要创建一个指向 http://example.com 的反向代理,但不是将其重定向到站点,而是需要重写它,而无需更改 URL。我的nginx反向代理的IP是 http://10.31.0.147/。 不幸的是,我当前的配置不起作用:

server {
listen 80 default_server;
listen [::]:80 default_server;
location / {
rewrite ^/$ http://example.com break;
proxy_redirect off;
}
}

如果我从重写命令中删除 http://,我会收到错误"404 未找到"如果我保持原样,我将被重定向并且 URL 更改为 www.example.com。有没有办法解决这个问题而不改变 http://10.31.0.147。在NGINX文档中,我读到每当我使用"http://"时,页面都会被重定向。

我没有使用重写,而是使用了代理传递

location / {
proxy_pass   https://example.com;
proxy_set_header X-Real-IP example.com;
proxy_set_header X-Forwarded-For example.com;
proxy_set_header Host example.com;
}

很简单:

location / {
proxy_pass https://$server_name$request_uri;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Port $server_port;
}

最新更新