zoukankan      html  css  js  c++  java
  • nginx在windows下配置负载均衡

    首先准备nginx的安装包。

    nginx下载地址:http://nginx.org/en/download.html

    当前最新稳定版本是 nginx-1.18.0

     第二步修改 nginx配置文件。

    解压nginx下载包,进入nginx解压包的 conf 文件夹,修改nginx.conf文件.

    比如nginx的根文件夹为 E: ginx-1.18.0  ,那么修改E: ginx-1.18.0conf ginx.conf 文件

    默认配置如下:

    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
        keepalive_timeout  65;
    
        server {
            listen       820;
            server_name  localhost;
    
            location / {
               root   html;
              index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }

    在上面的默认配置中,修改两个地方:

    1. 新增upstream配置节点,节点名称为webapi2

        webapi2节点里配置三个站点url,分别为 localhost:801, localhost:802, localhost:803

    2. 修改location配置节点,

       删除location节点里的内容。

       然后在location节点中新增proxy_pass节点,将proxy_pass的值指向名称为webapi2的upstream节点。

    修改后的配置如下:

    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
        keepalive_timeout  65;
    
        upstream webapi2{
            server localhost:801 weight=1;
            server localhost:802 weight=1;
            server localhost:803 weight=1;
        }
    
        server {
            listen       820;
            server_name  localhost;
    
            location / {
                proxy_pass http://webapi2;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }

    然后进入E: ginx-1.18.0  目录,打开cmd命令窗口,运行 nginx.exe 命令

    如果80端口被占用,启动 nginx 会失败,出现下面的错误:

    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)

     upstream节点里的 weight 参数表示权重,数值越大表示权重越高。

    这个时候修改一下监听端口,将 server 节点里的 listen修改为别的端口试试,比如修改成820然后再次启动nginx

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

    然后再次启动nginx,

    在cmd命令行窗口中输入nginx.exe  回车

     如果启动成功,会出现下面的效果,没有任何提示,

    为了确保nginx是否已经成功启动,可以查看任务管理器是否有nginx进程。

     然后在浏览器地址栏输入 http://localhost:820/  验证是否可以打开网站。

  • 相关阅读:
    linux 短信收发
    sama5d3 环境检测 adc测试
    【Codeforces 723C】Polycarp at the Radio 贪心
    【Codeforces 723B】Text Document Analysis 模拟
    【USACO 2.2】Preface Numbering (找规律)
    【Codeforces 722C】Destroying Array (数据结构、set)
    【USACO 2.1】Hamming Codes
    【USACO 2.1】Healthy Holsteins
    【USACO 2.1】Sorting A Three-Valued Sequence
    【USACO 2.1】Ordered Fractions
  • 原文地址:https://www.cnblogs.com/yyee/p/12826221.html
Copyright © 2011-2022 走看看