CentOS7下搭建Nginx实现负载均衡

    基本前提条件:安装好jdk,配置JAVA_HOME,CentOS7下再安装了gcc之后便可安装Nginx了。过程非常简单。

    下面记录开始:

    1.创建目录nginx,将nginx安装包上传至该文件夹

    mkdir /usr/local/nginx

    2.解压安装包

    tar -zxvf nginx-1.12.2.tar.gz

    3.进入解压后的安装包中运行configure命令(该命令后面可以指定参数,详见 --help)

    cd /usr/local/nginx/nginx-1.12.2/
    ./configure

    4.默认不指定参数的话在/usr/local/nginx/目录下生成如下目录结构

        CentOS7下搭建Nginx实现负载均衡_第1张图片

    5.在nginx目录下即可启动nginx

    ./sbin/nginx

    6.在防火墙关闭的情况下,windows访问虚拟机即可看到nginx启动成功页面

    systemctl stop firewalld.service #停止firewall
    systemctl disable firewalld.service #禁止firewall开机启动

CentOS7下搭建Nginx实现负载均衡_第2张图片


下面再在CentOS7中装两个或者两个以上的tomcat(我这里装了4个),修改tomcat解压目录下的server.xml中的3个端口号,保证不冲突就行  apache-tomcat-7.0.81.tar.gz

CentOS7下搭建Nginx实现负载均衡_第3张图片

启动4个tomcat,脚本如下:

startAll.sh

/usr/local/tomcat/tomcat01/apache-tomcat-7.0.81/bin/startup.sh
/usr/local/tomcat/tomcat02/apache-tomcat-7.0.81/bin/startup.sh
/usr/local/tomcat/tomcat03/apache-tomcat-7.0.81/bin/startup.sh
/usr/local/tomcat/tomcat04/apache-tomcat-7.0.81/bin/startup.sh

再编辑nginx的配置文件(本例中配置文件在  /usr/local/nginx/conf/ 下)

vi /usr/local/nginx/conf/nginx.conf

也用ssh工具编辑器编辑,内容如下:

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

    #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;
        #}
    #}
	
	server {
		listen 80;	
		server_name 192.168.56.101;	
		location / {
			root html;
			index index.html index.htm;
		}
		error_page 404 /404.html;
		error_page 500 502 503 504 /50x.html;
			location = /50x.html {
			root html;
		}
	}
	
	server {
		listen 9090;	#通过端口号来区分
		server_name 192.168.56.101;	
		location / {
			root /web1;
			index index.html index.htm;
		}
		error_page 404 /404.html;
		error_page 500 502 503 504 /50x.html;
			location = /50x.html {
			root html;
		}
	}
	
	server {
		listen 9091;	#通过端口号来区分
		server_name 192.168.56.101; 
		location / {
			root /web2;
			index index.html index.htm;
		}
		error_page 404 /404.html;
			location = /50x.html {
			root html;
		}
	}

	
	server {
		listen 6060;
		server_name www.web1.com;	#通过域名来区分
		location / {
			root /web1;
			index index.html index.htm;
		}
		error_page 404 /404.html;
		error_page 500 502 503 504 /50x.html;
			location = /50x.html {
			root html;
		}
	}
	
	server {
		listen 6060;
		server_name www.web2.com; #通过域名来区分
		location / {
			root /web2;
			index index.html index.htm;
		}
		error_page 404 /404.html;
			location = /50x.html {
			root html;
		}
	}
	
	
	#设置轮询或者权重来访问服务器
	
	upstream tomcat01{
		server 192.168.56.101:8080 weight=2;	#权重默认都是1(默认状态是轮询)
		server 192.168.56.101:8082 weight=2;
		server 192.168.56.101:8083 weight=5;
	}
	upstream tomcat02{
		server 192.168.56.101:8081 weight=2;
		server 192.168.56.101:8082 weight=2;
		server 192.168.56.101:8083 weight=5;
	}
	server {
		listen 80;	
		server_name www.web1.com;	
		location / {
			proxy_pass http://tomcat01;
			index index.html index.htm;
		}
		error_page 404 /404.html;
		error_page 500 502 503 504 /50x.html;
			location = /50x.html {
			root html;
		}
	}
	server {
		listen 80;	
		server_name www.web2.com;	
		location / {
			proxy_pass http://tomcat02; 
			index index.html index.htm;
		}
		error_page 404 /404.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       8080;
    #    server_name  localhost;

    #    location / {
    #        root /web1;
    #        index  index.html index.htm;
    #    }
    #}
	#server {
    #    listen       8080;
    #    server_name  localhost;

    #    location / {
    #        root /web2;
    #        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;
    #    }
    #}

}

具体参数含义可自行百度

然后可以在不停nginx的情况下重新加载配置文件

/usr/local/nginx/sbin/nginx -s reload

然后创建目录和文件来测试

[root@centos7-vm nginx]# mkdir /web1
[root@centos7-vm nginx]# mkdir /web2
[root@centos7-vm nginx]# echo "192.168.56.101--web1" > /web1/index.html
[root@centos7-vm nginx]# echo "192.168.56.101--web2" > /web2/index.html

在windows下分别访问9090和9091端口

CentOS7下搭建Nginx实现负载均衡_第4张图片

CentOS7下搭建Nginx实现负载均衡_第5张图片


在windows的hosts文件中新增

192.168.56.101 www.web1.com
192.168.56.101 www.web2.com

再访问www.web1.com

8082

CentOS7下搭建Nginx实现负载均衡_第6张图片


8080

CentOS7下搭建Nginx实现负载均衡_第7张图片


8083

CentOS7下搭建Nginx实现负载均衡_第8张图片


访问www.web2.com,一样交替访问配置文件中配置的3个不同的server。

到此,安装nginx实现负载均衡的实例已完成。




你可能感兴趣的:(随记)