01-Nginx 实现负载均衡

随着互联网不断发展,单应用单服务器有时候很难满足需求。比如京东、淘宝在面临双11的时候,处理程序肯定不会是一台服务器。那么Nginx 可以实现多服务器共同分担服务器压力问题。

Nginx可以做正向和反向代理。但是实际工作中。应用反向代理的较多。Nginx 地址

一、启动Nginx

你可以使用DOS 命令符启动服务,也可以直接双击 Nginx.exe 直接启动服务

启动服务:start nginx

退出服务:nginx -s quit

强制关闭服务:nginx -s stop

重新载入服务:nginx -s reload (比如配置文件发生的改变,使用此命令,服务不会停止,是新的配置生效)

验证配置文件:nginx -t

使用配置文件:nginx -c "配置文件路径"

使用帮助:nginx -h

二、通过 修改 配置 访问 webapi

01、新建 WebApi 项目,在IIS 上部署,地址:localhost:8011

02、修改配置文件

 

 

 03、这时候在浏览器上输入 localhost:8034 就会转发到 localhost:8011服务器上

三、多服务器实现负载均衡

01、轮询策略

之前我建立了一个端口号8011的api,现在新建8012、8013端口号的api,实现负载均衡。

修改配置文件

 

 

 这样配置实际上实现了简单的轮询策,记住 upstream 取得名字 webapi 那么 proxy_pass 中名称要对应,这样 通过访问 localhost:8034 找到 http://wenpai -> upstream 中服务地址列表

02、权重策略

You can you up,你行你来,在负载均衡的服务器中,由于你的性能比较优秀,那么你可以多承担一点访问量。这时候你可以使用权重策略。只要在 其后加 weight=1(默认) weight (小写) 即可

 

 

 补充:可以在加其他参数:

down 表示当前的server不参与负载

weight 默认1,weight 值越大,表示负载的权重越大

max_fails 允许请求失败的次数默认1,当超过最大次数时,返回 proxy_next_upstream 模块定义的错误

fail_timeout max_fails 次失败后,暂停的时间

backup 其他所有的非backup机器down或者忙的时候,请求backup机器,所以这台机器压力最轻松。

03、ip_hash 策略

 

 

  完整的配置信息:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	
	upstream webApi {
		ip_hash; #表示使用IPHash策略
		server localhost:8011 weight=6;
		server localhost:8012;
		server localhost:8013;
	}

    server {
        listen       8034;
        server_name  8034.max.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://webApi;
        }

        #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;
    #    }
    #}

}

  

 

posted @ 2020-08-09 18:08  delaywu  阅读(297)  评论(0编辑  收藏  举报