【问题标题】:nginx + codeigniter configuration not working on CentOS 7?nginx + codeigniter 配置在 CentOS 7 上不起作用?
【发布时间】:2018-03-21 19:16:39
【问题描述】:

我是 nginx 的新手,并尝试在 StackOverflow 上找到问题,但在论坛上没有找到确切的问题。 成功安装 nginx、MySQL 和 php-fpm 后,我测试了运行正常的 php.info。

我正在将 CodeIgniter 项目从 apache 服务器移动到 nginx。我编辑了包含代码的nginx.conf 文件

server {
    listen       80;
    server_name  173.249.40.xxx;

    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/html/ci;
    index index.html index.htm index.php;

    location /ci {
        try_files $uri $uri/ /index.php;
    }        

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

它对php.info 工作正常,就像我的路径是http://173.249.40.xxx/ci/info.php,但在调用时不适用于CodeIgniter controller name,比如http://173.249.40.xxx/ci/index.php/Welcome。我尝试从 https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/# 对于 CodeIgniter 应用程序,但它也不适用于我。 CodeIgniter 应用路径为'/usr/share/nginx/html/ci/;'

请给我一些建议。我该如何解决?

【问题讨论】:

  • 检查root /usr/share/nginx/html;,并确保设置为您的代码所在的目录。在您链接的示例页面中,它是 root /var/www/codeignitor; 有 index.php 或 html 所在的位置。假设,如果您的代码是您所说的应用程序,则将其更改为:root /usr/share/nginx/html/ci/;
  • 我试过那个也没用。 root /usr/share/nginx/html/ci/;

标签: php codeigniter nginx digital-ocean


【解决方案1】:

codeigniter 有它自己的路由机制,所以为了工作,你需要从 web 服务器将你找不到的所有东西传递给index.php,而不是抛出 404 消息。

所以尝试将您的 location / {...} 块更改为以下内容:

location /ci/ {
            # Check if a file or directory index file exists, else route it to index.php.
            try_files $uri $uri/ /ci/index.php;
    }

以上内容来自您问题中的链接。

【讨论】:

  • 我编辑了问题,请您现在查看。我仍然面临 404 错误。
  • 在应用我在配置中提出的更改后,如果仍然失败并显示 404,您能否提供访问日志数据?
  • 我只是将location /ci { } 删除到location/ { },它对我有用。
  • 是的,它也可以,但如果您想从根位置提供其他服务,您需要在/ci/ 中添加/
猜你喜欢
  • 1970-01-01
  • 2019-12-20
  • 2016-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-09
相关资源
最近更新 更多