Nginx将子域转换为无redirect的path组件

这个想法是将传入的请求传递给http://abc.example.com/...并将其重写http://example.com/abc/...

这很容易做301/302redirect:

 # rewrite via 301 Moved Permanently server { listen 80; server_name abc.example.com; rewrite ^ $scheme://example.com/abc$request_uri permanent; } 

诀窍是当abc.example.comexample.com指向相同的Nginx实例时,将此URL更改为透明地显示给客户端。

换句话说,当请求abc.example.com/...并且没有另外的客户往返时,Nginx是否可以提供来自example.com/abc/...的内容?

起点configuration

用301完成任务的Nginxconfiguration:

 # abc.example.com server { listen 80; server_name abc.example.com; rewrite ^ $scheme://example.com/abc$request_uri permanent; } # example.com server { listen 80; server_name example.com; location / { # ... } } 

 # abc.example.com server { listen 80; server_name abc.example.com; location / { proxy_pass http://127.0.0.1/abc$request_uri; proxy_set_header Host example.com; } }