前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >FastDFS蛋疼的集群和负载均衡(十四)之Nginx+Tomcat负载均衡

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

作者头像
用户2032165
发布2018-06-05 18:29:11
5760
发布2018-06-05 18:29:11
举报

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目录,添加两个虚拟主机。配置如下:

代码语言:javascript
复制
        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目录,添加两个虚拟主机。配置如下:

代码语言:javascript
复制
        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,具体如下:
代码语言:javascript
复制
          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负载均衡!

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.01.04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Interesting things
  • What did you do today
    • 什么是虚拟主机
      • 基于域名的虚拟主机
        • html目录创建
          • 配置虚拟主机
            • 基于端口的虚拟主机
              • Nginx负载均衡
              • Summary
              相关产品与服务
              轻量应用服务器
              轻量应用服务器(TencentCloud Lighthouse)是新一代开箱即用、面向轻量应用场景的云服务器产品,助力中小企业和开发者便捷高效的在云端构建网站、Web应用、小程序/小游戏、游戏服、电商应用、云盘/图床和开发测试环境,相比普通云服务器更加简单易用且更贴近应用,以套餐形式整体售卖云资源并提供高带宽流量包,将热门软件打包实现一键构建应用,提供极简上云体验。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
              http://www.vxiaotou.com