简体   繁体   中英

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 .

So far I have tried various combinations of configuration without any luck. Most of the time it gives me 404 error.

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. 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.

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 .

The location value and the alias value should either both end with / or neither end with / to achieve the correct substitution.

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. Avoid using alias and try_file due to this issue .

Use the second if block to avoid passing uncontrolled requests to PHP .

See this caution on the use of if .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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