繁体   English   中英

如何为Centos中的所有对象设置通用的一个Nginx服务器块(虚拟主机)

[英]How To Set Up common one Nginx Server Blocks (Virtual Hosts) for all peojects in Centos

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

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

    }

那么,是否还有其他方法可以为所有站点文件夹创建一个公共块,而无需为新站点添加新的服务器块?

提前致谢。

有多种实现方法。 如果使用多个域名,则可以在server_name使用正则表达式来创建命名捕获(有关更多信息,请参阅本文档 )。 您可以在location指令中使用正则表达式来捕获项目文件夹的值(有关更多信息,请参见本文档 )。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM