Nginx rewrite rule for CodeIgniter
Solution 1
You can add this to your config:
location ~* ^/(assets|files|robots\.txt) { }
This will work correctly with your location /
rule.
Your config also need to add root document and default index file.
...
root /ftp/wardrobe;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
location ~* ^/(assets|files|robots\.txt) { }
...
Solution 2
You want to do this instead:
if ($request_uri !~ ^/(index\.php|assets|files|robots\.txt)) {
rewrite ^/(.*)$ /index.php/$1 last;
}
$request_uri is for the original URI request by the client. If you want the URI request AFTER other Nginx rewrite rules have processed it then you would use $uri. However, for what you are trying to do the prior would be the one you would want.
Also, you need to escape special regular expression characters like .
by using a backslash.
Solution 3
Please try this. It works for me.
server {
server_name domain.tld;
root /var/www/codeignitor;
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 / {
# 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 =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Letomkov
Updated on April 20, 2020Comments
-
Letomkov about 4 years
Here is the rule in English:
Any HTTP request other than those for index.php, assets folder, files folder and robots.txt is treated as a request for your index.php file.
I have an
.htaccess
file that works correctly on Apache server:RewriteCond $1 !^(index\.php|assets|files|robots\.txt) RewriteRule ^(.*)$ index.php/$1 [L]
Some correct results for this rule:
example.com
=example.com/index.php
example.com/index.php/welcome
=example.com/welcome
example.com/assets/css/main.css
!=example.com/index.php/assets/css/main.css
I tried some tools to convert from htaccess rule to nginx rule but all were incorrect.
Via http://winginx.com/htaccess (missing the exception rules for assets folder...):
location / { rewrite ^(.*)$ /index.php/$1 break; }
Via http://www.anilcetin.com/convert-apache-htaccess-to-nginx/ (error in $1 value):
if ($1 !~ "^(index.php|assets|files|robots.txt)"){ set $rule_0 1$rule_0; } if ($rule_0 = "1"){ rewrite ^/(.*)$ /index.php/$1 last; }
How can I fix this? It's really hard to debug the rule.
Here is my nginx config so far:
server { listen 80; server_name www.example.com example.com; location / { try_files $uri $uri/ /index.php?/$request_uri; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
-
Letomkov over 10 yearsIt seems correct but doesn't work. Here are the results of some cases:
example.com
- 404,example.com/index.php
- OK,example.com/assets/css/main.css
- 404. Is there anyway can I debug the rule? -
cryptic ツ over 10 years@Letomkov update your question with the contents of your Nginx config file.
-
Letomkov over 10 yearsI added my nginx config file to my question. I tried to replace the
location / { ... }
rule with your current question but it didn't work. -
Letomkov over 10 yearsI can load the page with or without index.php but still can't load file in the assets folder
-
anvoz over 10 yearsAdd
error_log /var/log/nginx/localhost.error_log debug; rewrite_log on;
to your config, restart the server then try to load a file in assets folder. Tell me if you see any error. -
Letomkov over 10 yearsHow could I get an error:
open() "/etc/nginx/html/assets/..." failed (No such file or directory)
? The rule is missing something that help nginx route to correct web directory? -
anvoz over 10 yearsYou were missing
root {$your_document_root};
-
Letomkov over 10 yearsI missed something in my config that I've just figured out. Your rule is correct but it have to work with my
location /
rule. But if you request to a non-existed file in assets folder, it will still need to load theindex.php
file. -
Bernard Rosset about 10 yearsYou shall consider replacing
$request_uri
by$uri
, as the latter is normalized (see nginx.org/en/docs/http/ngx_http_core_module.html#variables). For that to work, you will also need to change the regex filtering.php
files processing, as yours currently won't handle the latter case of try_files as provided by Letomkov. See mezcalito.fr/hebergement/labo-single/2011/01/… for a more acceptable solution