Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX server configuration for Codeigniter

/etc/nginx/conf.d/default.conf

server{
listen 80;
listen [::]:80;
server_name  192.168.56.101 192.168.101.100 localhost;
root   /var/www/html;
index index.php index.html index.htm;

location / {
    try_files $uri $uri/ =404;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;

location = /50x.html {
    root /var/www/html;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}


location ~ /\.ht {
    deny all;
}
}

my codeigniter folder is 'ci' which is located in /var/www/html/ci what configuration do I need to work url rewriting?...

like image 742
Amin Avatar asked Aug 11 '17 11:08

Amin


4 Answers

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;
}
like image 140
Amin Avatar answered Oct 09 '22 03:10

Amin


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.

like image 44
Mert Öksüz Avatar answered Oct 09 '22 02:10

Mert Öksüz


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.

like image 1
Mikayel Margaryan Avatar answered Oct 09 '22 02:10

Mikayel Margaryan


This worked for me

location ~* \.php$ {
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                include fastcgi.conf;
        }
like image 1
4Jean Avatar answered Oct 09 '22 04:10

4Jean