這幾天在配置Nginx,PHP用FastCGI,在Apache裏,有alias,比較方便,在Nginx下沒有虛擬目錄概念的,是用location配合alias使用,但使用alias標籤的目錄塊中不能使用rewrite的break。
一、例子說明:
1)我的環境是:web根目錄在 /var/www/html/中,但是我要加上一個類似於apache的別名目錄 /bbs ,此目錄不在 web根目錄中。
我的配置文件如下:
server { listen 80; server_name localhost; default_type text/plain; location / { root /var/www/html; index index.php index.htm index.html; } location /bbs { alias /opt/bbs; index index.html index.htm index.php; } location ~ ^/bbs/.+\.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /opt$fastcgi_script_name; include fastcgi_params; #include fastcgi.conf; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; include fastcgi_params; #include fastcgi.conf; } }
說明: 上面這個就是成功的例子。
二、但是又如下幾點需要注意:
1)location ~ \.php$ {} 段,必須放在 location ~ ^/bbs/.+\.php$ {} 段後面,否則/bbs/的url打不開
2) location ~ ^/bbs/.+\.php$ {} 裏面也可以寫成如下:
location ~ ^/bbs/.+\.php$ { root /opt; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
也就是用變量名 $document_root 代替 /opt; 其實每個 location {}中的 $document_root 都是局部變量,都是在本段配置 root指令指定的路徑。