【问题标题】:Nginx CodeIgniter remove index.php in the URLNginx CodeIgniter 删除 URL 中的 index.php
【发布时间】:2014-06-12 15:01:53
【问题描述】:

我有一个安装了 CentOS 6.5 Plesk 11.5 的专用服务器,并且我已经打开了 Nginx 来处理 PHP 文件。

我已在 Plesk 中开启服务工具/服务管理

  • 反向代理服务器 (nginx)
  • 对 nginx 的 PHP-FPM 支持

在 Domains/example.com/Web 服务器设置中

  • 智能静态文件处理
  • 直接由 nginx 提供静态文件
  • 通过 nginx 处理 PHP

我使用 CodeIgniter 框架,默认配置下一切正常。但是当我尝试删除 CodeIgniter 配置文件 (config.php) 中的 index.php 时

$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

以前的 URL example.com/project/index.php/text 变成 example.com/project/text 虽然我无法访问链接并出现以下错误:

没有指定输入文件。

我已经搜索了很多解决方案,但对我没有任何帮助。 Plesk 自动为每个域生成 Nginx 配置。根据指南,我尝试在 Domains/example.com/Web Server Settings 的 Additional nginx 指令中添加此代码。

location ~ /project {
     try_files $uri $uri/ /index.php?$args;
 }

我得到一个不同的错误:

找不到文件。

我应该改变什么以及在哪里改变?我浪费了 2 天没有成功。

我在 example.com.conf 中有以下代码,但它是由 Plesk 自动生成的。

server {
    listen IP_IS_HIDDEN:80;

    server_name domain.com;
    server_name www.example.com;
    server_name ipv4.example.com;


    client_max_body_size 128m;


    root "/var/www/vhosts/example.com/httpdocs";
    access_log /var/www/vhosts/system/example.com/logs/proxy_access_log;


    if ($host ~* ^www.example.com$) {
        rewrite ^(.*)$ http://example.com$1 permanent;
    }

    location / {
        proxy_pass http://IP_IS_HIDDEN:7080;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;
    }

    location @fallback {
        proxy_pass http://IP_IS_HIDDEN:7080;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;
    }

    location ~ ^/plesk-stat/ {
        proxy_pass http://IP_IS_HIDDEN:7080;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;
    }

    location ~ ^/(.*\.(ac3|avi|bmp|bz2|css|cue|dat|doc|docx|dts|exe|flv|gif|gz|htm|html|ico|img|iso|jpeg|jpg|js|mkv|mp3|mp4|mpeg|mpg|ogg|pdf|png|ppt|pptx|qt|rar|rm|swf|tar|tgz|txt|wav|xls|xlsx|zip))$ {
        try_files $uri @fallback;
    }

    location ~ ^/~(.+?)(/.*?\.php)(/.*)?$ {
        alias /var/www/vhosts/example.com/web_users/$1/$2;
        fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass "unix:/var/www/vhosts/system/example.com/php-fpm.sock";
        include /etc/nginx/fastcgi.conf;    }

    location ~ ^/~(.+?)(/.*)?$ {
        proxy_pass http://IP_IS_HIDDEN:7080;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;
    }

    location ~ \.php(/.*)?$ {
        fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass "unix:/var/www/vhosts/system/example.com/php-fpm.sock";
        include /etc/nginx/fastcgi.conf;    }

    location ~ /$ {
        index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;
    }


    include "/var/www/vhosts/system/example.com/conf/vhost_nginx.conf";
}

【问题讨论】:

    标签: codeigniter nginx plesk


    【解决方案1】:

    我终于解决了重写问题。重要的是,Plesk 在 Nginx 配置文件中生成默认参数,该文件中的任何更改都是临时的。

    要删除 CodeIgniter 项目中的 index.php,只需在 Plesk>Domains/example.com/Web 服务器设置中为每个项目添加此代码:

    location /category/subcategory {
        try_files $uri $uri/ /category/subcategory/index.php; 
    }
    

    并删除 CodeIgniter config.php 中的 index.php 参数:

    $config['index_page'] = '';
    $config['uri_protocol'] = 'REQUEST_URI';
    

    【讨论】:

    • 对于我的子文件夹项目 /ci;这解决了。但是,如果在浏览器中提到localhost/ci/,它就无法打开index.php,我这样做了:location /ci { index index.html index.htm index.php; try_files $uri $uri/ /ci/index.php; }
    • 删除 $indexPage 的值仍然适用于 CodeIgniter 4。谢谢
    【解决方案2】:

    已解决

    如果我的项目文件夹是/html目录下nginx下的/ci

    @ikxi 的上述回答可以像这样打开页面

    http://localhost/ci/welcome
    

    但是,如果我只是输入,它不会打开 index.php

    http://localhost/ci/
    

    但是这个解决方案对我添加 nginx.conf 文件非常有效。

    找到类似的东西:

    // This lines will be there
    location / {
        root   html;
        index  index.html index.htm index.php;
    }
    // just keep above as it is.. (or you can add "index.php")
    

    在其后添加以下行:

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

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 2016-05-14
      • 2014-10-28
      • 2012-06-15
      • 2012-09-21
      相关资源
      最近更新 更多