需求
在阿里云买了个虚拟主机,正常虚拟主机只能部署一个站点,所以需要一些技术手段实现多站点部署和绑定多域名。
查了网上一些相关资料,实现方式主要有两种:
- 当访问地址时,使用php(或其他语言)进行host判断
- 直接在nginx上进行判断,然后rewrite到指定文件
方式1实用性不强,最后使用的是在nginx上进行判断跳转。
项目环境
- 部署环境:nginx
- 后台语言:php
- 应用框架:thinkphp5
实现
重定向调试比较麻烦,建议开启rewrite日志:修改日志级别 error_log D:/domain.com.error.log notice;
和 配置开启 rewrite_log on;
子域名访问
访问子域名为
a.domain.com
,部署子目录为a
location /a/public/ {
if (!-e $request_filename){
rewrite ^/a/public/(.*)$ /a/public/index.php?s=$1 last;
}
}
location / {
if ($host = 'a.domain.com') {
rewrite ^(.*)$ /a/public/$1 last;
}
}
子目录访问
访问路径为
domain.com/test
,部署子目录为b
location /test/ {
rewrite ^/test/(.*)$ /b/public/$1 last;
}
location /b/public/ {
if (!-e $request_filename){
rewrite ^/b/public/(.*)$ /b/public/index.php?s=$1 last;
}
}