ScaleScaleScaleScale

Tips / Nginx


CodeIgniter Nginx Configuration

Following our Nginx Configuration for PHP Frameworks guides, today I will share you the CodeIgniter nginx configuration to get it working using Nginx web server. According to the author’s own words: CodeIgniter is a full Baked PHP Framework, proven to be agile & open PHP web application framework with a small footprint. So, if you are using CodeIgniter and Nginx, this is your lucky day.

I will assume you have nginx and php-fpm properly installed and configured to work with sockets, otherwise, you should need to change fastcgi_pass_unix to 127.0.0.1:9000, that is the default port and interface php-fpm listen to. Also, feel free to change domain.tld with your own domain name and root directory to the real directory of your app.

CodeIgniter Nginx Server Block

Check out this configuration:

server {
    listen   80;
    server_name mysite.com www.mysite.com;

    root   /path/to/your/website.com/;
    index  index.php;
    error_log log/error.log;

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

    # main codeigniter rewrite rule
    location / {
        try_files $uri $uri/ /index.php;
    }
        
        # php parsing 
        location ~ .php$ {
            root           /path/to/your/website.com/;
            try_files $uri =404;
            fastcgi_pass   unix:/tmp/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 256 4k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
        }

}

Update CodeIgniter config.php

Once you are done, make sure that your CodeIgniter config.php contains the following information:

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

Need additional information? Check out this links:

Popular search terms:

  • nginx codeigniter
  • codeigniter nginx
  • codeigniter $config[uri_protocol]>= REQUEST_URI
  • codeigniter 3 nginx try files
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Arfan Ali

    did not work for me still getting 404 Not Found.
    I am using. PHP7, Nginx, Digital hosting