騰訊云:Nginx+Tomcat+SSL證書搭建高性能負載均衡集群

準備:

一边锁、環(huán)境

jdk-1.8
nginx-1.12.1
tomcat-7.0.79
jdk與nginx版本
1姑食、防火墻開通80和443端口,參看CentOS7.x之防火墻
2茅坛、騰訊云安全組開放80和443端口
騰訊云服務器安全組開放80個443端口

二音半、JDK1.8安裝

參見騰訊云服務器:CentOS安裝MySQL、JDK贡蓖、Tomcat及web項目發(fā)布外網(wǎng)實戰(zhàn)中jdk安裝部分

三曹鸠、Tomcat安裝

#如果沒有此目錄,則創(chuàng)建
cd /usr/local/tomcat
#下載tomcat-7.0.79
wget http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.79/bin/apache-tomcat-7.0.79.tar.gz
#解壓并重命名分別為tomcat-7.0.79_01斥铺、tomcat-7.0.79_02(server.conf中的端口下文有詳細配置)
tar -xzvf apache-tomcat-7.0.79.tar.gz

四彻桃、Web項目

1、tomcat-7.0.79_01中的WebProject項目index.jsp內(nèi)容

WebProject01中的jsp內(nèi)容

tomcat-7.0.79_01中的server.conf配置如下

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1" URIEncoding="UTF-8"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the BIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
      <Context path="" docBase="/usr/local/tomcat/tomcat7/tomcat-7.0.79_01/webapps/WebProject" reloadable="true" crossContext="true" />
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
  </Service>
</Server>

2晾蜘、tomcat-7.0.79_02中的WebProject項目index.jsp內(nèi)容

WebProject02中的jsp內(nèi)容

tomcat-7.0.79_02中的server.conf中的配置如下

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8006" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8081" protocol="HTTP/1.1" URIEncoding="UTF-8"
               connectionTimeout="20000"
               redirectPort="8444" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the BIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
      <Context path="" docBase="/usr/local/tomcat/tomcat7/tomcat-7.0.79_02/webapps/WebProject" reloadable="true" crossContext="true" />
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

3邻眷、server.conf配置不同之處有四

#1_01
<Server port="8005" shutdown="SHUTDOWN">

#1_02
<Server port="8006" shutdown="SHUTDOWN">
#2_01
<Connector port="8080" protocol="HTTP/1.1" URIEncoding="UTF-8"
               connectionTimeout="20000"
               redirectPort="8443" />

#2_02
<Connector port="8081" protocol="HTTP/1.1" URIEncoding="UTF-8"
               connectionTimeout="20000"
               redirectPort="8444" />

#3_01
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
#3_02
<Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />
#4_01
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
      <Context path="" docBase="/usr/local/tomcat/tomcat7/tomcat-7.0.79_01/webapps/WebProject" reloadable="true" crossContext="true" />

#4_02
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
      <Context path="" docBase="/usr/local/tomcat/tomcat7/tomcat-7.0.79_02/webapps/WebProject" reloadable="true" crossContext="true" />

說明:

  • 將WebProject項目在Eclipse或者MyEclipse中打成war包上傳至tomcat啟動目錄中眠屎,tomcat啟動一次后,會自動解壓war包肆饶,將tomcat關閉后改衩,即可刪除war包
  • 將兩個tomcat分別啟動

五、Nginx安裝

1驯镊、下載葫督、解壓

cd /usr/local/src
wget http://nginx.org/download/nginx-1.12.1.tar.gz   #穩(wěn)定版
tar nginx-1.12.1.tar.gz
cd nginx-1.12.1

2、安裝準備

yum install gcc gcc-c++ -y  #安裝gcc和c++包
yum -y install pcre pcre-devel zlib-devel openssl-devel

3、安裝Nginx

 cd /usr/local/src/nginx-1.12.1
 #安裝到指定目錄
 ./configure --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module
 #編譯
 make && make install
  • --prefix=/usr/local/nginx/ #nginx安裝的根目錄
  • --with-http_stub_status_module #監(jiān)控nginx運行狀態(tài)
  • --with-http_ssl_module #添加ssl模塊

