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

查看:109
本文介绍了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指向同一Nginx实例时,向客户端进行透明的URL更改.

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.

换句话说,Nginx是否可以在请求abc.example.com/...没有其他客户端往返的情况下提供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天全站免登陆