美文网首页Nginx高端成长之路
windows搭建nginx负载均衡

windows搭建nginx负载均衡

作者: 清远_03d9 | 来源:发表于2019-06-19 19:49 被阅读2次

    1. 软件介绍

    软件 版本 类型 下载地址
    Nginx 1.8.1 压缩包 http://nginx.org/en/download.html

    2. 实现功能:负载均衡,反向代理

    图片1.png 图片4.png 图片5.png

    3. 安装说明

    1. 从官网下载压缩包进行解压

    2.该文档解压目录为E盘,解压后的目录为E:\nginx-1.8.1

    3.修改E:\nginx-1.8.1\conf\nginx.conf配置文件(警告:打开该文件不要用记事本打开),内容如下:

    图片6.png

    多台服务器以ip+端口的形式写入upstream tomcat{},weight表示权重,即nginx将任务进行分配时,将更多的任务分配给权重较大的服务器。backup表示备用,即如果有两台服务器,其中一台作为备用服务器,当另外一台服务器挂了之后,会启动备用服务器。
    格式如下:server ip:端口 weight=2;

    4.常用命令(操作命令需在nginx的根目录执行,即E:\nginx-1.8.1)

    启动nginx:start nginx;
    
    重新加载nginx: nginx.exe –s reload;
    
    安全退出nginx:nginx.exe –s quit;
    
    强制退出nginx:nginx.exe –s stop;
    
    查看端口:netstat -ano|findstr 80(该命令为查看80端口);
    

    5.配置文件内容( 其他参数配置请参考 )

    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include mime.types;#文件扩展名与文件类型映射表
        default_type application/octet-stream;#默认文件类型,默认为text/plain
        sendfile on;#允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
        keepalive_timeout  65;#连接超时时间,默认为75s,可以在http,server,location块
        #gzip  on;
        upstream tomcat {  
            server 120.79.14.152:8085 weight=2; 
            server 127.0.0.1:8085 weight=4; #这个可以配置权重,权重越大,访问该服务器的机率就越大 
            server 120.79.14.152:8086 backup;       
        } 
        server {
            listen 80;#监听端口
            server_name test.cdmtn.com;#监听地址
            location / {
                root html;
                index /tpl/index/login.html;
                proxy_pass  http://tomcat;#可通过域名访问
                proxy_redirect  default;
                proxy_connect_timeout 1; #单位为秒 连接的超时时间。 我设置成1,表示是1秒后超时会连接到另外一台服务器
                proxy_send_timeout 1; #这个指定设置了发送请求给upstream服务器的超时时间。超时设置不是为了整个发送期间,而是在两次write操作期间。如果超时后,upstream没有收到新的数据,nginx会关闭连接
                proxy_read_timeout 1; #该指令设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应。这个时间不是获得整个response的时间,而是两次reading操作的时间
            }
            error_page 500 502 503 504  /50x.html;#错误页
        }
    }
    

    相关文章

      网友评论

        本文标题:windows搭建nginx负载均衡

        本文链接:https://www.haomeiwen.com/subject/ygklqctx.html