最近在 ubuntu 上搗騰 nginx,安裝成功了,就剩 rewrite 還沒試驗。因為伺服器上有多個網站,還不敢直接在上面試,慢慢來。網上查了些文章,下了一篇留著做試驗。
nginx 的虛擬主機配置其實和 apache 大同小異。
需要注意的幾點:
第一、關於 .htaccess 配置(靜態配置),在 nginx 上一般要寫進虛擬主機配置文本,也有人用 include .htaccess 文件來解決,但我沒試過。
第二、先計劃好用何種方式跑 php,fastcgi?我并不認為網上流傳的那種做法是上策,相反,作為出色的反向代理伺服器,nginx 應該發揮反向代理的優勢,所以執行 php 的方式請先斟酌。
回到正題。觀察一下 nginx 的目錄結構,你大概就知道怎麼做了,和 apache 的虛擬主機配置基本一致。在 /etc/nginx/sites-available 新建一個文件,比如 www.blogguy.cn,然後 vi www.blogguy.cn 加入內容:
server
{
listen [::]:80;
server_name www.blogguy.cn blogguy.cn;
root /var/www/blogguy.cn;
index index.html index.htm index.php;
include /etc/nginx/common.conf;
location /nginx_status
{
stub_status on;
access_log off;
allow all;
}
}
簡單解釋:listen 是監聽端口,不必多說;server_name 要多說幾句,你可能想到 server_alias,其實在 nginx 中第一個就是 server_name,後面的就是 server_alias,所以別名不用另外聲明,這和 apache 差別很大,注意一下;index 是查找網頁的先後順序;include 是包含文件,這裡包含的 common.conf 用來指定 php 運行方式、文件緩存等,示例如下:
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
break;
}
location ~ .*\.php$ {
# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
if ( $fastcgi_script_name ~ \..*\/.*php ) {
return 403;
}