Nginx自學手冊(六)Nginx+Tomcat實現動靜分離

(一)簡述html

    Nginx是一種輕量級,高性能,多進程的Web服務器,很是適合做爲靜態資源的服務器使用,而動態的訪問操做能夠使用穩定的Apache、Tomcat及IIS等來實現,這裏就以Nginx做爲代理服務器的同時,也使用其做爲靜態資源的服務器,而動態的訪問服務器就以Tomcat爲例說明。java

wKioL1mVOu7hSTagAADS-vIP1pE166.png

(二)環境簡介nginx

服務器名稱 IP 備註
Nginx服務器 192.168.180.4
Tomcat服務器 192.168.180.23
client 192.168.181.231 客戶端訪問


(三)具體步驟:web

(1)tomcat服務器(192.168.180.23)的相關配置配置apache

1.1 tomcat的安裝及相關環境變量的配置能夠參考前面文檔,具體本次試驗省略了bootstrap

1.2 ,啓動tomcat測試界面,打開會出現測試頁面。vim

[root@localhost local]# /usr/local/apache-tomcat-7.0.63/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/apache-tomcat-7.0.63
Using CATALINA_HOME:   /usr/local/apache-tomcat-7.0.63
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.63/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_131/
Using CLASSPATH:       /usr/local/apache-tomcat-7.0.63/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.63/bin/tomcat-juli.jar
Tomcat started.

1.3新建測試頁面。在/usr/local/apache-tomcat-7.0.63/webapps下新建html目錄和index.html文件瀏覽器

[root@localhost local]# mkdir /usr/local/apache-tomcat-7.0.63/webapps/html
[root@localhost local]# vim /usr/local/apache-tomcat-7.0.63/webapps/html/index.html
this is tomcat index.html
and this ip:192.168.180.23
port:8080

1.4修改server.xml文件的測試路徑的路徑。在<host> ****</host>標籤之間添加上:  <Context path="" docBase="html" debug="0" reloadable="true" />tomcat

path是說明虛擬目錄的名字,若是你要只輸入ip地址就顯示主頁,則該鍵值留爲空;
docBase是虛擬目錄的路徑,它默認的是$tomcat/webapps/ROOT目錄,如今我在webapps目錄下建了一個html目錄,讓該目錄做爲個人默認目錄。
debug和reloadable通常都分別設置成0和true。bash

[root@localhost local]# vim /usr/local/apache-tomcat-7.0.63/conf/server.xml 
   <Host name="localhost"  appBase="webapps"                                                                                                                                                 
            unpackWARs="true" autoDeploy="true">                                                                                                                                                
                                                                                                                                                                                                
        <!-- SingleSignOn valve, share authentication between web applications                                                                                                                  
             Documentation at: /docs/config/valve.html -->                                                                                                                                      
        <!--                                                                                                                                                                                    
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />                                                                                                                    
        -->                                                                                                                                                                                     
       <Context path="" docBase="html" debug="0" reloadable="true" />                                                                                                                           
        <!-- 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>

1.5修改web.xml文件,查看html文件的內容。在<welcome-file-list>****</welcome-file>段之間添加上:<welcome-file>html</welcome-file>。完成以後重啓便可。

[root@localhost local]# vim /usr/local/apache-tomcat-7.0.63/conf/web.xml 
 <welcome-file-list>
        <welcome-file>html</welcome-file>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

1.6新建相關的測試頁面,如test.jsp test.do

[root@localhost html]# pwd
/usr/local/apache-tomcat-7.0.63/webapps/html
[root@localhost html]# cat test.jsp 
this is tomcat of test.jsp
[root@localhost html]# cat test.do 
welcome to tomcat ,this is test-do.do

經過瀏覽器訪問的結果以下:

a.訪問默認頁面:

wKioL1mVQ_6ibj0NAAA50r1q2IE798.pngb.訪問test.jsp頁面

wKioL1mVRC2R-j3XAAAvYC9n5xU396.png

c.訪問test.do頁面

wKiom1mVRGSyzy9CAAAyjhWlLKc973.png


(2)Nginx服務器的相關配置

[root@Monitor server]# vim /usr/local/nginx/conf/server/server.conf
server {
         listen    80;
         server_name  xn2.lqb.com;
         root /html/xn2;
    #    rewrite ^/(.*)$ https:xn3.lqb.com/$1 permanent;
     location / {
       index index.html;
    #    proxy_cache mycache;
    #    proxy_cache_valid 200 3h;
    #    proxy_cache_valid 301 302 10m;
    #    proxy_cache_valid all 1m;
    #    proxy_cache_use_stale error timeout http_500 http_502 http_503;
    #    proxy_pass http://192.168.180.9;
    #    proxy_set_header Host    $host;
    #    proxy_set_header X-Real-IP  $remote_addr;
                     }
       location  /ie/
           {
             index index.html;
             }
       location ~ .*\.(gif|jpg|jpeg|png|swf)$  {      #全部的靜態文件以gif、jpg等結尾的都在本地打開,目錄爲/html/xn2下,保存時間爲30天
                     expires 30d;
                          }
       location ~ (\.jsp)|(\.do)$ {                 ##########全部的以jsp .do的動態請求都交給後邊的tomcat代理服務器處理。
                proxy_pass http://192.168.180.23:8080;
                proxy_redirect off;
                proxy_set_header HOST $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                       }
                    }
[root@Monitor server]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@Monitor server]# /usr/local/nginx/sbin/nginx -s reload

訪問結果以下:

a.訪問默認頁面,會直接訪問nginx所默認的頁面,而不會調到後臺tomcat服務器頁面

wKioL1mVRu6C15E5AAAzsUM36yY644.png

b.訪問.do頁面。其實訪問的就是tomcat後臺test.do頁面

wKioL1mVRsXjZs46AAAyjhWlLKc615.png-wh_50

c.訪問jsp頁面。其實訪問的就是tomcat後臺test.jsp頁面

wKioL1mVRyzRiGlGAAAvYC9n5xU371.png

至此nginx+tomcat的動靜分離配置完成。

相關文章
相關標籤/搜索