Nginx convert subdomain to path component without redirect

别说谁变了你拦得住时间么 提交于 2019-12-17 17:34:16

问题


The idea is to take incoming requests to http://abc.example.com/... and rewrite them to http://example.com/abc/...

That's easy enough to do with a 301/302 redirect:

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

The trick is to do this URL change transparently to the client when abc.example.com and example.com point at the same Nginx instance.

Put differently, can Nginx serve the contents from example.com/abc/... when abc.example.com/... is requested and without another client round trip?

Starting Point Config

Nginx config that accomplishes the task with a 301:

# 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:


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


来源:https://stackoverflow.com/questions/14491944/nginx-convert-subdomain-to-path-component-without-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!