由於要符合 PHP CodeIgniter 的 routing rules,必須把 /ci 這個 path 給去掉,在 Apache Web server 透過 RewriteBase 處理,而 Nginx 又更麻煩一點,需處理 fastcgi_param REQUEST_URI 跟 fastcgi_param SCRIPT_FILENAME 資訊。
Apache 的設定:
Alias /ci /data/ci-project
<Directory /data/ci-project>
Options FollowSymLinks
DirectoryIndex index.php
AllowOverride None
Require all granted
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ci
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
</Directory>
Nginx 設定:
set $request_prefix '/ci/';
set $ci_sys_dir '/opt/actions-channel-api/';
location /ci/ {
alias $ci_sys_dir;
index index.php index.html index.htm;
try_files $uri $uri/ /ci/index.php?$query_string;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
set $target_request_uri $request_uri;
if ($target_request_uri ~ ^/ci/(.*)$ ) {
set $target_request_uri /$1;
}
fastcgi_param REQUEST_URI $target_request_uri;
set $target_fastcgi_script_name $fastcgi_script_name;
if ($target_fastcgi_script_name ~ ^/ci/(.*)$ ) {
set $target_fastcgi_script_name $1;
}
fastcgi_param SCRIPT_FILENAME $ci_sys_dir$target_fastcgi_script_name;
}
其他 Nginx 筆記:
- alias 的數值記得要補上最後的 "/" ;alias 跟 root 的最大差別是 alias 的位置不需要在 document root 裡頭
- 在 try_files 流程中,一旦符合條件後,就不會再執行該 nginx location 底部項目,因此還是把 php handler 拉到最外層,而不要全部都寫在 location /ci/ 此區塊裡頭
- nginx 可定義很多環境變數,若想要看它就寫到 log 即可:
- http {
log_format proxy ' "$request" "$status" "$http_referer" "$http_user_agent" $request_time $upstream_response_time "$ci_sys_dir" "$target_fastcgi_script_name" "$target_request_uri" '
}
沒有留言:
張貼留言