繁体   English   中英

无法通过 nginx 访问 CodeIgniter controller

[英]Cannot access CodeIgniter controller via nginx

我正在将我的项目从 apache 迁移到 nginx,不幸的是我在此过程中遇到了问题。 我试图在这里找到类似的问题,但在我的情况下它们都不起作用。

我试图将 POST 请求发送到我在 CI 中的 controller 中的一个,在 apache 中,这很好用,无需太多修补。 这是我发送的请求。

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

如果请求成功,我应该得到一个 json 数据——而不是我得到这个 html 响应。

在此处输入图像描述

这是我的/etc/nginx/conf.d/default.conf文件。

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;
    }
}

这是我试图访问的控制器的路径。

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

如果您想查看任何配置,请告诉我,我很乐意将它们包含在此处。 我现在真的很纠结。

在以下 URL 中:

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

/NameOfController部分称为 PATH_INFO。 CodeIgniter 使用它来知道要调用哪个 controller。

这里的问题是您的 nginx 配置未定义PATH_INFO参数。 因此,CodeIgniter 将调用默认的 controller( welcome )。

要修复它,您需要使用fastcgi_split_path_info指令并定义PATH_INFO参数,如下所示:

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;
    }

暂无
暂无

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

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