我有一个带有Nginx服务器的Centos,并且在wamp中存在多个站点文件夹。
但是对于每个项目,我需要在/etc/ Nginx /conf.d/websites.conf文件下编写单独的nginx服务器块。所以每当我创建一个新项目之后,我必须在Nginx的websites.conf文件下添加以下代码行。
location /project-folder {
root path;
index index.php index.html index.htm;
rewrite ^/project-folder/(.*)$ /project-folder/app/webroot/$1 break;
try_files $uri $uri/ /project-folder/app/webroot/index.php?q=$uri&$args;
location ~ .*\.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:xxxx;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* /project-folder/(.*)\.(css|js|ico|gif|png|jpg|jpeg)$ {
root path/project-folder/app/webroot/;
try_files /$1.$2 =404;
}
}那么,有没有其他方法可以为所有站点文件夹创建一个公共块,而不需要为新站点添加新的服务器块?
提前谢谢。
发布于 2017-05-25 20:52:25
有多种方法可以实现这一点。如果您使用多个域名,则可以在server_name中使用正则表达式来创建命名捕获(有关详细信息,请参阅this document )。您可以在location指令中使用正则表达式来捕获项目文件夹的值(有关更多信息,请参阅this document )。
此配置的主要功能是在项目名称和URI的其余部分之间插入文本/app/webroot。挑战是在不创建重定向循环的情况下做到这一点。
我已经测试了下面的示例,它的工作方式是将rewrite语句的一个通用版本放入server块中,并捕获项目名称以供稍后在其中一条try_files语句中使用:
server {
...
root /path;
index index.php index.html index.htm;
rewrite ^(?<project>/[^/]+)(/.*)$ $1/app/webroot$2;
location / {
try_files $uri $uri/ $project/index.php?q=$uri&$args;
}
location ~ .*\.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:xxxx;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
location ~* \.(css|js|ico|gif|png|jpg|jpeg)$ {
try_files $uri =404;
}
}https://stackoverflow.com/questions/44177230
复制相似问题