Two CI project deployment methods for Nginx: rewrite and PATH_INFO. of course, the latter is relatively simple URL rewrite.
server { listen 8080; server_name www.xxx.com; root /Users/lch/work/www/ci; access_log /usr/local/var/log/access.log; error_log /usr/local/var/log/error.log; location ~ ^/(img|images|script|js|css|upload)/ { root /Users/lch/work/www/ci; break; } location ~ { if (!-e $request_filename) { # for /admin rewrite ^/(admin)$ /index.php?c=welcome&m=index&d=$1 break; # for /admin/index rewrite ^/(admin)/([a-zA-Z_]+)$ /index.php?c=$2&m=index&d=$1 break; # for /admin/account/login rewrite ^/(admin+)/([a-zA-Z_]+)/([a-zA-Z_]+)$ /index.php?c=$2&m=$3&d=$1 break; ## for general URL rewrite ^/([a-zA-Z_]+)/([a-zA-Z_]+)/?(.*)$ /index.php?c=$1&m=$2 last; } root /Users/lch/work/www/ci; fastcgi_pass 127.0.0.1:9001; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Description of the above configuration, because I created a folder named admin in application/controllers/to store the controller related to the background, therefore, it is more than a common path (corresponding to & d = admin. Here we can see that the rewrite method is insufficient. when there is a situation like admin, we need to add the corresponding rewrite rule.
PATH_INFO method
server { listen 8080; server_name www.xxx.com; root /Users/lch/work/kidulty/snap_www; access_log /usr/local/var/log/snap_access.log; error_log /usr/local/var/log/snap_error.log; location ~ ^/(img|images|script|js|css|upload)/ { root /Users/lch/work/kidulty/snap_www; break; } if (!-e $request_filename) { rewrite ^(.*)$ /index.php/$1 last; } location ~ { set $path_info ""; set $real_script_name $fastcgi_script_name; if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") { set $real_script_name $1; set $path_info $2; } root /Users/lch/work/kidulty/snap_www; fastcgi_pass 127.0.0.1:9001; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$real_script_name; fastcgi_param SCRIPT_NAME $real_script_name; fastcgi_param PATH_INFO $path_info; include fastcgi_params; } }
Note:
- If the project URL is similar to http://www.xxx.com/index.php/user/profile, the following rewrite:
if (!-e $request_filename) { rewrite ^(.*)$ /index.php/$1 last; }