這個原因是建構在部署專案時,採用 Apache Alais 跟 Requests Rewrite 方式,非常便利 Requests URI 導向至指定的 PHP CI Project:
# Apache Web Server
Alias /prefix/project /path/ci/project
<Directory /path/ci/project>
Options FollowSymLinks
DirectoryIndex index.php
AllowOverride None
Require all granted
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /prefix/project
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>
其中 /prefix 是個虛擬的目錄,然而,在 Nginx 環境中,若要採用類似的架構時,就會需要處理一些問題,包含要做 requests uri 或 php script filename 的更新等,若 PHP CI Project 有提供 Web UI 時,就還得處理 css/js/image 的 requests 導向。
最終的解法:
# static files
location ^~ /prefix/project/assets/ {
alias "/path/ci/project/assets/";
}
# php requests
location ^~ /prefix/project {
root "path/ci/project";
try_files $uri $uri/ /prefix/project/index.php?$query_string;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_read_timeout 180;
fastcgi_buffer_size 64k;
fastcgi_buffers 512 32k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
fastcgi_index index.php;
include fastcgi_params;
set $target_fastcgi_script_name $fastcgi_script_name;
if ($target_fastcgi_script_name ~ ^/prefix/project/(.*)$) {
set $target_fastcgi_script_name /$1;
}
fastcgi_param SCRIPT_FILENAME $document_root$target_fastcgi_script_name;
}
}
此外,若想多了解 nginx location 的優先順序,可以參考這則: Nginx location priority - Stack Overflow
沒有留言:
張貼留言