
我正在嘗試使用 nginx 重寫特定路徑。我已經使用 nginx/php-fpm/php 設定了我的伺服器,並且工作正常。
我有以下伺服器配置:
server {
listen 80;
server_name domain.com;
root /srv/www/domain.com/public;
location ~ ^/index.php($|/) {
fastcgi_split_path_info ^(.+.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
}
location / {
index index.php;
try_files $uri /index.php?$args;
}
}
這有效。即,當我訪問http://domain.com
它時,它會很好地呈現 php 檔案。不過,我現在正在嘗試重寫/update/whatever
./update.php?var=whatever
為了做到這一點,我將以下location
區塊添加到我的配置中(在該location ~ ^/index.php($|/) {
區塊之前):
location ~* ^\/update/(\w+)$ {
rewrite ^/update/(\w+)$ /update.php?browser=$1 last;
}
但是當我嘗試存取 URL 時,http://domain.com/update/whatever
它會下載 php 文件,而不是提供 PHP 的解析輸出。
我還嘗試將fastcgi
指令添加到該位置塊,並獲得相同的結果。任何人都可以告訴我我的配置搞砸了什麼?
答案1
這行:
location ~ ^/index.php($|/) {
告訴 nginx 僅傳遞index.php
給 php 解釋器,而且您需要它也適用於其他 php 文件,因此這裡需要更通用的規則:
location ~ ^/(.*).php($|/) {