windows下nginx安装、配置与使用

摘要:
最近,人们发现Nginx技术在中国越来越流行,越来越多的网站开始部署Nginx。与apeach和iis相比,nginx以其重量轻、性能高、稳定性好、配置简单、资源消耗低等优点而备受欢迎。1) 下载地址:http://nginx.org2)开始解压到c:ginx并运行nginx.Exe,默认情况下使用端口80,日志请参见文件夹c:ginxlogs3)http://localhost4)关闭nginxsstop或taskkill/F/IMnginxExe˃nul5)通用配置C:ginxconfginx.conf。使用自定义的conf文件,例如my。conf,命令是nginxcconfmy。conf的常见配置如下:NginxConf-code http{server{#1。侦听端口80 listen80;location/{#2。默认主页目录是nginx安装目录的html子目录。

 

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

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

1)下载地址:

  http://nginx.org

2)启动

  解压至c: ginx,运行nginx.exe(即nginx -c conf ginx.conf),默认使用80端口,日志见文件夹C: ginxlogs

3)使用

  http://localhost

4)关闭

  nginx -s stop 或taskkill /F /IM nginx.exe > nul 
5)常用配置

   C: ginxconf ginx.conf,使用自己定义的conf文件如my.conf,命令为nginx -c confmy.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:programsApache2htdocs shirt; 
   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:programsApache2htdocs shirt;
   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:wwwyourwebapp" /applications=yourdomain.com:/:. /multiplex=True

  最后执行运行 Nginx 服务器,我们的 ASP.Net 程序就能脱离 IIS。

免责声明:文章转载自《windows下nginx安装、配置与使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇对actuator的管理端点进行ip白名单限制(springBoot添加filter)关于Linux下面msyql安装后并未设置初始密码,但是登录报错“Access denied for user 'root'@'localhost' (using password: NO)”的解决方案下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Nginx没有启动文件、nginx服务不支持chkconfig、nginx无法自启

Nginx没有启动文件、nginx服务不支持chkconfig、nginx无法自启 问题描述: Nginx安装后,当想要设置Ngixn为开机启动时, 就需要把nginx的启动命令路径放到/etc/rc.d/rc.local文件里面。 这个时候就会出现几个问题: (1)目录中找不到nginx的启动命令文件。 (2)目录中找到了,但是使用chkconfig n...

Spring Cloud Zuul性能调整

Spring Cloud 版本: Dalston.SR5 这两天通过JMeter测了一下Spring Cloud Zuul的性能,用的是两台虚机8核8G和4核8G,宿主机是10核逻辑20核,代理的服务简单的返回字符串hello,vm堆内存1G够用 先说一下测试情况,值得一提的是测试并不严谨,因为用的是虚机,并且虚机上还跑了一些其它的东西,所以不能作为最终指...

Nginx高并发简单配置

https://www.cnblogs.com/sunjianguo/p/8298283.html 停用除SSH外的所有服务,仅保留nginx,优化思路主要包括两个层面:系统层面+nginx层面。 1、调整同时打开文件数量 ulimit -n 20480 2、TCP最大连接数(somaxconn) echo 10000 > /proc/sys/net...

docker一键搭建Nginx+PHP环境(含自动部署命令)

文章的主要部分是一步一步的教程,文章的最后是我整理好的一键安装命令,自动下载并安装docker,构建镜像,启动容器集群(压缩包内注释覆盖范围达到80%) 大家可以看完教程亲自尝试下,也可以直接执行一键安装命令,整个过程大概10分钟左右,我在四台不同的机器上执行过该命令,由于网络原因,5-15分钟不等。 如本文章内容与通过一键安装下载的不同,以一键安装的为...

Nginx实战-后端应用健康检查

原文链接:http://nolinux.blog.51cto.com/4824967/1594029?utm_source=tuicool&utm_medium=referral 公司前一段对业务线上的nginx做了整理,重点就是对nginx上负载均衡器的后端节点做健康检查。目前,nginx对后端节点健康检查的方式主要有3种,这里列出: 1 2...

nginx开启代理后,出现net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK)错误

使用nginx开启代理后,部分js/css文件出现net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK)错误,页面加载错误 nginx代理后,css文件出现net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK)错误,导致页面的样式都乱了。 折磨了我好半天,后来发现不知道哪位兄台,改了nginx里面某些...