Home > Blockchain >  Cannot access CodeIgniter controller via nginx
Cannot access CodeIgniter controller via nginx

Time:09-16

I'm in process of migrating my project from apache to nginx, unfortunately I am facing a problem during the process. I tried to find similar problem here, but all of them not working in my case.

I tried to send POST request to one of my controller in CI, in apache this works well without much tinkering. This is the request I'm sending.

curl --request POST \
  --url http://SERVER_IP/directory/index.php/NameOfController \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data token=xxx \
  --data user_id=1234 \
  --data trx_number=123456789 \
  --data trx_value=1000000

If the request is successful, I should have gotten a json data— instead I got this html response.

enter image description here

This is my /etc/nginx/conf.d/default.conf file.

server {
    listen       80;
    server_name  localhost;
    root /var/www;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        index index.html index.htm index.php;
        try_files $uri $uri/ /index.php;
        location ~ /.ssh {
            deny all;
            return 403;
        }
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root /var/www;
    }
    location ~ [^/]\.php(?:$|/) {
        fastcgi_pass   unix:/var/run/php/php7.4-fpm.sock;
        include        fastcgi_params;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

This is the path of controllers I'm trying to access.

/var/www/directory/application/controllers/NameOfController.php

Please tell me if there's any configuration you'd like to see, I'll be happy to include them here. I really in a bind now.

CodePudding user response:

In the following URL:

http://SERVER_IP/directory/index.php/NameOfController

the /NameOfController part is called the PATH_INFO. It is used by CodeIgniter to know which controller to call.

The issue here is that your nginx configuration does not define the PATH_INFO parameter. As a consequence, CodeIgniter will call the default controller (welcome).

To fix it, you need to use the fastcgi_split_path_info directive and define the PATH_INFO parameter like this:

location ~ [^/]\.php(?:$|/) {
        fastcgi_split_path_info ^(. \.php)(.*)$;
        fastcgi_pass   unix:/var/run/php/php7.4-fpm.sock;
        include        fastcgi_params;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
    }
  • Related