nginx+tomcat網頁動靜分離配置

一、環境描述

nginx server (Proxy):192.168.1.135(做爲代理服務器)
WEB server1: 192.168.1.138(使用tomcat做爲web容器)
WEB server2: 192.168.1.139 (使用tomcat做爲web容器)php

 

二、安裝jdk

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
# tar -xvf jdk-8u5-linux-x64.tar.gz
# mv jdk1.8.0_05/ /usr/local/java
# vim /etc/profile
JAVA_HOME=/usr/local/java

JRE_HOME=/usr/local/java/jre

PATH=$JAVA_HOME/bin:$PATH:$JRE_HOME/bin

CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib

export JAVA_HOME JRE_HOME PATH CLASSPATH

# source /etc/profile  ;使得新添加的配置當即生效
# java –version

css

三、安裝Tomcat(在兩臺web服務器上都要安裝)

1)tomcat安裝html

http://apache.fayea.com/apache-mirror/tomcat/tomcat-7/v7.0.54/bin/apache-tomcat-7.0.54.tar.gz
# tar -xvf apache-tomcat-7.0.54.tar.gz
# mv apache-tomcat-7.0.54 /usr/local/tomcat

2)配置Tomcat前端

# vim /usr/local/tomcat/conf/server.xml
    <Connector port="8080" protocol="HTTP/1.1"  索搜此項修改默認WEB端口
               connectionTimeout="20000"
               redirectPort="8443" />
<Host name="localhost"  appBase="web"  能夠修改域名或者IP,但做代理請保持localhost
            unpackWARs="true" autoDeploy="true">
          <Context path="" docBase="/opt/web"></Context> 新增此項修改WEB的家目錄

3)啓動tomcatjava

 

[root@slave bin]# ./startup.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

linux

四、tomcat測試

http://10.0.0.202:8080 在沒有修改WEB目錄的狀況出現此頁面就算成功

nginx

 

五、Nginx的安裝與配置

1)安裝依賴庫web

# yum -y install gcc openssl-devel zlib-devel

1)安裝pcre依賴包 apache

pcre-8.01.tar.gz
# cd /soft/
# tar xf pcre-8.01.tar.gz -C tmp/
# cd tmp/pcre-8.01/
#./configure && make && make install

2)安裝libmd5依賴庫bootstrap

libmd5-0.8.2b.tar.gz
# cd /soft/
#tar xf libmd5-0.8.2b.tar.gz -C tmp/

3)安裝Nginx
nginx-0.8.55.tar.gz
# cd /soft/
# tar nginx-0.8.55.tar.gz -C tmp/
# cd tmp/nginx-0.8.55/
#CONFOPTS="
--user=user1 \
--group=users \
--with-http_realip_module \ 
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-md5=/soft/md5/ \
--with-sha1=auto/lib/sha1 \
--with-pcre=/soft/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-0.8.55 \
"
#./configure $ CONFOPTS >/dev/null
# make >/dev/null && make install >/dev/null
#chown  user_00.users /data/www/pvp.fanhougame.net –R

編譯參數詳解:
--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模塊

 

6.編輯nginx主配置文件 /usr/local/nginx/conf/nginx.conf

修改如下配置

user  user_00 users;  #這個模塊指令,指Nginx Worker 運用的用戶和組,默認爲nobody

worker_processes  8;  #指定了要開啓的進程數,每進程佔用10M~12M的內存,建議和CPU的核心數量同樣多的進程就好了。

error_log  logs/error.log; #全局錯誤日誌

#===================自定義虛擬機配置文件===========

include vhost/vhost.dali.com; 文件末尾添加反向代理文件

include vhost/vhost.aatest.com;添加本地虛擬主機文件

 

七、Nginx反向代理(負載)vim /usr/local/nginx/conf/vhost/vhost.daili.com

vhost.daili.com 編輯內容爲;
upstream webcount {
      server  192.168.1.135:80 weight=1 max_fails=3 fail_timeout=20s;
  server  192.168.1.138:8080 weight=1 max_fails=3 fail_timeout=20s;
  server  192.168.1.139: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/web;    #可自由指定
    expires      30d;  
    }  
}

 


八、本地虛擬主機配置

vhost.aatest.com編輯內容爲;

    server {
        listen       80 ;
        server_name  www.aatest.com;
        root         /opt/web ;

        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;
        }
    }

九、啓動與平滑重啓

# useradd user1

# chown -R user1:users /opt/web
# cd /usr/local/services/nginx-0.8.55/sbin/
# ./nginx –t 檢測配置文件是否有錯誤
# ./nginx 啓動nginx
# ./nginx -s reload


十、 動靜分離測試

1)在tomcat上
在WEB目錄建立
# cd /opt/web
# echo 「this is jsp」 >index.jsp
# echo 「this is do」 >index.do

2)在Nginx上
在Nginx的/opt/alvin目錄下
# cd /opt/alvin
# echo 「this is index 」 >index.html
# echo 「this is index a」 >aa.html

三、在瀏覽器裏面
http://www.aatest.com/  (在代理服務器上添加主機名和ip地址的映射)
 

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


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

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

相關文章
相關標籤/搜索