34

这个想法是接受传入的请求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;
}

诀窍是在指向同一个 Nginx 实例时,对客户端透明地更改此 URL 。abc.example.comexample.com

换句话说,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 / { 
    # ...
  }
}
4

1 回答 1

43
# 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;
  }
}
于 2013-01-24T02:09:15.467 回答