Nginx 无需重定向即可将子域转换为路径组件 [英] Nginx convert subdomain to path component without redirect

查看:23
本文介绍了Nginx 无需重定向即可将子域转换为路径组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

诀窍是当 abc.example.comexample.com 指向相同时,将这个 URL 更改透明给客户端Nginx 实例.

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.

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

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

起点配置

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

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

推荐答案

# 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 无需重定向即可将子域转换为路径组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