如何使用 Nginx 将请求重定向到不同的 URL,而不更改浏览器中子域的 URL

问题描述 投票:0回答:1

我正在尝试重写从子域到子文件夹的请求,因此 URL 在浏览器中保持为

subdomain.example.com
,但显示了
example.com/subdomain
的内容。

我尝试使用重写指令,但这会重定向用户而不是更改浏览器中的 URL。如何在不更改浏览器中的 URL 的情况下执行此操作?

这是我尝试过的代码:

server {
    server_name subdomain.example.com;

    location / {
        rewrite ^/(.*)$ https://example.com/subdomain/$1 permanent;
    }
}
server {
    listen 80;
    server_name subdomain.example.com;

    location / {
        rewrite ^ /subdomain$request_uri last;
    }
}

有人可以帮我吗?

http nginx redirect url-rewriting
1个回答
0
投票

您可以使用以下方法实现此目的 Nginx 中的

proxy_pass
指令。以下是修改配置的方法:

nginxserver {
    listen 80;
    server_name subdomain.example.com;

    location / {
        proxy_pass https://example.com/subdomain;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.