Nginx URL屏蔽到不同的域

时间:2014-12-25 07:11:06

标签: nginx rewrite

关于SO有几个类似的问题,但没有一个是我的,我到目前为止没有运气试图调整他们的答案。

我想将网址http://sub.example.com映射到https://123.12.12.12/path,以便浏览器仍然显示网址http://sub.example.com

我的Nginx配置文件看起来像,

server {
    listen 80;
    server_name sub.example.com;

    location / {
        proxy_pass https://123.12.12.12;
        rewrite ^/$ /path last;
    }
}

此处的路由工作正常,但显示的网址为http://sub.example.com/path。如何仅显示http://sub.example.com

2 个答案:

答案 0 :(得分:23)

server {
    listen 80;
    server_name sub.example.com;

    location / {
        proxy_pass https://123.12.12.12/path;
    }
}

这是怎么回事。如果proxy_pass包含位置部分 - 当前位置将被替换为指定。 http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

但它仅对http请求和http重定向有帮助。如果应用程序使用链接https://123.12.12.12创建html - 它仍然保持不变。在这种情况下,您可以尝试ngx_http_sub_module。

答案 1 :(得分:0)

我确实这样:

server {
    listen 80;
    listen [::]:80;
    listen 443 http2 ssl;
    listen [::]:443 http2 ssl;

    server_name domain1;

    if ($request_method ~* OPTIONS|GET|HEAD) {
        return 301 https://domain2$request_uri;
    }

    location ~* api {
        proxy_pass https://domain2$request_uri;
    }
}

因为后请求会导致重定向时出现405错误。