nginx在windows平台下的使用笔记

nginx主要提供反向代理及负载均衡的能力,重定向报文代理及报文数据替换也是经常使用功能。(参考http://www.javashuo.com/article/p-nugcehza-ga.htmlhtml

1、经常使用命令

start nginx  nginx

nginx –s stop — fast shutdown 快速中止,可能并不保存相关信息git

nginx –s quit — graceful shutdown 完整有序的中止,并保存相关信息github

nginx –s reload — reloading the configuration file 配置信息修改,需从新载入配置web

nginx –s reopen — reopening the log files 从新打开日志文件windows

nginx –v 查看Nginx版本服务器

注意:nginx的启动有两种方式,即1.start nginx和2.nginx.exe,但第二种cmd窗口一直处于执行状态,没法进行任何命令操做,建议使用方式1.start nginx来启动。app

 

2、辅助命令

tasklist /fi "imagename eq nginx.exe"  查看进程是否启动负载均衡

netstat -aon|findstr "80"  查看80端口ide

tasklist |findstr "pid"  查看PID对应的进程

taskkill /f /t /im 进程名imagename   结束进程

 

3、以windows services方式运行

因ngnix没有提供服务运行的安装方式,因此须要借助以工具Windows Service Wrapper,下载版本http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/

详细使用说明参考https://github.com/kohsuke/winsw/blob/master/doc/installation.md

另外一种方式是利用NSSM,详看http://nssm.cc/

简单使用样例

一、将winsw.exe更名成nginx-service.exe

二、新建nginx-service.exe.config配置文件

<configuration>
  <startup>
    <supportedRuntime version="v2.0.50727" />
    <supportedRuntime version="v4.0" />
  </startup>
  <runtime>
    <generatePublisherEvidence enabled="false"/>
  </runtime>
</configuration>

三、新建nginx-service.xml服务配置文件

<service>
  <id>nginx</id>
  <name>Nginx Service</name>
  <description>High Performance Nginx Service</description>
  <executable>%BASE%\nginx.exe</executable>
  <stopexecutable>%BASE%\nginx.exe -s stop</stopexecutable>
  <workingdirectory>%BASE%\</workingdirectory>
  <startmode>Automatic</startmode>
  <delayedAutoStart/>
  <logpath>%BASE%\logs</logpath>
  <log mode="roll-by-time">
    <pattern>yyyyMMdd</pattern>
  </log>
</service>

四、安装服务

以管理员身份运行cmd,进入ngnix目录,输入:nginx-service.exe install

五、管理服务

打开服务管理器进行服务的启动、中止,或CMD下运行命令 net start/stop nginx来管理

 

4、配置说明

主配置文件nginx.confg分为4部分,main(全局配置)、server(主机配置)、upstream(负载均衡服务器设置)以及location(URL匹配特定位置的设置),这四者的关系是:server继承main,location继承server,upstream既不会继承其它设置也不会被继承

配置项说明

worker_processes  开启的线程数,通常跟逻辑CPU核数一致

upstream  设定负载均衡的服务器列表 支持多组的负载均衡,能够配置多个upstream  来服务于不一样的Server

upstream mysvr {
  #weigth参数表示权值,权值越高被分配到的概率越大  
  #1.down 表示单前的server暂时不参与负载
  #2.weight 默认为1.weight越大,负载的权重就越大。    
  #3.backup: 其它全部的非backup机器down或者忙的时候,请求backup机器。因此这台机器压力会最轻。 
  #server 192.168.31.233  down;
  #server 192.168.31.233  backup;
  server 192.168.31.233:8087  weight=1;
  server 192.168.31.233:8088  weight=2;
}

 

5、配置场景

一、负载平衡,参考http://nginx.org/en/docs/http/load_balancing.html

 

例1

http {

    upstream backend {       
        least_conn;

        server webserver1 weight=1;
        server webserver2:80 weight=4;   
    }


    server {
        listen 80;

        location / {
            proxy_pass http://backend;
           
            # Rewrite the 'Host' header to the value in the client request
            # or primary server name
            proxy_set_header Host $host;

            # Alternatively, put the value in the config:
            #proxy_set_header Host www.example.com;           
        }
    }
}

例2

http {
     # Basic reverse proxy server
     upstream backend  {
           server 127.0.0.1:4433;
     }

     # *:80 -> 127.0.0.1:4433
     server {
            listen       80;
            server_name  example.com;
            ## send all traffic to the back-end
            location / {
                 proxy_pass        http://backend;                 proxy_redirect    off;                 proxy_set_header  X-Forwarded-For $remote_addr;            }    }}