windows下nginx安装、配置与使用

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

    相比apeach、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,使用自己定义的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下使用更方便。

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"="C://nginx//nginx.exe"
"AppParameters"=""
"AppDirectory"="C://nginx//"

  注意:windows 下的Nginx 内置的module 很多没有,用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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Windows安装 nginx 比较简单,可以直接从官网下载安装包并进行安装。 1. 从 nginx 官网下载 Windows 版本的安装包(http://nginx.org/en/download.html) 2. 双击安装包并依次按照安装向导的提示进行安装。 3. 安装完成后,在命令行中输入 nginx,如果能看到“nginx 已成功启动”的提示,说明 nginx 安装成功。 配置 nginx: 1. 打开 nginx 安装目录下的 conf/nginx.conf 文件。 2. 修改配置文件中的相关配置,如服务器的地址、端口号等。 3. 保存配置文件。 4. 在命令行中输入 nginx -s reload,重新加载配置文件。 注意:具体配置内容因个人需求而异。 ### 回答2: WindowsNginx安装配置相对简单。下面是详细步骤: 1. 下载Nginx Windows版本的安装包,推荐到官网下载,地址为:http://nginx.org/en/download.html 2. 解压安装包,将解压后的文件夹重命名为nginx,并将其移动到C盘根目录。因为nginx默认会在C盘根目录寻找配置文件。 3. 进入nginx文件夹,打开conf目录下的nginx.conf文件,在开头添加如下代码,指定Nginx的运行目录: ```bash chdir c:/nginx/ ``` 4. 在nginx.conf文件中修改server块的配置,添加监听端口号和root目录,如下: ```bash server{ listen 80; server_name localhost; root html; index index.html index.htm; } ``` 这里的端口号可以根据实际需求修改,root目录则为Nginx的默认站点目录。 5. 启动nginx,在cmd命令行下输入: ```bash cd C:\nginx start nginx ``` 如果没有错误提示,则说明启动成功。 6. 在浏览器中输入localhost,如果看到Nginx的欢迎页面,则表示配置成功。 7. 关闭nginx,在cmd命令栏下输入: ```bash nginx -s stop ``` 注意:如果修改配置文件后需要重新启动Nginx,则输入: ```bash nginx -s reload ``` 总结:以上是WindowsNginx安装配置步骤,如果需要添加虚拟主机、负载均衡等功能,在nginx.conf文件中进行对应的配置即可。 ### 回答3: Nginx是一个高性能的HTTP和反向代理服务器。在Windows操作系统中,通过安装Nginx可以实现Web服务器的搭建以及各种代理和负载平衡操作。下面是WindowsNginx安装配置步骤。 一、下载Nginx 访问Nginx的官网,选择Windows版本的Nginx进行下载,下载后将文件解压到指定目录。 二、配置Nginx 1、进入到Nginx的目录中找到conf文件夹,这个文件夹中包含了Nginx配置文件。 2、打开nginx.conf文件,修改以下选项: 修改listen:将listen 80改为listen 8080,如果端口冲突,可以选择其他端口。 修改server_name:将server_name localhost改为server_name 你的域名,也可以使用IP地址。 修改root:将root html改为你的网站根目录。 3、将文件保存后,关闭文件。 三、启动Nginx服务 1、在cmd中进入到nginx.exe所在的文件夹中。 2、执行命令:nginx.exe -c conf/nginx.conf 提示nginx: [emerg] 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),说明80端口被占用,改成其他端口即可(如8080)。 3、打开浏览器,输入localhost:8080,就可以访问Nginx搭建的Web服务器了。 总结: 以上就是在Windows操作系统下Nginx安装配置步骤。需要注意的是,配置文件中的选项对应的含义需要认真了解并修改,否则可能会造成配置错误或安全隐患。在运行之前还应当检查一下端口是否被占用,并选择一个空闲的端口,才可以正常启动Nginx服务器。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值