FastDFS蛋疼的集群和负载均衡(十四)之Nginx+Tomcat负载均衡

288 阅读2分钟

diary_report.jpg
###Interesting things

今天来配置一下Nginx+Tomcat负载均衡环境。

image.png

###What did you do today ####什么是虚拟主机

虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响。如下图:

image.png

通过nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置

1.基于ip的虚拟主机 2.基于域名的虚拟主机 3.基于端口的虚拟主机

####基于域名的虚拟主机

  • ashin.mayday.com和monster.mayday.com都指向同一台nginx服务器(192.168.12.5),用户访问不同的域名显示不同的网页内容。

  • 修改hosts文件(C:\Windows\System32\drivers\etc\hosts),指定ashin.mayday.com和monster.mayday.com对应的虚拟机。

    image.png

####html目录创建

在192.168.12.5上创建/usr/local/html/ashin_html,此目录为ashin.mayday.com域名访问的目录

在192.168.12.5创建/usr/local/html/monster_html,此目录为monster.mayday.com域名访问的目录

我们把/usr/local/nginx/html/index.html 拷贝到 /usr/local/html/ashin_html 和 /usr/local/html/monster_html目录下,然后做一些个性化修改。

####配置虚拟主机 修改/usr/local/nginx/conf/nginx.conf目录,添加两个虚拟主机。配置如下:

        server {
                listen  192.168.12.5:80;
                server_name     ashin.mayday.com;

                location / {
                        root /usr/local/ashin_html;
                        index index.html index.htm;
                }

        }


        server {
                listen  192.168.12.5:80;
                server_name     monster.mayday.com;

                location / {
                        root /usr/local/monster_html;
                        index index.html index.htm;
                }

        }

  • 访问ashin.mayday.com和monster.mayday.com,美滋滋大功告成!
    image.png

image.png

####基于端口的虚拟主机

nginx对外提供80和8080两个端口监听服务。请求80端口则请求80_html目录下的index.html,请求8080端口则请求8080_html目录下的index.html

修改/usr/local/nginx/conf/nginx.conf目录,添加两个虚拟主机。配置如下:

        server {
                listen 8080;
                server_name     192.168.12.5;

                location / {
                        root /usr/local/8080_html;
                        index index.html index.htm;
                }

        }

    server {
        listen       80;
        server_name  192.168.12.5;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/local/80_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;
        #}
    }
  • 防火墙添加8080端口策略

    image.png

  • 启动nginx,查看端口监听状态

netstat -an|grep 80

image.png

  • 访问192.168.12.5:80 和 192.168.12.5:8080
    image.png
    image.png

####Nginx负载均衡

负载均衡,建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、添加吞吐量、加强网络数据处理能力、提高网络的灵活性和可靠性。

nginx负载均衡服务器:192.168.12.5 tomcat1服务器:192.168.12.6 tomcat2服务器:192.168.12.7

  • 对192.168.12.6和192.168.12.7里的apache-tomcat-8.0.48.tar.gz进行解压,在此之前我们需要安装java环境。

Java环境配置

1.在/usr/local/目录,创建java目录,把jdk-8u151-linux-x64.tar.gz解压到/usr/local/java 2.为java配置本地环境变量。 vim /etc/profile, 添加如下内容: JAVA_HOME=/usr/local/java/jdk1.8.0_151 JRE_HOME=/usr/local/java/jdk1.8.0_151/jre CLASSPATH=JAVA_HOME/lib:JAVA_HOME/jre/lib PATH=JAVA_HOME/bin:PATH export PATH CLASSPATH JAVA_HOME

image.png
3.使配置生效source /etc/profile 4.测试java环境是否配置成功。
image.png

  • 我们测试启动192.168.12.6和192.168.12.7的tomcat,记得在防火墙添加8080端口策略。

    image.png
    image.png
    image.png

  • 我们进入/webapps/ROOT/目录下,修改index.jsp,进行个性化设置。

  • 我们在192.168.12.5中修改nginx.conf,具体如下:

          upstream tomcat_server_pool {
                server 192.168.12.6:8080 weight=10;
                server 192.168.12.7:8080 weight=10;
        }

    server {
        listen       192.168.12.5:80;
        server_name  ashin.mayday.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://tomcat_server_pool;
            index  index.jsp 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;
        #}
    }
  • 开启nginx,第一次访问ashin.mayday.com

    image.png

  • 第二次访问ashin.mayday.com

    image.png


###Summary

以上就实现了Nginx+Tomcat负载均衡!