Johna
Johna

Reputation: 1894

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.

Upvotes: 3

Views: 528

Answers (2)

Alok
Alok

Reputation: 1

I have 2 projects in /var/www directory

  • /var/www
    -- project1
    -- project2
server {
        listen 80;

        server_name www.your_domain.com;

        root /var/www/project1/public;
        index index.php;

        
        location ^~ /api {
            rewrite ^/api/?(.*)$ /project2/public/$1 last;
        }

        location ^~ /project2/public {
            root /var/www/;
            try_files $uri $uri/ /project2/public/index.php$args;

            location ~ \.php$ {
                    try_files $uri = 404;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_pass 127.0.0.1:9000;
                    fastcgi_keep_conn on;
                    fastcgi_index index.php;
                    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                    include fastcgi_params;
            }
        }

        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                try_files $uri = 404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_keep_conn on;
                fastcgi_index index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

        # Return a 404 for files and directories starting with a period except well-known.
        location ~ /\.(?!well-known) {
                return 404;
        }
}

Upvotes: 0

Richard Smith
Richard Smith

Reputation: 49702

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.

Upvotes: 2

Related Questions