nginx+tomcat實現動靜態分離,nginxtomcat

來源:互聯網
上載者:User

nginx+tomcat實現動靜態分離,nginxtomcat

  ===============Tomcat 概述:
    Tomcat是Apache 軟體基金會(Apache Software Foundation)的Jakarta 項目中的一個核心項目,由Apache、Sun 和其他一些公司及個人共同開發而成。由於有了Sun 的參與和支援,最新的Servlet 和JSP 規範總是能在Tomcat 中得到體現,Tomcat 5支援最新的Servlet 2.4 和JSP 2.0 規範。因為Tomcat 技術先進、效能穩定,而且免費,因而深受Java 愛好者的喜愛並得到了部分軟體開發商的認可,成為目前比較流行的Web 應用程式伺服器。目前最新版本是8.0。

===tomcat伺服器中:
1》安裝jdk並配置環境變數:
  tar -xvf jdk-8u5-linux-x64.tar.gz
  mv jdk1.8.0_05/ /usr/local/src/java

       

  2》安裝tomcat:
    tar -xvf apache-tomcat-7.0.54.tar.gz
    mv apache-tomcat-7.0.54 /usr/local/src/tomcat

3》配置tomcat:
  #vim /usr/local/src/tomcat/conf/server.xml
  <Connector port="80" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" />
   修改連接埠為預設WEB連接埠80;

  <Host name="localhost" appBase="che"
    unpackWARs="true" autoDeploy="true">
  <Context path="" docBase="/opt/che">
  新增最後一行修改為WEB的家目錄,如果要做代理,則name為localhost;      

  4》啟動tomcat:

    

5》tomcat啟動測試:

   

 

=============Nginx的安裝與配置:
1》安裝依賴庫:
  yum -y install gcc gcc-c++ openssl-devel zlib-devel pcre-devel
2》安裝pcre庫:
  tar -xvf pcre-8.36.tar.gz
  cd pcre-8.36
  ./configure && make && make install cd pcre-8.36
3》安裝libmd5:
  tar xf libmd5-0.8.2b.tar.gz
4》安裝nginx:

           ar -xvf nginx-1.9.8.tar.gz
      cd nginx-1.9.8

CONFOPTS="
--user=user_00 \
--group=users \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-md5=/soft/md5/ \
--with-pcre=/usr/local/src/pcre-8.01 \
--without-select_module \
--without-poll_module \
--without-http_ssi_module \
--without-http_userid_module \
--without-http_geo_module \
--without-http_map_module \
--without-http_memcached_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--prefix=/usr/local/services/nginx-1.9.8 \
"
./configure $CONFOPTS && make && make install

 

  編譯參數詳解:
--with-http_realip_module
此模組支援顯示真實來源IP地址,主要用於NGINX做前端負載平衡伺服器使用。

-with-http_stub_status_module
這個模組可以取得一些nginx的運行狀態,

--with-http_gzip_static_module
這個模組在一個預壓縮檔傳送到開啟Gzip壓縮的用戶端之前檢查是否已經存在以“.gz”結尾的壓縮檔,這樣可以防止檔案被重複壓縮。

--with-md5=/soft/md5/
設定md5庫檔案路徑

--with-sha1=auto/lib/sha1
設定sha1庫檔案路徑
--with-pcre=/soft/pcre-8.01
設定PCRE庫路徑

--without-select_module
標準串連模式。預設情況下自動編譯方式。您可以啟用或禁用通過使用-select_module和不帶- select_module配置參數這個模組

--without-poll_module
不使用poll模組

--without-http_ssi_module
不使用ngx_http_ssi_module模組,此模組處理伺服器端包含檔案(ssi)的處理.

--without-http_userid_module
不使用ngx_http_userid_module模組

--without-http_geo_module
這個模組基於用戶端的IP地址建立一些ngx_http_geoip_module變數,並與MaxMindGeoIP檔案進行匹配,該模組僅用於 0.7.63和0.8.6版本之後。但效果不太理想,對於城市的IP記錄並不是特別準確,不過對於網站的來源訪問地區的分析大致有一定參考性

--without-http_map_module
不使用ngx_http_map_module模組

--without-http_memcached_module
不使用ngx_http_memcached_module模組

