Nginx rewrite rule for CodeIgniter

Here is the rule in English:

Any HTTP request other than those related to index.php, the resource folder, the file folder, and the robots.txt file is considered a request for your index.php file.

I have a .htaccess file that works correctly on an 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 conversion tools from the htaccess rule to the nginx rule, but all were incorrect.

Via http://winginx.com/htaccess (there is no exclusion rule for the resource folder ...):

 location / { rewrite ^(.*)$ /index.php/$1 break; } 

Via http://www.anilcetin.com/convert-apache-htaccess-to-nginx/ (error in the value of $ 1):

 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 very difficult to debug a rule.

Here is my nginx configuration:

 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; } } 
+6
source share
3 answers

You can add this to your configuration:

 location ~* ^/(assets|files|robots\.txt) { } 

This will work correctly with your location / rule.

In your configuration, you also need to add the root directory file and the 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) { } ... 
+10
source

Instead, you want to do this:

 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 to process it, you should use $ uri . However, what you are trying to do would be what you would like.

In addition, you need to avoid special regular expression characters such as . using backslash.

+2
source

Try it. This 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; } } 
0
source

Source: https://habr.com/ru/post/955873/


All Articles