简体   繁体   English

如何在Nginx、FastCGI中的两个目录下配置两个Codeigniter应用

[英]How to configure two Codeigniter applications in two directories in Nginx, FastCGI

I have a specific requirement to setup two CodeIgniter based application in same Nginx server pointing to domain.com/ and domain.com/site2 .我有一个特定要求,即在指向domain.com/domain.com/site2同一 Nginx 服务器中设置两个基于 CodeIgniter 的应用程序。

So far I have tried various combinations of configuration without any luck.到目前为止,我已经尝试了各种配置组合,但没有任何运气。 Most of the time it gives me 404 error.大多数时候它给了我 404 错误。

Following is my latest configuration file.以下是我最新的配置文件。

server {
        listen       80;
        server_name  domain.com www.domain.com;
        root   /var/www/domain.com/;

        location / {
            root   /var/www/domain.com/site1/public/;
            index index.html index.php
            access_log /var/www/domain.com/site1/logs/access.log;
            error_log /var/www/domain.com/site1/logs/errors.log;
            location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_param  SCRIPT_FILENAME /var/www/domain.com/main/public$fastcgi_script_name;
                fastcgi_param PATH_INFO       $fastcgi_path_info;
            }
        }

        location /site2{
            root /var/www/domain.com/site2/public/;
            index index.html index.php;
            access_log /var/www/domain.com/site2/logs/access.log;
            error_log /var/www/domain.com/site2/logs/errors.log;
            location ~ \.php$ {
                include fastcgi_params;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_param  SCRIPT_FILENAME
                $document_root$fastcgi_script_name;
            }
        }
    }

I have used alias in place of root without the domain part.我使用alias代替了没有域部分的root Still no luck.仍然没有运气。

However when I use alias and try_files $uri $uri/ /site2/site2/public/index.php?$query_string inside location block, it loads the website without assets like css/js.但是,当我在 location 块中使用aliastry_files $uri $uri/ /site2/site2/public/index.php?$query_string ,它会加载没有像 css/js 这样的资产的网站。

Could you please show me what is wrong in my configuration.你能告诉我我的配置有什么问题吗?

Using alias with fastcgi requires you to use $request_filename to compute the correct value for SCRIPT_FILENAME .fastcgi使用alias需要您使用$request_filename来计算SCRIPT_FILENAME的正确值。

The location value and the alias value should either both end with / or neither end with / to achieve the correct substitution. location值和alias值都应以/结尾或都不以/结尾,以实现正确的替换。

location /site2 {
    alias /var/www/domain.com/site2/public;
    index index.html index.php;
    access_log /var/www/domain.com/site2/logs/access.log;
    error_log /var/www/domain.com/site2/logs/errors.log;

    if (!-e $request_filename) { rewrite ^ /site2/index.php last; }

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }

        include fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $request_filename;
    }
}

Use the first if block to simulate the try_files statement in your question.使用第一个if块来模拟问题中的try_files语句。 Avoid using alias and try_file due to this issue .由于这个问题,避免使用aliastry_file

Use the second if block to avoid passing uncontrolled requests to PHP .使用第二个if块来避免将不受控制的请求传递给 PHP

See this caution on the use of if .请参阅有关使用if 注意事项

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

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