Search code examples
phpcodeigniternginxurl-rewritingvagrant

Nginx with angular and codeigniter applications in subfolders


I'm trying to make a website with codeigniter in the backend and angularjs in the frontend. For the backend I'm using nginx as a server. Currently I have problems with proper configuration of the nginx. What I want to achieve is to have codeigniter and angularjs applications in separate folders. And What I want is to access as follows

  • codeigniter from my_website.com/api
  • angular from my_website.com

And in terms of folder I want to keep them in:

  • codeigniter: /var/www/html/api
  • angular: /var/www/html/angular

So far I've manage to make it work from the same folder. So now I have codeigniter in /var/www/html and angular folder is in the same directory. And that way I can access codeigniter from my_website.com/ and angular from my_website.com/app which is not what I want.

I was trying multiple different setups for nginx /etc/nginx/sites-available/default file, but every single time with some problems. Farthest I've got was to have those in separate folders but codeigniter was working only for default_controller and I couldn't access any other files. That's the current working config.

server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/html;

    server_name localhost;
    index index.php index.htm index.html;

    location /app/ {
        root /var/www/html/angular; 
        try_files $uri/ $uri index.html =404;
    }

    location / {
        try_files $uri $uri/ /index.php;
        #index index.html;
    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
        include fastcgi_params;
    }
}

And here is simple config where I just try to run codeigniter from demo sub-folder,

server {
        listen       80;
        server_name  localhost;
        root   /var/www/html;
        autoindex on;
        index index.php;

        location / {

            #try_files $uri $uri/ /index.php;
                try_files $uri $uri/ /index.php$request_uri;
            location = /demo/index.php {
                try_files $uri $uri/ /index.php$request_uri;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

                fastcgi_param  SCRIPT_FILENAME /var/www/html/demo$fastcgi_script_name;
                include        fastcgi_params;
            }
        }

        location ~ \.php$ {
            return 444;
        }


}

And here my_website.com/demo/index.php works fine but my_website.com/demo/index.php/welcome shows 500 Internal Server Error

I hope I make myself clear. I tried so many different configs found online, but I always had some troubles with that. Can any of you propose some simple solution for that problem?

Thanks, Mateusz


Solution

  • I've finally managed to make it work the way I want. I've used this solution link to make it work with codeigniter in sub-directory which was the main problem. Here is my config:

    server {
        listen       80;
        server_name  localhost;
        root   /var/www/html;
        autoindex on;
        index index.html;
    
        location / {
            root /var/www/html/angular; 
            try_files $uri/ $uri index.html =404;
        }
    
        location /api/ {
                alias  /var/www/html/api/;
                try_files $uri $uri/ /api/index.php;
    
                location ~ \.php$ {
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_index   index.php;
                    fastcgi_pass    unix:/var/run/php/php7.0-fpm.sock;
                    include /etc/nginx/fastcgi_params;
                    fastcgi_param   SCRIPT_FILENAME $request_filename;
            }
        }
    
        location ~ \.php$ {
            return 444;
        }
    }
    

    Thanks to @FrédéricHenri for suggesting using the log file under /var/log/nginx/*.log, that helped with tracking the way nginx is redirecting requests. Also what I've encounter was that web-browser was caching the website(I think that is what was happening) so that was giving me false results while I was making some changes to the config file.