有關nginx參數(shù)詳解洒放,請參考nginx安裝及編譯參數(shù)詳解

4蛉鹿、 nginx.conf參數(shù)配置(圖文)

nginx.conf配置參數(shù)_01
nginx.conf配置參數(shù)_02

xxx.cn全部以你申請的域名替換

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

    #gzip  on;

    
    upstream tomcat7_cluster{
        server localhost:8080 weight=5;
        server localhost:8081 weight=5;
    }
    server {
        listen      80;
        server_name www.xxx.cn;

        access_log logs/jmszwzr.access.log main;
        error_log logs/jmszwzr.error.log;
        
    #rewrite ^(.*) https://$server_name$1 permanent;
    #棄用rewrite,使用return效率更高
    return 301 https://$server_name$request_uri;
        location / {
            proxy_pass http://tomcat7_cluster;
        }
    }

    # HTTPS server
    server {
        listen       443 ssl;
        #server_name  www.xxx.cn;
        server_name  localhost;
        
        #ssl on;    
        access_log logs/ssl.access.log main;
        error_log logs/ssl.error.log;
    
        #為虛擬主機指定pem格式的證書文件
        ssl_certificate      1_www.xxx.cn_bundle.crt;
        #為虛擬主機指定私鑰文件
        ssl_certificate_key  2_www.xxx.cn.key;

        #ssl_session_cache    shared:SSL:1m;
        #客戶端能夠重復使用存儲在緩存中的會話參數(shù)時間
        ssl_session_timeout  10m;

        #指定使用的ssl協(xié)議
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        #指定許可的密碼描述
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        #SSLv3和TLSv1協(xié)議的服務器密碼需求優(yōu)先級高于客戶端密碼
        ssl_prefer_server_ciphers  on;

         location / {
            proxy_pass http://tomcat7_cluster;
        }
    }
}

配置好nginx.conf文件之后往湿,可以進行驗證配置是否正確

配置nginx.conf是否正確
#在nginx根目錄下
./sbin/nginx    #啟動nginx
./sbin/nginx -s reload  #軟重啟

六妖异、瀏覽器訪問

訪問兩個項目的幾率可在nginx中配置

第一次訪問
第二次訪問

結尾:以上只是簡單的將nginx作為反向代理服務器,配以https加密訪問领追,實際情況中他膳,還有很多需要添加的模塊和優(yōu)化的地方。如tomcat集群下的session共享绒窑、nginx優(yōu)化等問題棕孙。

小白初入,如有錯誤些膨,歡迎各位簡友指正蟀俊,在此感謝!


資料來源或參考

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末烫映,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子锭沟,更是在濱河造成了極大的恐慌,老刑警劉巖族淮,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異瞧筛,居然都是意外死亡,警方通過查閱死者的電腦和手機较幌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來乍炉,“玉大人,你說我怎么就攤上這事底循。” “怎么了熙涤?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長祠挫。 經(jīng)常有香客問我悼沿,道長,這世上最難降的妖魔是什么糟趾? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮义郑,結果婚禮上,老公的妹妹穿的比我還像新娘魔慷。我一直安慰自己,他們只是感情好院尔,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布邀摆。 她就那樣靜靜地躺著,像睡著了一般栋盹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上例获,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機與錄音榨汤,去河邊找鬼。 笑死收壕,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的蜜宪。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼圃验,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了澳窑?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤照捡,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后栗精,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡鹿寨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了脚草。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡馏慨,死狀恐怖姑隅,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情讲仰,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站躏啰,受9級特大地震影響,放射性物質發(fā)生泄漏给僵。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一觅玻、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧溪厘,春花似錦、人聲如沸畸悬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至冷冗,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蒿辙,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工思灌, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人泰偿。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像裕照,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子牍氛,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容