gpt4 book ai didi

apache - 如何在 Centos 中为所有项目设置通用的一个 Nginx 服务器 block (虚拟主机)

转载 作者:太空宇宙 更新时间:2023-11-03 17:17:02 24 4
gpt4 key购买 nike

我有一个带有 Nginx 服务器的 Centos,并且 wamp 中存在多个站点文件夹。

但对于每个项目,我都需要在/etc/nginx/conf.d/websites.conf 文件下编写单独的 Nginx 服务器 block 。因此,每当我创建一个新项目时,我必须在 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;
}

}

那么有没有其他方法可以为所有站点文件夹创建一个公共(public) block ,并且不需要为新站点添加新的服务器 block ?

提前致谢。

最佳答案

有多种方法可以实现这一点。如果您使用多个域名,则可以在 server_name 中使用正则表达式来创建命名捕获(有关更多信息,请参见 this document)。您可以在 location 指令中使用正则表达式来捕获 project-folder 的值(有关更多信息,请参见 this document)。

此配置的主要功能是在项目名称和 URI 的其余部分之间插入文本 /app/webroot。挑战在于如何在不创建重定向循环的情况下做到这一点。

我已经测试了以下示例,该示例通过将通用版本的 rewrite 语句放入 server block 并捕获项目名称以供稍后在其中使用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;
}
}

关于apache - 如何在 Centos 中为所有项目设置通用的一个 Nginx 服务器 block (虚拟主机),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44177230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com