linux下Nginx+tomcat整合的安裝與配置


Nginx+tomcat是目前主流的java web架構,如何讓nginx+tomcat同時工作呢,也可以說如何使用nginx來反向代理tomcat后端均衡呢?

目的:搭建Nginx與tomcat整合,用Nginx代替apache
步驟:
一、安裝Tomcat和JDK 

 1、上傳apache-tomcat-6.0.18.tar.gz和jdk-6u12-linux-i586.bin至/usr/local
2、執行如下命令安裝tomcat:

1. #cd /usr/local
2. #tar zxvf apache-tomcat-6.0.18.tar.gz

解壓完成后將apache-tomcat-6.0.18重命名為tomcat
3、執行如下命令安裝JDK:

1. #./jdk-6u12-linux-i586.bin

4、配置環境變量:
編輯/etc下的profile文件,加上如下內容:

1. JAVA_HOME="/usr/local/jdk1.6.0_12"
2. CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
3. PATH=".:$PATH:$JAVA_HOME/bin "
4.  
5. CATALINA_HOME="/usr/local/tomcat"
6. export JAVA_HOME CATALINA_HOME

5、啟動tomcat並輸入http://localhost:8080,如果看到貓的頁面即tomcat和jdk安裝成功
6、新建文件目錄/home/www為網站存放目錄,設置server.xml文件,在Host name="localhost"處將appBase=的指向路徑改為/home/www/web
7、創建index.jsp至/home/www/web/ROOT,內容為:“My web!”   

  

二、安裝Nginx
1、上傳nginx-0.7.63.tar.gz至/usr/local 

2、執行如下命令解壓nginx:

1. #cd /usr/local
2. #tar zxvf  nginx-0.7.63.tar.gz

3、編譯安裝nginx

1. #cd nginx-0.7.63
2. #./configure --with-http_stub_status_module --with-http_ssl_module  #啟動server狀態頁和https模塊

執行完后會提示一個錯誤,說缺少PCRE library 這個是HTTP Rewrite 模塊,也即是url靜態化的包
可上傳pcre-7.9.tar.gz,輸入如下命令安裝:

1. #tar zxvf pcre-7.9.tar.gz
2. #cd pcre-7.9
3. #./configure
4. #make
5. #make install

安裝pcre成功后,繼續安裝nginx

1. #cd nginx-0.7.63
2. #./configure
3. #make
4. #make install

4、nginx安裝成功后的安裝目錄為/usr/local/nginx
在conf文件夾中新建proxy.conf,用於配置一些代理參數,內容如下:

01. #!nginx (-)
02. # proxy.conf
03. proxy_redirect          off;
04. proxy_set_header        Host $host;
05. proxy_set_header        X-Real-IP $remote_addr;  #獲取真實ip
06. #proxy_set_header       X-Forwarded-For   $proxy_add_x_forwarded_for; #獲取代理者的真實ip
07. client_max_body_size    10m;
08. client_body_buffer_size 128k;
09. proxy_connect_timeout   90;
10. proxy_send_timeout      90;
11. proxy_read_timeout      90;
12. proxy_buffer_size       4k;
13. proxy_buffers           4 32k;
14. proxy_busy_buffers_size 64k;
15. proxy_temp_file_write_size 64k;

編輯安裝目錄下conf文件夾中的nginx.conf,輸入如下內容

