據說 nginx 是這幾年來 Web 服務器的後起之秀,是“Apache殺手”,由俄羅斯程式員編寫。是一個輕量級的 Web 服務器,也是據說,佔用資源少,高並發,在某些情況下,效率是 Apache 的 10 倍。國內外很多大型門戶站都在用。
經不住蠱惑,決定在 Windows Server 2003 下安裝試用一下,並與 PHP 進行整合。
截至 2010 年 5 月底,nginx 的最新版本是 0.8.38,可以到 http://www.nginx.org/ 下載。
解壓 PHP 到 C:/php-5.3.2-Win32-VC6-x86/,正確配置 php.ini 檔案。
直接解壓下載的 nginx-0.8.38.zip 檔案到 C:/nginx-0.8.38/,檔案夾結構:
conf/
contrib/
docs/
html/
logs/
temp/
nginx.exe
雙擊運行nginx.exe檔案,nginx 就開始提供服務。
html/ 檔案夾為網站預設根目錄。
conf/ 放置 nginx 配置有關的檔案。設定檔 nginx.conf 內容如下(#號打頭的語句被注釋掉了,可以參考):
server {……} 部分配製了 nginx 的 http 服務的連接埠(預設為80)、網域名稱、字元集、根資料夾、首頁檔案等內容。
其中以下部分配置 nginx 與 PHP 以 fastcgi 方式進行整合,“C:/nginx-0.8.38/html”表示網站的根資料夾:
location ~ /.php$ {
# root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME C:/nginx-0.8.38/html$fastcgi_script_name;
include fastcgi_params;
}
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
autoindex on;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ /.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ /.php$ {
# root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME C:/nginx-0.8.38/html$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ //.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
這時,在本機上開啟瀏覽器,瀏覽 http://localhost,可以看到資訊“Welcome to nginx!”,內容來自 html 下的 index.html 檔案。
為了真正與 PHP 一起協同工作,還必須運行 PHP 的 php-cgi.exe 程式。方法是,在命令視窗內,切換到 php-cgl.exe 所在檔案夾,運行下,即 C:/php-5.3.2-Win32-VC6-x86,運行 php-cgi.exe -b 127.0.0.1:9000 命令,即:
C:/php-5.3.2-Win32-VC6-x86>php-cgi.exe -b 127.0.0.1:9000
這裡的127.0.0.1:9000 就是我們在 nginx.conf 檔案中配置的那個,連接埠號碼一定要相同。
nginx.exe 與 php-cgi.exe 兩條命令啟動並執行前後順序對 PHP 檔案的解析沒有影響。
這時,我們在根目錄下放一個 xxx.php 檔案,在瀏覽器地址欄裡面輸入 http://localhost/xxx.php,應該看到結果。建議檔案內容為:
<?php
phpinfo();
?>
我們可以看到 PHP 環境的很多有用的資訊。
nginx 還可以配置實現反向 Proxy、多個虛擬機器主機、url重新導向等功能。
作者:張慶(網眼) 西安 PHP 教育培訓中心 2010-5-28
來自“網眼視界”:http://blog.why100000.com
“十萬個為什麼”電腦學習網:http://www.why100000.com