Nginx 教程一:在windows环境下的安装、配置、使用以及设置为windows服务自启动

目前国内各大门户网站已经部署了Nginx,如新浪、网易、腾讯等;国内几个重要的视频分享网站也部署了Nginx,如六房间、酷6等。新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx。

    相比apeach、iis,nginx以轻量级、高性能、稳定、配置简单、资源占用少等优势广受欢迎。

下载地址:

  http://nginx.org

启动

  解压至c:\nginx,运行nginx.exe(即nginx -c conf\nginx.conf),默认使用80端口,日志见文件夹C:\nginx\logs; 若nginx.exe启动一闪而过,则需要修改nginx.conf中的默认端口

使用

  http://localhost:端口

关闭

  nginx -s stop 或taskkill /F /IM nginx.exe > nul 

常用配置

   C:\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; 
   } 
   } 
  } 
  
  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 
  
  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下使用更方便。


简单原理

配置文件基本结构就是这样子,由若干指令(directives)构成。指令分为简单指令(siple directives)和块指令(block directives)。


简单指令由指令名和参数构成,指令名和参数以空格分隔,每条指令以分号结尾。例如 

user nginx; 

这就是一条简单指令,表示以 nginx 这个用户身份运行 nginx 工作进程。指令名为 user ,参数为 nginx,最后分号结束。


块指令由指令名和若干由花括号{}包围起来的一组指令组成。例如

events {

    worker_connections 1024;

}

就是一个块指令,指令名为 events,后面紧跟 {} 包围起来的一组指令。

如果一个块指令内有其他指令,那么这个块指令也成为上下文(context),不在任何上下文中的指令被认为是在主上下文中(main context)。例如 events 和 http 位于主上下文中,server 位于 http 上下文中,location 则位于 server 上下文中。

以井号#开头的行是注释行,不起作用。

查看nginx进程

  tasklist /fi "imagename eq nginx.exe",如下显示:
映像名称                       PID 会话名              会话#       内存使用
========================= ======== ================ =========== ============
nginx.exe                     8944 Console                    1      5,128 K
nginx.exe                     6712 Console                    1      5,556 K

nginx常用命令

nginx -s stop 强制关闭 
nginx -s quit 安全关闭 
nginx -s reload 改变配置文件的时候,重启nginx工作进程,来时配置文件生效 
nginx -s reopen 打开日志文件

nginx设置为windows自启动

      每次手动启动和关闭nginx.exe比较麻烦,因此可以在windows系统下将nginx作为系统服务启动。
 把 Nginx 创建为 Windows 的一个服务(一个较好的做法)
  多亏了一个叫做 "Windows Service Wrapper" 的小项目,我们有了一个办法来恰当地启动和停止 Nginx。首先从http://download.java.net/maven/2/com/sun/winsw/winsw/ 下载最新的 exe 程序(Misterdai 写本文时最新的是 "winsw-1.8-bin.exe"。译者已经上传了一个 winsw-1.8-bin.exe 到 CSDN 资源,下载地址:http://download.csdn.net/detail/defonds/4517957)。

a. 准备工作

下载安装nginx,并记住安装目录 官网下载

下载winsw,下载地址 (http://www.cr173.com/soft/101797.html)

b. winsw设置

将winsw可执行程序复制到nginx安装目录下,并重命名为nginx-service

新建名为nginx-service.xml的文件(注:文件名必须与可执行文件名相同)

并编辑如下,其中name为 服务名,executable为可执行程序路径,logpath为程序运行日志路径

[html]  view plain  copy
  1. <service>      
  2.  <id>nginx</id>      
  3.   <name>nginx</name>      
  4.   <description>nginx</description>      
  5.   <executable>E:\phpStudy\nginx\nginx.exe</executable>      
  6.   <logpath>E:\phpStudy\nginx\</logpath>      
  7.   <logmode>roll</logmode>      
  8.   <depend></depend>      
  9.   <startargument>-p E:\phpStudy\nginx</startargument>      
  10.   <stopargument>-p E:\phpStudy\nginx -s stop</stopargument>      
  11. </service>  

如下:

c. 安装服务

在nginx安装目录下运行cmd(快捷方式:shift + 鼠标右键),运行命令:nginx-service.exe install

注:nginx-service.exe uninstall命令可删除对应的系统服务

nginx-service.exestop命令可停止对应的系统服务

nginx-service.exe start命令可启动对应的系统服务

d. 查看服务是否安装成功

计算机管理  -> 服务


如服务为未运行状态,可在此启动服务,或设置为自动启动

注:若服务安装成功,可在cmd(管理员身份)中对服务进行如下操作

启动nginx :net start nginx

停止nginx:net stop nginx

e. 验证nginx是否正常运行

在浏览器中打开网址http://localhost

原文地址:http://www.cnblogs.com/chuncn/archive/2011/10/14/2212291.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值