Nginx CodeIgniter remove index.php in the URL

ikzi picture ikzi · Jun 12, 2014 · Viewed 9.8k times · Source

I have a dedicated server installed with CentOS 6.5 Plesk 11.5 and I've turned on Nginx to process PHP files.

I've turned on services in Plesk Tools/Services Management:

  • Reverse Proxy Server (nginx)
  • PHP-FPM support for nginx

and in Domains/example.com/Web Server Settings

  • Smart static files processing
  • Serve static files directly by nginx
  • Process PHP by nginx

I use CodeIgniter Framework, everything works well with default configurations. But when I try to remove index.php in the CodeIgniter config file (config.php)

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

previous URL example.com/project/index.php/text turns into example.com/project/text though I can't access to link and have following error:

No input file specified.

I've searched and there are many solutions, nothing worked for me. Plesk automatically generates Nginx configurations for each domain. According to guides, I've tried adding this code in Additional nginx directives in Domains/example.com/Web Server Settings.

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

I get a different error:

File not found.

What should I change and where? I've wasted 2 days without success.

I have following code in example.com.conf but it's generated automatically by the 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";
}

Answer

ikzi picture ikzi · Jun 13, 2014

I have finally solved a rewrite problem. What it's important, Plesk generates default parameters in Nginx configuration file, any changes inside that file is temporary.

To remove index.php in CodeIgniter projects, just add this code for each project in the Plesk>Domains/example.com/Web Server Settings:

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

and remove index.php parameter in CodeIgniter config.php:

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