Windows下Nginx初入門
下載
目前(2016.5.17),nginx的穩定版本是1.10.0,在官網下載先,http://nginx.org/en/download,建議使用stable version版
這是一個zip檔案,解壓後即可使用
啟動
綠色檔案,無須安裝,直接即可啟動。
據我所知,3種啟動途徑,其實都類似:
一、雙擊nginx.exe圖示,可見黑視窗一閃而過,啟動完畢。
二、命令列到nginx目錄,輸入nginx啟動。(注,此方式命令列視窗無任何提示,且被鎖定)
三、命令列到nginx目錄,輸入start nginx啟動,此方式不鎖定
啟動後,預設情況下(無修改配置),可見到有兩個nginx的程序,一個是master process,一個是worker processes。
測試
預設nginx部署了些靜態內容,我們可通過它測試nginx是否在工作。
預設的配置檔案(NGINX_HOME/conf/nginx.conf)如下:
#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;
}
#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 /scripts$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 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
1、啟動了1個worker processes
2、worker_connections,最大併發數為1024
3、include mime.types,引入mime.types檔案所宣告的副檔名與檔案型別對映
4、application/octet-stream,預設使用application/octet-stream
5、sendfile,開啟高效檔案傳輸模式
6、監聽本機“localhost”的80埠
7、對映目錄為“當前目錄的html目錄”
8、出現500、502、503、504錯誤,則對映到50x.html
瀏覽地址http://localhost,即可訪問其預設頁面,即對映到NGINX_HOME/html/index.html
其他靜態內容,如html、圖片,可自行新增測試。
日誌
日誌預設位於NGINX_HOME/logs/,可見:
1、access.log,訪問日誌
2、error.log,異常日誌
3、nginx.pid,程序(僅在啟動nginx後才有此日誌)
常見錯誤
如果啟動失敗 可以看下logs目錄下 error.log 檔案裡的錯誤資訊。
我在第一次安裝的時遇到兩個錯誤,也是最容易碰到的問題,在這裡列出來方便大家碰到相同的問題時快速解決。
1. 端口占用問題
我的配置檔案裡服務偵聽的是 80 埠,由於機器上部署了IIS,80埠被預設站點佔用,把站點關閉就可以了,這個問題在錯誤日誌裡記錄是這樣的。
2015/01/15 10:44:12 [emerg] 8800#5988: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
碰到類似的錯誤,請確認埠是否被佔用或被防火牆遮蔽
2.Nginx所在目錄有中文
錯誤日誌大致輸出一下內容
2015/01/15 11:55:55 [emerg] 5664#8528: CreateFile() "E:\軟體\nginx-1.7.8/conf/nginx.conf" failed (1113: No mapping for the Unicode character exists in the target multi-byte code page)
3. 啟用快取時報錯
2015/01/15 17:26:50 [emerg] 17068#20356: shared zone "cache_one" has no equal addresses: 02CF0000 vs 02A20000 2015/01/15 17:26:50 [alert] 11536#11228: worker process 17068 exited with code 1
我一直沒有找到解決的方法,有人說重啟服務,或者快取設定大一點就可以了,我試了一下沒有用的,官網原文是這樣講的,只能認為windwos下無解了。
: The cache and other modules which require shared memory support do : not work in Windows Vista and later due to address space layout : randomization being enabled in these Windows versions.