windows下搭建nginx负载均衡

1.下载Windows版本的Nginx 

http://nginx.org/en/download.html

  2.解压Nginx包,配置conf文件下的nginx.conf文件

 

 

3.配置说明:

#user  nobody;
#N工作进程数,默认为1
worker_processes  1;

#错误日志保存路径
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid文件路径
#pid        logs/nginx.pid;

#工作模式及连接数上限
events {
    #单个后台worker process进程的最大并发链接数,默认1024,可以按照最大客户端连接数max_clients配置
    #nginx作为http服务器的时候:
    #max_clients = worker_processes * worker_connections

    #nginx作为反向代理服务器的时候:
    #max_clients = worker_processes * worker_connections/4
    worker_connections  1024;
}

#http服务器配置,做web服务器或反向代理
http {
    include       mime.types;#文件扩展名与类型映射表
    default_type  application/octet-stream;#默认文件类型

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #日志文件路径
    #access_log  logs/access.log  main;

    sendfile        on;#开启高效传输模式
    
    #在nginx中,tcp_nopush配置与tcp_nodelay“互斥”。它可以配置一次发送数据包的大小。
    #也就是说,数据包累积到一定大小后就发送。
    #在nginx中tcp_nopush必须和sendfile配合使用。
    #tcp_nopush     on;

    #连接超时时间 单位是秒
    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    #启用gzip压缩
    #gzip  on;

    upstream web70{ #有域名的可以用域名
        #负载的一个实例,可以是外网IP
        #默认轮询,把每个请求按顺序逐一分配到不同的server,如果server挂掉,能自动剔除。

        #其他配置:
        #1.在负载的实例后面加weigth参数,表示权值,权值越高被分配到的几率越大
        #譬如server 127.0.0.1:71 weight=1;   

        #2.把请求分配到连接数最少的server
        #least_conn;

        #3.每个请求会按照访问ip的hash值分配,这样同一客户端连续的Web请求都会被分发到同一server进行处理
        #可以解决session的问题。如果server挂掉,能自动剔除。
        #ip_hash;

        server 127.0.0.1:71; #weight=1;   
        server 127.0.0.1:72; #weight=1; 
    }

    #对外web服务器实例
    server {
        listen       70;#监听端口
        server_name  localhost;#服务器名,默认localhost
        
        #默认字符编码,默认koi8-r,可改为utf-8
        #charset koi8-r;
        charset utf-8;
        
        #本服务器实例日志路径
        #access_log  logs/host.access.log  main;

        #默认请求配置
        location / {
            root   html;#web服务器根目录,用来放网站程序的目录
            index  index.html index.htm;#默认首页
            proxy_pass  http://web70; #请求转向的url地址,反向代理则填写对应upstream后面的域名
            proxy_redirect  default; 
        }
        
        #错误页面
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

4.打开控制台进入Nginx目录,输入nginx.exe命令来启动Nginx

  

 5.打开任务管理器可以看到Nginx已经在运行了,此时控制台也可以关闭了

 

6.配置负载均衡web实例

  

 7.然后刷新Nginx的服务器,显示第一个负载的web实例

   8.再刷新下,显示第二个负载的web实例

  Nginx其他命令:

nginx.exe -s stop     //停止nginx
nginx.exe -s reload  //重新加载nginx
nginx.exe -s quit     //退出nginx

 Nginx配置文件下下载 

posted on 2020-11-02 11:21  心所欲  阅读(933)  评论(0编辑  收藏  举报

导航