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

kmbjn2e3  于 2023-10-17  发布在  Nginx
关注(0)|答案(1)|浏览(193)

我正在尝试将请求从子域重写到子文件夹,因此URL在浏览器中保持为subdomain.example.com,但显示example.com/subdomain的内容。
我尝试过使用rewrite指令,但这会重定向用户,而不是更改浏览器中的URL。如何在不更改浏览器中的URL的情况下执行此操作?
下面是我尝试过的代码:

  1. server {
  2. server_name subdomain.example.com;
  3. location / {
  4. rewrite ^/(.*)$ https://example.com/subdomain/$1 permanent;
  5. }
  6. }
  1. server {
  2. listen 80;
  3. server_name subdomain.example.com;
  4. location / {
  5. rewrite ^ /subdomain$request_uri last;
  6. }
  7. }

有人能帮我吗?

7ajki6be

7ajki6be1#

你可以在Nginx中使用proxy_pass指令来实现这一点。以下是修改配置的方法:

  1. nginxserver {
  2. listen 80;
  3. server_name subdomain.example.com;
  4. location / {
  5. proxy_pass https://example.com/subdomain;
  6. proxy_set_header Host $host;
  7. proxy_set_header X-Real-IP $remote_addr;
  8. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  9. proxy_set_header X-Forwarded-Proto $scheme;
  10. }
  11. }

相关问题