Linux nginx+多tomcat实现负载均衡(集群 宕机)+html

nginx是一个web服务器,类似apache一样,但是比apache性能更好,更快。还可以实现反向代理,和负载均衡,常用于构建web服务集群的负载均衡。今天就来记录一下,nginx的安装与配置,实现负载均衡的作用。

首先,安装nginx
$ wget http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm  
(ps:网上有一些源是不完全的,只是安装了简单的nginx,其他一些依赖并没有一起安装,导致配置文件不 全。请用这里的连接,完整可用。)
$ rpm -ivh epel-release-latest-7.noarch.rpm

$ yum install nginx (直接yum安装)

安装就这么简单方便,安装完成后,就可以使用systemctl来控制nginx的启动了
$ systemctl enable nginx (加入开机启动)
$ systemctl start nginx (开启nginx)

$ systemctl status nginx (查看状态)

然后就是设置nginx的配置文件,实现负载均衡。顾名思义就是将多个请求分发到不同的服务上,实现均衡的负载,减小单个服务的压力。

$ vi /etc/nginx/nginx.conf  (修改配置文件,全局配置文件)

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    # tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    #Nginx upstream的5种权重分配方式Nginx upstream的5种权重分配方式
    #1.轮询(默认) 2.weight 3.ip_hash 4.fair(第三方) 5.url_hash(第三方)
    upstream tomcat { #(tomcat为自定义的负载均衡规则名)        
        # ip_hash; (ip_hash则为ip-hash方法)
        server 39.105.88.56:8080 backup; #(weihgt越大,负载的权重越大)
        server 39.105.88.56:8081 weight=1 ;
        server 39.105.88.56:8082 weight=2 ;

        ## 可以定义多组规则
    }

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;


        location ~\.(html|js|css){#拦截
          root /home/page/;#静态资源html的位置,在home下创建一个文件夹,放的是index.html  jquery-1.11.3.min.js
        }


        location / { #( /  表示所有请求,可以自定义来针对不同的域名设定不同负载规则 和服务)
          proxy_pass    http://tomcat; #(反向代理,填上你自己的负载均衡规则名)
          proxy_redirect off; #(下面一些设置可以直接复制过去,不要的话,有可能导致一些 没法认证等的问题)
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_connect_timeout 90; #(下面这几个都只是一些超时设置,可不要)
          proxy_send_timeout 90;
          proxy_read_timeout 90;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }upstream tomcat { #(tomcat为自定义的负载均衡规则名)        
        # ip_hash; (ip_hash则为ip-hash方法)
        server 39.105.88.56:8080 backup; #(weihgt越大,负载的权重越大)
        server 39.105.88.56:8081 weight=1 ;
        server 39.105.88.56:8082 weight=2 ;

        ## 可以定义多组规则
    }

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;


        location ~\.(html|js|css){#拦截
          root /home/page/;#静态资源html的位置,在home下创建一个文件夹,放的是index.html  jquery-1.11.3.min.js
        }


        location / { #( /  表示所有请求,可以自定义来针对不同的域名设定不同负载规则 和服务)
          proxy_pass    http://tomcat; #(反向代理,填上你自己的负载均衡规则名)
          proxy_redirect off; #(下面一些设置可以直接复制过去,不要的话,有可能导致一些 没法认证等的问题)
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_connect_timeout 90; #(下面这几个都只是一些超时设置,可不要)
          proxy_send_timeout 90;
          proxy_read_timeout 90;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

tomcat的配置就不再多说了

        server 39.105.88.56:8080 backup; #(weihgt越大,负载的权重越大)
        server 39.105.88.56:8081 weight=1 down;
        server 39.105.88.56:8082 weight=2 down;down;
        server 39.105.88.56:8082 weight=2 down;

在后边加上down模拟宕机,这时8080端口就会被启用了

猜你喜欢

转载自blog.csdn.net/APM800/article/details/80321659