windows下使用nginx

起步

下载安装

下载 http://nginx.org/download/

进入根目录运行 
start nginx

如果报错或者无法启动应该是80端口被占用,设置nginx.conf文件将80端口换成其他端口,我换成了8085

访问 http://localhost:8085/,可以看到nginx的欢迎界面

将nginx安装成windows服务

步骤1,下载winsw工具 http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/

步骤2,我下载的winsw是winsw-2.1.2-bin.exe,将其放置于nginx根目录改名为nginxsvc.exe

步骤3,创建nginxsvc.xml置于nginx根目录,写入如下配置(注:国内外网站的配置文件都有问题,写如下配置文件直接跑通)
    <service>
        <id>nginx</id>
        <name>nginx</name>
        <description>nginx</description>
        <executable>C:\nginx\nginx.exe</executable>
        <logpath>C:\nginx\logs</logpath>
        <logmode>roll</logmode>
        <depend></depend>
        <startargument>-pc:\nginx</startargument>
        <stopexecutable>c:\nginx\nginx.exe</stopexecutable>
        <stopargument>-s</stopargument>
        <stopargument>stop</stopargument>
    </service>
步骤4,运行 nginxsvc.exe install 安装服务
步骤5,到服务界面启动服务
步骤6,修改服务登录用户,点击服务->属性,将服务登录用户修改为当前电脑用户  
    如果不这么做,nginx的命令你都没法执行

常用命令

nginx -s stop                           暴力的停止nginx
nginx -s quit                           优雅的停止nginx 
nginx -s reload                         重新加载conf配置
nginx -s reopen                         重新打开配置文件
taskkill /f /pid  28544                 结束进程 
tasklist /fi "imagename eq nginx.exe"   查看nginx进程
nginx -c ./conf/nginx.conf              指定配置文件开启nginx

构建服务

静态服务

进入nginx.conf添加如下配置

server {
    listen 10000;
    server_name  localhost;
    location /sjl/ {
        root   data;
        index  index.html index.htm;
    }
}

此配置告知nginx,当有请求 http://localhost:10000/sjl/

访问nginx根目录 data/sjl/index.html

代理服务器

server {
    listen 10000;
    server_name  localhost;
    location /sjl/ {
        root   data;
        index  index.html index.htm;
    }
}

server {
    listen 10001;
    root   data;
    server_name  localhost;

    location / {
        proxy_pass http://localhost:10000/sjl/;
    }
}

监听 http://localhost:10001 

将访问http://localhost:10001代理到http://localhost:10000/sjl/

也就是说你访问http://localhost:10001实际上是访问http://localhost:10000/sjl/

http配置文件转移

为了配置清晰明朗,在你的nginx.conf的http模块中

http {
    ...
    include       selfconf/my.conf;
    ...
}

在nginx.conf同级目录下创建selfconf/my.conf文件

在my.conf中直接填写server配置即可

负载均衡

负载均衡配置

我没有那么多电脑,所以就用nginx做了多个server的模拟

// 均分请求到下面三个模拟服务器
upstream localhost {
    server 127.0.0.1:10001;
    server 127.0.0.1:10002;
    server 127.0.0.1:10003 backup;  // 这个是备用服务器,当前面两个搞不定了,再分配到此处
}

server {
    listen 10000;
    server_name  127.0.0.1;
    location / {
        proxy_pass http://localhost;
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

// 下面模拟三个服务器
server {
    listen 10001;
    server_name  127.0.0.1;
    location / {
        root   data/sji/;
        index  index.html index.htm;
    }
}

server {
    listen 10002;
    server_name  127.0.0.1;
    location / {
        root   data/sjl/;
        index  index.html index.htm;
    }
}

server {
    listen 10003;
    server_name  127.0.0.1;
    location / {
        root   data/sjm/;
        index  index.html index.htm;
    }
}

负载均衡方法

第一种:Round Robin,默认方式请求权重均分

    upstream localhost {
        server 127.0.0.1:10001 weight=5; // 权重为5
        server 127.0.0.1:10002 max_fails=3 fail_timeout=30s; // 设置最大重连次数为3次,和最大不可用时长30s
        server 127.0.0.1:10003 backup; // 备用服务器
    }
第二种:Least Connections,将请求分配给连接数最少的服务器

    upstream localhost {
        least_conn;
        server 127.0.0.1:10001;
        server 127.0.0.1:10002;
        server 127.0.0.1:10003 backup;
    }
第三种,IP Hash,相同的ip访问都会被分配到同一个服务器

    upstream localhost {
        ip_hash;
        server 127.0.0.1:10001;
        server 127.0.0.1:10002;
        server 127.0.0.1:10003 down; // 此服务器不参与分配
    }
第四种,Generic Hash,可以自定义检测key

    key相同则负载到同一台服务器上

    upstream localhost {
        hash $request_uri consistent;
        server 127.0.0.1:10001;
        server 127.0.0.1:10002;
        server 127.0.0.1:10003;
    }

    $request_uri    表示的是http url后边的uri
    $args           查询参数
    $remote_addr    客户端的IP
    $remote_port    客户端的端口
    ... 类似的还有很多

正式线上的负载均衡配置

// ipAdress 这是你的服务器IP地址

// proxyAdress 这是你的代理服务器IP地址

// 假设你的后台api接口为 ipAdress:9000/test 和 ipAdress:9001/test 可以使用如下方式进行负载均衡

// 通过你的代理服务器 proxyAdress:10000/test 即可负载到上面两个服务器对应的test api

upstream ipAdress {
    server ipAdress:9000;
    server ipAdress:9001;
}

server {
    listen 10000;
    
    server_name  proxyAdress;

    location /test/ {
        proxy_pass http://ipAdress;
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}