Nginx在Windows下的应用现状与优势
目前,新浪、网易、腾讯等国内主流门户网站均已采用Nginx作为Web服务器;六房间、酷6等视频分享平台也广泛部署了该技术。近期观察发现,Nginx在国内的技术热度持续攀升,越来越多的网站开始转向使用Nginx以提升性能。
相较于Apache和IIS,Nginx凭借轻量级架构、高性能并发处理能力、运行稳定、配置简洁以及资源占用低等显著优势,赢得了广泛的青睐。
1)下载地址:
http://nginx.org
2)启动
将压缩包解压至C:\nginx目录,随后运行nginx.exe(即执行命令 nginx -c conf\nginx.conf)。默认情况下,服务监听80端口,访问日志文件存放于C:\nginx\logs文件夹中。
3)使用
http://localhost
4)关闭
可通过命令 nginx -s stop 强制停止,或使用 taskkill /F /IM nginx.exe > nul 结束进程。
5)常用配置
主配置文件位于 C:\nginx\conf\nginx.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; } } }
小提示: 运行nginx -V可以查看该Win32平台编译版支持哪些模块。我这里的结果为: Log代码
复制代码 代码如下:nginx version: nginx/0.7.65 TLS SNI support enabled configure arguments: --builddir=objs.msvc8 --crossbuild=win32 --with-debug --prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-path=logs/access.log --error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp --http-proxy-temp-path=temp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp --with-cc-opt=-DFD_SETSIZE=1024 --with-pcre=objs.msvc8/lib/pcre-7.9 --with-openssl=objs.msvc8/lib/openssl-0.9.8k --with-openssl-opt=enable-tlsext --with-zlib=objs.msvc8/lib/zlib-1.2.3 --with-select_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-http_flv_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-mail --with-mail_ssl_module --with-ipv6
可以看出,常用的Memcache和Rewrite模块并未包含在此版本中。因此,该Win32编译版主要适用于基础的开发与测试场景。对于生产环境,建议重新编译包含所需模块的Win32版本,或直接迁移至Linux平台以获得更佳体验。
6)查看nginx进程
使用命令 tasklist /fi "imagename eq nginx.exe" 可查看当前运行的Nginx进程,输出示例如下: 映像名称 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 -s reopen:重新打开日志文件。
8)其它
可通过配置文件启动多个Nginx工作进程,但同一时刻仅有一个进程处于工作状态,其余进程阻塞等待。单个Nginx工作进程最多可同时处理1024个连接。此外,需要共享内存的缓存或模块在Windows环境下无法正常使用。不过,Nginx官方正在改进此状况,未来计划通过服务方式运行,并使用I/O完成端口(IOCP)替代select模型,以实现多工作进程的并发处理。若需配合php-cgi使用,需设置环境变量PHP_FCGI_MAX_REQUESTS为0,否则php-cgi运行一定次数后会退出,需手动重启。
以上步骤已在Windows 7系统上验证通过。
8)nginx以windows服务形式启动
1.下载微软两个工具:
instsrv.exe srvany.exe
2.执行命令:
instsrv Nginx C:/nginx/srvany.exe
3.配置Nginx的运行参数
可以直接将配置导入到注册表
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/NGINX/Parameters] "Application"="C://nginx//nginx.exe" "AppParameters"="" "AppDirectory"="C://nginx//"
注意:Windows下的Nginx内置模块较少,具体支持的模块可使用 Nginx -V 命令查看。
9)Nginx下部署mono+asp.net环境
1、从Mono for Windows中提取FastCGI-Mono-Server
2、Nginx nginx.conf 的配置:
复制代码 代码如下:worker_processes 1; error_log logs/error-debug.log info;
events { worker_connections 1024; }
http { include mime.types; default_type text/plain; sendfile on;
keepalive_timeout 65; index index.html index.htm;
server { listen 80; server_name yourdomain.com; index index.aspx default.aspx;
location / { root D:\www/yourwebapp;
fastcgi_pass 127.0.0.1:8000; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; } } }
将提取出的 FastCGI-Mono-Server 所有文件注册到GAC中(否则Web应用将无法找到它们,当然也可以直接放置于 webapp/bin 目录下),然后将其解压至指定文件夹,此处假设为 D:/FastCGI-Mono-Server。
之后即可按下列命令启动 FastCGI 服务: fastcgi-mono-server2 /socket=tcp:127.0.0.1:8000 /root="D:\www\yourwebapp" /applications=yourdomain.com:/:. /multiplex=True
最后启动 Nginx 服务器,此时我们的 ASP.NET 程序即可脱离 IIS 独立运行。
以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持本站。