--without-mail_pop3_module
不允許ngx_mail_pop3_module模組

--without-mail_imap_module
不允許ngx_mail_imap_module模組

--without-mail_smtp_module
不允許ngx_mail_smtp_module模組   

4》配置說明:
  設定檔位置:/usr/local/nginx/conf/nginx.conf
  1. Nginx設定檔分為4個部分
  2. main(全域設定)
  3. server(主機設定)
  4. upstream(負載平衡設定)
  5. localtion(URL匹配特定位置的設定)
  這四個 server繼承main location繼承server,upstream即不會繼承,其它設定也不會被繼承.

   

====nginx檔案配置內容:

user user_00;
worker_processes 8;
error_log logs/error.log;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 100000;
}
worker_rlimit_nofile 100000;
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
server_names_hash_bucket_size 128k;
client_header_buffer_size 32k;
large_client_header_buffers 432k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 254k;
fastcgi_buffers 16 256k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
gzip on;
gzip_min_length 1k;
gzip_buffers 416k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript;
gzip_vary on;
charset utf-8;
access_log off;
log_not_found off;
error_page 400 403 405 408 /40x.html;
error_page 500 502 503 504 /50x.html;

server {
listen 80;
server_name www.test.com;
location / {
root html/cheche;
index index.html index.htm;
}
}

include   vhost/vhost.localhost.com;
}    

 

虛擬機器主機檔案內容:

upstream webcount {

      server  10.0.0.202:8080 weight=1 max_fails=3 fail_timeout=20s;

#     server  10.0.0.203:8080 weight=1 max_fails=3 fail_timeout=20s;

 

}

server { 

        listen       80; 

        server_name  www.aatest.com; 

        charset utf-8; 

            location ~ (\.jsp)|(\.do)$ {  #jsp和do的交給tomcat處理實現動靜分離

            index  index.html index.htm index.jsp index.do; 

            proxy_pass        http://webcount; 

            proxy_set_header  X-Real-IP  $remote_addr; 

            client_max_body_size  100m; 

            }

         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|html|htm)$#html和圖片的和其它的靜態內容存路徑html

    { 

       root   /opt/che;   #可自由指定

        expires      30d; 

    } 

}~ 

 

5》啟動與平滑重啟

  # cd /usr/local/services/nginx-0.8.55/sbin/

  # ./nginx –t 檢測設定檔是否有錯誤

  # ./nginx 啟動nginx

  # ./nginx -s reload

6》動靜分離測試       1>在tomcat上

        在WEB目錄建立

    # cd /opt/che

    # echo “this is jsp” >index.jsp

    # echo “this is do” >index.do

      http://10.0.0.202:8080/index.jsp

      http://10.0.0.202:8080/index.do

      

  2>在Nginx上

    在Nginx的/opt/che目錄下

    # cd /opt/che

    # echo “this is index ” >index.html

    # echo “this is index a” >aa.html

      3>在瀏覽器裡面

     http://www.aatest.com/ 

                

         http://www.aatest.com/aa.html

          

        http:// www.aatest.com /index.jsp

       

        

           http://www.aatest.com/index.do

                     

 

==========================

可以實現控制tomcat開啟與關閉的指令碼:

#!/bin/bash
# Title:tomcat.sh
# Decription:Tomcat manager
# system:Use Linux
# Author:Alvin_zeng
# Version:1.0
# Date:2016-08-22
# Copyright:   2016 (c) alvin
# License:     GPL
TomcatDir="/usr/local/tomcat"
#====================================
# Function-->Men_CD()
#====================================
Men_CD()
{
clear
echo
echo "========TomcatManagerTool======"
echo
tput bold
echo "1: 啟動tomcat服務"
tput sgr0
echo "    [*] start tomcat service"
echo
tput bold
echo "2: 關閉tomcat服務"
tput sgr0
echo "    [*] stop tomcat service"
echo
tput bold
echo "3: 重啟tomcat服務"
tput sgr0
echo "    [*] restart tomcat service"
echo
tput bold
echo "4: 查看tomcat日誌"
tput sgr0
echo "    [*] check tomcat logs"
echo
tput bold
echo "5:退出"
tput sgr0
echo "    [*]exit"
echo
echo "========TomcatManagerTool======="
echo
}
#====================================
# Function-->Acation_Manager
#====================================
Acation_Manager()
{
Men_CD
echo -n "Please enter you number:"
 read Myenter 
case $Myenter in 
1)
checktomcat=`ps -ef | grep java | grep "$TomcatDir" | wc -l`
if [ ${checktomcat} -eq 1 ]
then
    echo
             tput bold
    echo  " [Error]:The service is already running. Please stop the service at startup."
                                   
             tput sgr0
    echo
else
    $TomcatDir/bin/startup.sh start
    checktomcat1=`ps -ef | grep java | grep "$TomcatDir" | wc -l`
    if [ ${checktomcat1} -eq 1 ]
    then
              tput bold
              echo "Start Tomcat.....................[OK]"
              echo "Start Tomcat.....................[OK]"
              echo "Start Tomcat.....................[OK]"
              tput sgr0
    else
              tput bold
              echo "Start tomcat.....................[Fail]"
              echo "Start tomcat.....................[Fail]"
              echo "Start tomcat.....................[Fail]"
              tput sgr0
              return 1
    fi

fi
;;

2)
checktomcat1=`ps -ef | grep java | grep "$TomcatDir" | wc -l`
if [ ${checktomcat1}  -eq 1 ]
then
   
        /bin/ps -ef | grep java | grep "$TomcatDir"  | awk '{print $2}' | xargs kill -9 >>/dev/null 2>&1
    if [ $? -eq 0 ]
    then
       echo
       tput bold
       echo  "Tomcat Stop..............[OK]"
       echo  "Tomcat Stop..............[OK]"
       echo  "Tomcat Stop..............[OK]"
       tput sgr0
       echo
   else
       echo
       tput bold
       echo "Tomcat Stop...............[Fail]"
       echo "Tomcat Stop...............[Fail]"
       echo "Tomcat Stop...............[Fail]"
       tput sgr0
       echo
       return 1
    fi
else
   echo
   tput bold
   echo "Tomcat The service is already close."
   echo "Tomcat The service is already close."
   echo "Tomcat The service is already close."
   tput sgr0
   echo
fi
;;



3)
    ps -ef | grep java | grep "$TomcatDir"  | awk '{print $2}' | xargs kill -9 >>/dev/null 2>&1
        $TomcatDir/bin/startup.sh start
        checktomcat1=`ps -ef | grep java | grep "$TomcatDir" | wc -l`
        if [ ${checktomcat1} -eq 1 ]
        then
              echo
              tput bold
              echo "Restart Tomcat.....................[OK]"
              echo "Restart Tomcat.....................[OK]"
              echo "Restart Tomcat.....................[OK]"
              tput sgr0
              echo
        else
              tput bold
              echo "Restart Tomcat.....................[Fail]"
              echo "Restart Tomcat.....................[Fail]"
              echo "Restart Tomcat.....................[Fail]"
              tput sgr0
              return 1
        fi
;;

4)
 tail -f $TomcatDir/logs/catalina.out 
;;

5)
  clear
   exit 2
;;

*)
  echo "Please enter |1|2|3|4|5"
;;
esac
}

#====================================
# Function-->Main
#====================================
Main()
{
Acation_Manager
   if [ $? -eq 1 ]
   then
      exit 1
   fi
}
Main;
================================

vhost.www.fanhougame.com 檔案內容(原始內容,根據需要修改)

server {
        listen       80 ;
        server_name  www.fanhougame.com fanhougame.com  www.fanhouapp.com www.fanhougame.net fanhougame.net;
#        server_name  www.fanhougame.net fanhougame.net  www.fanhouapp.net;
        root         /data/www/oa.com/www.fanhougame.com ;

        location / {
            index index.php index.html index.htm;
            if (!-e $request_filename) {
                return 444;
            }
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/nginx/html;
        }

        location ~ \.php$ {
            fastcgi_pass   unix:/tmp/php-cgi-5313-web.sock;
            fastcgi_index  index.php;
            include        fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param SERVER_NAME $http_host;
            fastcgi_ignore_client_abort on;
        }

        location ~ \.(swf|js|css|xml|gif|jpg|jpeg|png|bmp)$ {
            error_log    off;
            access_log   off;
            expires      max;
        }
    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.