1)下載地址:
2)啟動
解壓至d:\nginx,運行nginx.exe(即nginx -c conf\nginx.conf),默認使用80端口,日志見文件夾D:\nginx\logs
3)使用
4)關閉
nginx -s stop
5)常用配置
D:\nginx\conf\nginx.conf,使用自己定義的conf文件如my.conf,命令為nginx -c conf\my.conf
常用配置如下:
Nginx.conf代碼
http {
server {
#1.偵聽80端口
listen 80;
location / {
# 2. 默認主頁目錄在nginx安裝目錄的html子目錄。
root html;
index index.html index.htm;
# 3. 沒有索引頁時,羅列文件和子目錄
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}
# 4.指定虛擬目錄
location /tshirt {
alias D:\programs\Apache2\htdocs\tshirt;
index index.html index.htm;
}
}
# 5.虛擬主機www.emb.info配置
server {
listen 80;
server_name www.emb.info;
access_log emb.info/logs/access.log;
location / {
index index.html;
root emb.info/htdocs;
}
}
}
6)查看nginx進程
tasklist /fi "imagename eq nginx.exe",如下顯示:
映像名稱 PID 會話名 會話# 內存使用
========================= ======== ================ =========== ============
nginx.exe 8944 Console 1 5,128 K
nginx.exe 6712 Console 1 5,556 K
7)nginx常用命令
nginx -s stop 強制關閉
nginx -s quit 安全關閉
nginx -s reload 改變配置文件的時候,重啟nginx工作進程,來時配置文件生效
nginx -s reopen 打開日志文件
8)其它
可以通過配置文件開啟多個nginx工作進程,但同時只有其中一個nginx工作進程在工作,其他的阻塞等待。
一個nginx工作進程最多同時可以處理1024個連接。
nginx中需要共享內存的cache或者模塊無法在windows下正常使用。
不過,nginx官方正在改進,將來nginx會以服務的方式運行,使用 I/O completion ports代替select方法,使多個工作進程能並發工作。
要使用nginx配合php-cgi使用,需要修改環境變量,否則,php-cgi運行一定次數就推出,需要重啟,設置PHP_FCGI_MAX_REQUESTS這個變量為0即可。
以上在win7上通過。
8)nginx以windows服務形式啟動
1.下載微軟兩個工具:
instsrv.exe srvay.exe
2.執行命令:
instsrv Nginxc:/nginx/srvany.exe
3.配置Nginx的運行參數
可以直接將配置導入到注冊表
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/NGINX/Parameters]
"Application"="D://nginx//nginx.exe"
"AppParameters"=""
"AppDirectory"="D://nginx//"
注意:windows 下的Nginx 內置的module 很多沒有,用Nginx -V 命令查看。
跨域示例:
conf內填寫:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#下面這句話就是允許跨域,我以知乎日報API為例
add_header Access-Control-Allow-Origin *;
location / {
proxy_pass http://news-at.zhihu.com/api/3/news/latest;
}
#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://www.quankan.tv;
# }
# 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;
#}
}
前台代碼示例:
$.getJSON("http://localhost", function(data){ console.log(data); });
打印結果顯示: