Nginx 是一个轻量级的高性能 Http WebServer,以事件驱动方式编写,因此相比 Apache 而言,Nginx 更加稳定、性能更好,而且配置简单,资源占用较低。
1. 安装 Nginx
从 v0.7.52 开始,Nginx 开始发布 Windows 版本的 Nginx,你可以在其官方网站上面下载:http://nginx.net
下载后直接解压即可,这里解压缩到c:\nginx目录。
2. 启动Nginx
命令行进入c:\nginx目录,运行nginx.exe,启动控制台窗口。默认启用80端口。用过Tomcat的人都希望能在控制台看到启动日志,nginx的日志却不得不查看logs目录下的相应log文件。
3. 访问欢迎html页
在浏览器中访问http://localhost,可以看到默认的欢迎页.
4. 停止Nginx
Ctrl+C没反应。于是关闭控制台窗口。可是再访问http://localhost依然有效。查看进程,发现nginx根本没有被关闭。因此如果想彻底关闭nginx,应该是
nginx -s stop
请参考官方文档 nginx/Windows usage
或者使用windows的taskkill命令:
taskkill /F /IM nginx.exe > nul
5. Ngnix常用配置
Nginx的所有配置都默认使用conf/nginx.conf文件,其地位相当于apache的httpd.conf文件 。当运行nginx.exe暗含运行了nginx -c conf\nginx.conf. 如果想使用自己定义的conf文件如my.conf,命令为nginx -c conf\my.conf.
常用配置如下:
1. http {
2. server {
3. 1.侦听80端口
4. 80;
5. location / {
6. 2. 默认主页目录在nginx安装目录的html子目录。
7. root html;
8. index index.html index.htm;
9. 3. 没有索引页时,罗列文件和子目录
10. autoindex on;
11. autoindex_exact_size on;
12. autoindex_localtime on;
13. }
14. 4.指定虚拟目录
15. location /tshirt {
16. alias D:\programs\Apache2\htdocs\tshirt;
17. index index.html index.htm;
18. }
19. }
20. 5.虚拟主机www.emb.info配置
21. server {
22. 80;
23. server_name www.emb.info;
24. access_log emb.info/logs/access.log;
25. location / {
26. index index.html;
27. root emb.info/htdocs;
28. }
29. }
30. }
小提示:
运行nginx -V可以查看该Win32平台编译版支持哪些模块。我这里的结果为:
1. nginx version: nginx/0.7.65
2. TLS SNI support enabled
3. configure arguments:
4. --builddir=objs.msvc8
5. --crossbuild=win32
6. --with-debug --prefix=
7. --conf-path=conf/nginx.conf
8. --pid-path=logs/nginx.pid
9. --http-log-path=logs/access.log
10. --error-log-path=logs/error.log
11. --sbin-path=nginx.exe
12. --http-client-body-temp-path=temp/client_body_temp
13. --http-proxy-temp-path=temp/proxy_temp
14. --http-fastcgi-temp-path=temp/fastcgi_temp
15. --with-cc-opt=-DFD_SETSIZE=1024
16. --with-pcre=objs.msvc8/lib/pcre-7.9
17. --with-openssl=objs.msvc8/lib/openssl-0.9.8k
18. --with-openssl-opt=enable-tlsext
19. --with-zlib=objs.msvc8/lib/zlib-1.2.3
20. --with-select_module
21. --with-http_ssl_module
22. --with-http_realip_module
23. --with-http_addition_module
24. --with-http_sub_module
25. --with-http_dav_module
26. --with-http_stub_status_module
27. --with-http_flv_module
28. --with-http_gzip_static_module
29. --with-http_random_index_module
30. --with-http_secure_link_module
31. --with-mail
32. --with-mail_ssl_module
33. --with-ipv6
显然,最经常用的memcache, rewrite模块都没在其中,因此该win32编译版本仅能供基本开发测试使用,对于产品平台,应该重新编译自己想要的win32版本,或者在linux下使用更方便。