要保留nginx的URI并使用不同的子域名打开相同页面,可以使用nginx的proxy_pass
指令和正则表达式来实现。
以下是一个示例nginx配置文件的代码示例:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:8080; # 将请求转发给后端服务器
proxy_set_header Host $host; # 将原始主机头传递给后端服务器
}
}
server {
listen 80;
server_name subdomain.example.com;
location / {
proxy_pass http://localhost:8080; # 将请求转发给后端服务器
proxy_set_header Host $host; # 将原始主机头传递给后端服务器
}
}
以上配置中,我们创建了两个server块,分别用于处理主域名和子域名的请求。在每个server块内,我们使用proxy_pass
指令将请求转发给后端服务器,并使用proxy_set_header
指令将原始主机头传递给后端服务器。
这样,无论是通过主域名还是子域名访问网页,都会将请求转发给后端服务器,并保留原始URI的路径。