nginx - Nginx将子域转换为路径组件而无需重定向

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

最佳答案

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

关于nginx - Nginx将子域转换为路径组件而无需重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14491944/

相关文章:

configuration - nginx 到 localhost 的配置是否正确?

python - Flask - 使用 Gunicorn、Nginx 和 Supervisor 部署,Supervisor 错误日志

php - 子目录中的 CodeIgniter 站点,htaccess 文件可能会干扰主目录中的 htaccess 文件?

javascript - 如何使 Hapi 插件仅适用于特定域或子域?

nginx - 使用 namecheap 在 nginx 上配置子域

python - 在 docker 中通过 nginx 和 gunicorn 服务 flask

http - HTTP 连接和请求之间有什么关系?

php - .htaccess 重写多个参数的规则

Nginx 重写语法

iis - 如何为子域正确设置hostHeader?