001. #運行nginx所在的用戶名和用戶組
002. #user  www www;
003.  
004. #啟動進程數
005. worker_processes 8;
006. #全局錯誤日志及PID文件
007. error_log  /usr/local/nginx/logs/nginx_error.log  crit;
008.  
009. pid        /usr/local/nginx/nginx.pid;
010.  
011. #Specifies the value for maximum file descriptors that can be opened by this process.
012.  
013. worker_rlimit_nofile 65535;
014. #工作模式及連接數上限
015. events
016. {
017. use epoll;
018. worker_connections 65535;
019. }
020. #設定http服務器,利用它的反向代理功能提供負載均衡支持
021. http
022. {
023. #設定mime類型
024. include       mime.types;
025. default_type  application/octet-stream;
026. include /usr/local/nginx/conf/proxy.conf;
027. #charset  gb2312;
028. #設定請求緩沖   
029. server_names_hash_bucket_size 128;
030. client_header_buffer_size 32k;
031. large_client_header_buffers 4 32k;
032. client_max_body_size 8m;
033.  
034. sendfile on;
035. tcp_nopush     on;
036.  
037. keepalive_timeout 60;
038.  
039. tcp_nodelay on;
040.  
041. #  fastcgi_connect_timeout 300;
042. #  fastcgi_send_timeout 300;
043. #  fastcgi_read_timeout 300;
044. #  fastcgi_buffer_size 64k;
045. #  fastcgi_buffers 4 64k;
046. #  fastcgi_busy_buffers_size 128k;
047. #  fastcgi_temp_file_write_size 128k;
048.  
049. #  gzip on;
050. #  gzip_min_length  1k;
051. #  gzip_buffers     4 16k;
052. #  gzip_http_version 1.0;
053. #  gzip_comp_level 2;
054. #  gzip_types       text/plain application/x-javascript text/css application/xml;
055. #  gzip_vary on;
056.  
057. #limit_zone  crawler  $binary_remote_addr  10m;
058. ###禁止通過ip訪問站點
059. server{
060. server_name _;
061. return 404;
062. }
063.  
064.  
065. server
066. {
067. listen       80;
068. server_name  localhost;
069. index index.html index.htm index.jsp;#設定訪問的默認首頁地址
070. root  /home/www/web/ROOT;#設定網站的資源存放路徑
071.  
072. #limit_conn   crawler  20;   
073.  
074. location ~ .*.jsp$ #所有jsp的頁面均交由tomcat處理
075. {
076. index index.jsp;
077. proxy_pass http://localhost:8080;#轉向tomcat處理
078. }
079.  
080.  
081. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #設定訪問靜態文件直接讀取不經過tomcat
082. {
083. expires      30d;
084. }
085.  
086. location ~ .*\.(js|css)?$
087. {
088. expires      1h;
089. }   
090.  
091. #定義訪問日志的寫入格式
092. log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
093. '$status $body_bytes_sent "$http_referer" '
094. '"$http_user_agent" $http_x_forwarded_for';
095. access_log  /usr/local/nginx/logs/localhost.log access;#設定訪問日志的存放路徑
096.  
097. }
098.  
099.  
100.  
101.  
102. }

5、修改/usr/local/nginx/conf/nginx.conf配置文件后,請執行以下命令檢查配置文件是否正確:

1. #/usr/local/nginx/sbin/nginx -t

如果屏幕顯示以下兩行信息,說明配置文件正確:

1. the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2.   the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully

如果提示unknown host,則可在服務器上執行:ping www.baidu.com如果也是同樣提示unknown host則有兩種可能:
    a、服務器沒有設置DNS服務器地址,查看/etc/resolv.conf下是否設置,若無則加上
    b、防火牆攔截 

 6、啟動nginx的命令

1. #/usr/local/nginx/sbin/nginx

這時,輸入以下命令查看Nginx主進程號:

1. ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'

7、停止nginx的命令

1. #/usr/local/nginx/sbin/nginx -s stop

8、在不停止Nginx服務的情況下平滑變更Nginx配置
a、修改/usr/local/nginx/conf/nginx.conf配置文件后,請執行以下命令檢查配置文件是否正確:

1. /usr/local/nginx/sbin/nginx -t

  如果屏幕顯示以下兩行信息,說明配置文件正確:

1. the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2. the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully

b、這時,輸入以下命令查看Nginx主進程號:

1. ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'

屏幕顯示的即為Nginx主進程號,例如:
  6302
  這時,執行以下命令即可使修改過的Nginx配置文件生效:

1. kill -HUP 6302

   

或者無需這么麻煩,找到Nginx的Pid文件:

1. kill -HUP `cat /usr/local/nginx/nginx.pid`  

9、nginx啟動好后啟動tomcat,此時輸入http://主機ip地址即可看到“My web!” 

  

三、其他 
stub_status
語法: stub_status on 

默認值: None 

作用域: location 

創建一個 location 區域啟用 stub_status 

"stub status" 模塊返回的狀態信息跟 mathopd's 的狀態信息很相似. 返回的狀態信息如下:

1. Active connections: 291
2. server accepts handled requests
3. 16630948 16630948 31070465
4. Reading: 6 Writing: 179 Waiting: 106


active connections -- 對后端發起的活動連接數 

server accepts handled requests -- nginx 總共處理了 16630948 個連接, 成功創建 16630948 次握手 (證明中間沒有失敗的), 總共處理了 31070465 個請求 (平均每次握手處理了 1.8個數據請求) 

reading -- nginx 讀取到客戶端的Header信息數 

writing -- nginx 返回給客戶端的Header信息數 

waiting -- 開啟 keep-alive 的情況下,這個值等於 active - (reading + writing),意思就是Nginx說已經處理完正在等候下一次請求指令的駐留連接

如需要gzip功能,則將nginx.conf文件中相關gzip前面的“#”號去掉


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM