Nginx配置负载均衡

其实nginx配置负载均衡就相当于反向代理,你不知到那个服务器给你提供资源你只是知道代理服务器。

正向代理:是你知道你要访问的资源,例如 你访问youtuber你发出请求vpn把你的请求拦截然后再去通过他的服务器请求给你返回数据这是正向代理。

反向代理nginx配置

   upstream tomcat{
  
        server  127.0.0.1:8083;
    }

    #配置自己的项目,如果是vue的需要是vue编译之后的文件
     server {
            listen       8080;#监听端口
            server_name  localhost;#域名

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
               proxy_pass http://tomcat;
            }


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

        }

在配置文件中加入一个upstream,upstream可以认为是对新增server的管理

在location中增加一个proxy_pass,顾名思义它就是代理转发,通过location路由规则进入的服务会转发到proxy_pass配置的服务。

负载均衡

负载均衡它是通过分发多个客户端的请求,到多个服务器的方式来缓解服务器端的压力。

upstream

upstream中可以配置多个代理服务器,并设置分发到各服务器的策略,默认的策略是轮询,并且如果其中有一个down掉,会自动将它剔除。

 upstream tomcat{
        server  127.0.0.1:8081;
        server  127.0.0.1:8083;
    }

常用的负载均衡策略有4种:配置权重(weight),访问ip(ip_hash),fair(fair我没有测出来).

配置权重就是设置每个服务器的轮询几率

 upstream tomcat{
  
    server  127.0.0.1:8081  weight=1;
    server  127.0.0.1:8083  weight=3;
      
    }

根据访问ip的hash算法结果来分配,这样保证每个ip访问到的都是固定server(这样也不会存在session共享问题)

 upstream tomcat{
       ip_hash;
        server  127.0.0.1:8081;
        server  127.0.0.1:8083;
    }

fair(按照服务器的相应时间)

upstream tomcat{
        fair;
     server  127.0.0.1:8081;
        server  127.0.0.1:8083;
} 

nginx的完全配置

#user  nobody; #nginx使用用户
worker_processes  1; #设置值和CPU核心数一致

#日志位置和日志级别
#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;
#server虚拟主机的配置
    server {
        listen       80;#监听端口
        server_name  localhost;#域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html; #站点目录
            index  index.html index.htm;
        }

        #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;
        #}
    }
    upstream tomcat{
     #ip_hash;
    #server  127.0.0.1:8081  weight=1;
    # server  127.0.0.1:8083  weight=3;
        server  127.0.0.1:8081;
        server  127.0.0.1:8083;
    }

    #配置自己的项目,如果是vue的需要是vue编译之后的文件
     server {
            listen       8080;#监听端口
            server_name  localhost;#域名

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
               proxy_pass http://tomcat;
            }


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

        }

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

}
View Code

 

注: proxy_pass也就是替换了127.0.0.1:8081或127.0.0.1:8083 

  例如之前8081端口 之前请求路径是127.0.0.1:8081/user会获取到user值 现在你只需要 http://127.0.0.1:8080/user就能访问       8080是nginx服务器端口

  

posted @ 2020-02-12 22:11  Angry-rookie  阅读(108)  评论(0)    收藏  举报