NGINX server configuration for Codeigniter

This worked for me

location ~* \.php$ {
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                include fastcgi.conf;
        }

I didn't want to change the current document root (/var/www/html) since my 'ci' folder is located at /var/www/html/ci.

So instead, I created a new location block in /etc/nginx/conf.d/default.conf:

server{
...
    location /ci {
        try_files $uri $uri/ /ci/index.php?/$request_uri;
    }
...
}

Thanks to Mert Öksüz for suggesting to use try_files $uri $uri/ /ci/index.php?/$request_uri;.

This one also work:

location /ci {
    try_files $uri $uri/ /ci/index.php?$query_string;
}

Change your root to root /var/www/html/ci

Change your try_files to try_files $uri $uri/ /index.php?/$request_uri;

Be sure your fpm path (unix:/var/run/php-fpm/php-fpm.sock;) is correct.


I faced same problem and modified a little bit nginx conf from this site https://gist.github.com/yidas/30a611449992b0fac173267951e5f17f

server {

listen 80;

# For https
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server ipv6only=on;
# ssl_certificate /etc/nginx/ssl/default.crt;
# ssl_certificate_key /etc/nginx/ssl/default.key;

server_name sc.hr;
root /var/www/sc/hr/;
index index.php index.html index.htm;

# 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 / {
        # Check if a file or directory index file exists, else route it to index.php.
        try_files $uri $uri/ /index.php;
}

location ~ \.php$ {
    try_files $uri /index.php =404;
    fastcgi_pass php-upstream;
    fastcgi_index index.php;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #fixes timeouts
    fastcgi_read_timeout 600;
    include fastcgi_params;
}

# Deny for accessing .htaccess files for Nginx
location ~ /\.ht {
        deny all;
    }

# Deny for accessing codes
    location ~ ^/(application|system|tests)/ {
        return 403;
    }
}

This conf worked on my laradock nginx container.