简体   繁体   中英

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?...

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; .

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.

This worked for me

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

In case someone looking for CI 4 nginx on ubuntu 18.04 configuration :

root /var/www/ci/public;

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.

    # this is not working for the first get argument :
    # try_files $uri $uri/ /index.php?/$request_uri;

    # use this :
    try_files $uri $uri/ /index.php$is_args$args;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM