nginx code igniter remove index.php prefix

Objective

Remove the index.php prefix from your nginx code igniter instance.

Assumptions

  • In your main nginx conf file you define how php is called (unix socket or ip:port)
  • You will replace foo.example.com with whatever your domain name is
  • The proper logging path will be defined per your system as opposed to the location i have used

Configurations

Example upstream in main nginx conf

        # Upstream to abstract backend connection(s) for PHP.
        upstream php {
                #using unix socket
                server unix:/tmp/php-fpm.sock;
                #
                #
               	#server 127.0.0.1:9000;
        }

Virtual Host to remove index.php

Here’s how i did it:

erver {
        server_name .foo.example.com;
        root /opt/www/foo.example.com;

        access_log /opt/nginx/logs/foo-access.log;
        error_log /opt/nginx/logs/foo-error.log info;
        index index.html index.php;

        #set expiration of assets to MAX for caching
        location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
                expires max;
                log_not_found off;
        }

	location / {
             try_files $uri $uri/ @codei;
        }

	location @codei {
        rewrite ^/[_0-9a-zA-Z-]+/(.*\.php)?$ /$1 last;
        rewrite .* /index.php last;
        }

	location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(.*)$;
                include fastcgi_params;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                fastcgi_pass php;
                fastcgi_buffer_size 4K;
                fastcgi_buffers 64 4k;
        }
}

CodeIgniter Specific Info

After this, make sure that your codeIgniter config.php contains the following information:

$config['base_url'] = "http://foo.example.com/";
$config['index_page']   = "";
$config['uri_protocol'] = "REQUEST_URI";

Still not working?

Try following the CodeIgniter http://ellislab.com/codeigniter/user_guide/tutorial/static_pages.html

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.