FastDFS蛋疼的集羣和負載均衡(十四)之Nginx+Tomcat負載均衡

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=JAVAHOME/lib: JAVA_HOME/jre/lib
PATH=JAVAHOME/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負載均衡!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章