【问题标题】:CodeIgniter routing not working under NginxCodeIgniter 路由在 Nginx 下不起作用
【发布时间】:2017-09-13 01:55:45
【问题描述】:

通过引用 thisthis 设置 Ubuntu 16.04。

我可以在 http://x.x.x.x/index.php 上看到我对 CI 页面的欢迎,但是当我添加一个测试控制器并转到 http://x.x.x.x/index.php/test 时,我得到了 404 响应。我也没有使用域,而只是使用 IP。

/etc/nginx/nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http {
    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 2;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/;
}

/etc/nginx/sites-available/默认:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html/codeigniter;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html index.php;

    server_name x.x.x.x;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

/var/www/html/codeigniter/application/config/config.php:

$config['base_url'] = ''; //tried putting http://x.x.x.x
$config['index_page'] = 'index.php';
$config['uri_protocol'] = 'REQUEST_URI';

尝试创建一个测试控制器:

<?php
class Test extends CI_Controller
{
    public function index()
    {
        echo "Hello World!";
    }
}
?>

但它没有加载,我得到一个 404 响应。

有什么想法吗?

【问题讨论】:

  • 不,这也不起作用。 404
  • 从这里移除 index.php $config['index_page'] = 'index.php';
  • 不,还有 404
  • 你使用的是哪个版本的codeigniter

标签: php codeigniter nginx


【解决方案1】:

变化

try_files $uri $uri/ =404;

到:

try_files $uri $uri/ /index.php;

不太清楚为什么这会解决问题,但通过反复试验找到它: https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/

这里有一些关于 try_files 的内容: https://serverfault.com/questions/329592/how-does-try-files-work

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 2018-06-27
    • 1970-01-01
    相关资源
    最近更新 更多