Nginx将子域转换为路径组件而不重定向

时间:2013-01-24 00:33:11

标签: nginx rewrite subdomain roundtrip

我们的想法是将传入的请求发送到http://abc.example.com/...并将其重写为http://example.com/abc/...

使用301/302重定向很容易:

# 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更改透明地发送到客户端。

换句话说,Nginx可以在example.com/abc/...请求abc.example.com/...时提供内容,在没有其他客户往返的情况下提供吗?

起点配置

使用301完成任务的Nginx配置:

# 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 / { 
    # ...
  }
}

1 个答案:

答案 0 :(得分:35)

# 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;
  }
}