假设 /www 下有多个版本的项目, 如:v1d0, v1d2, v1d3。通过配置nginx根url中的版本号将请求重定向到不同项目。
请求 demo.com/v1d0/my-path 将请求定向到v1d0的项目下。
例如nginx配置如下:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /www;
server_name demo.com;
if (!-e $request_filename) {
rewrite ^/([a-zA-Z0-9]+)/(.*)$ /$1/public/index.php/$2 break;
}
location ~ \.php {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
注意的点:
1. "root /www" 这一行末尾不带“/“ 那么规则 "rewrite ^/([a-zA-Z0-9]+)/(.*)$ /$1/public/index.php/$2 break" 配置开头就要 "^/" 意思是匹配以 / 开头
2. 配置文件中不能有中文空格
3. "location ~ \.php {" 与"location ~ \.php${" 是有区别的!\$ 表示匹配以什么结尾,
经过rewrite后的url 如果不以.php 结尾则无法匹配到,会出现404
4. server_name 要写对