nginx+tomcat的安裝配置

Nginx ("engine x") 是一個高性能的 HTTP 和 反向代理 服務器,也是一個 IMAP/POP3/SMTP代理服務器。已經因爲它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而被人們廣泛使用了。
大多數人選擇它主要考慮到的是它的高併發和負載均衡。
一、安裝Nginx
1. tar zxvf  nginx-0.7.44.tar.gz

2、編譯安裝nginx
   cd nginx-0.7.44
   ./configure --with-http_stub_status_module --with-http_ssl_module --prefix=/usr/local/nginx
   執行後,可能會提示 ./configure: error: the HTTP rewrite module requires the PCRE library.這個錯誤,是缺少pcre 包,可用yum install pcre pcre-devlel來解決

3、make && make install

4、nginx安裝成功後的安裝目錄爲/usr/local/nginx
5.編輯配置文件nginx.conf
#user  www www; 
worker_processes 8;    #啓動進程數,可根據機器核數來修改
error_log  /usr/local/nginx/logs/nginx_error.log  crit; #全局錯誤日誌及PID文件
pid        /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;
events
   {
     use epoll;
     worker_connections 65535; #工作模式及連接數上限
    } 

http                  #設定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;  #定義的日誌格式和存放路徑
    
    #General Options
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_body_buffer_size    8m; #256k
    server_tokens off;
    ignore_invalid_headers   on;
    recursive_error_pages    on;
    server_name_in_redirect off; 
    sendfile                 on;
 
    #timeouts
    keepalive_timeout 60;
    #client_body_timeout   3m;
    #client_header_timeout 3m;
    #send_timeout          3m;
     
    #TCP Options
    tcp_nopush  on;
    tcp_nodelay on;
  
    #size limits
    client_max_body_size       50m;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
  
    upstream xxx.com {
         ip_hash;
         server x.x.x.x:8080 max_fails=0 weight=1;
         server x.x.x.x:8080 max_fails=0 weight=1; #8080爲tomcat端口
     }

    server {
        listen 80 default;
        rewrite ^(.*) http://www.xxx.com/ permanent;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        root   html;
        }
    }

   server
        {
            listen      80;
            server_name  www.xxx.com;
            index index.php index.html index.htm;
            root   /http;
            access_log /data/logs/access.xxx.com.log  combined;
            error_log  /data/logs/error_test.xxx.log;
           
            #expires                        
            location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
              {
                   expires 30d;
               }
 
            location ~ .*\.(js|css)?$
              {
                   expires 24h;
              }   
 
            location /nginxstatus {
                stub_status on;
                access_log off;
             }

          # location ~ .*\.jsp?$
            location /
            {
                proxy_pass http://xxx.com;
                proxy_redirect off;
             #  proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $http_host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Connection Close;
                proxy_set_header X-Forwarded-For $remote_addr;
                error_page   404              /404.html;
                error_page   500 502 503 504  /50x.html;
             #  location = /50x.html {
             #  root   html;
             #  }
           }
        
        }
}

6、修改/usr/local/nginx/conf/nginx.conf配置文件後,用命令/usr/local/nginx/sbin/nginx -t 檢查配置文件是否正確:

7、啓動nginx的命令
  /usr/local/nginx/sbin/nginx

8、停止nginx的命令
  /usr/local/nginx/sbin/nginx -s stop
9、在不停止Nginx服務的情況下加載Nginx配置
  kill -HUP `cat /usr/local/nginx/nginx.pid` 

10.nginx網站開啓後(已啓用stub_stauts),可用http://www.xxx.com/nginxstatus查看nginx狀態
   active connections -- 對後端發起的活動連接數
   server accepts handled requests -- nginx 總共處理的連接, 成功創建了幾次握手  總共處理了多少個請求
   reading -- nginx 讀取到客戶端的Header信息數
   writing -- nginx 返回給客戶端的Header信息數
   waiting -- 開啓 keep-alive 的情況下,這個值等於 active - (reading + writing)


下面開始Tomcat的安裝了,那就更簡單了,網上文檔也是一大把
Tomcat安裝
一、 jdk的安裝
   安裝的jdk爲:jdk-6u21-linux-x64.bin
   1.sh jdk-6u17-linux-x64-rpm.bin
   2.安裝程序在問您是否願意遵守剛纔看過的許可協議。輸入"y" 或 "yes" 回車
   3.執行後,把後成的文件夾命名爲jdk,放到/usr/local/下 (路徑可自己選,但在變量中要指出)
   4.vi /etc/profile
          在裏面添加如下內容
          export JAVA_HOME=/usr/local/jdk
          export JAVA_BIN=/usr/local/jdk/bin
          export PATH=$PATH:$JAVA_HOME/bin
          export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
   5. source /etc/profile  使剛纔填加的生效
   6.驗證
     java -version
     屏幕輸出:
     java version "1.6.0_13"
     Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
     Java HotSpot(TM) Server VM (build 11.3-b02, mixed mode)
   安裝JDK1.6完畢.
 
二、 Tomcat的安裝
   下載apache-tomcat-6.0.18.tar.gz,解壓後更名或更改存放路徑(可自行規定)
 
   nginx與tomcat的結合,主要用的是nginx中的upstream,後端可包括有多臺tomcat來處理動態頁面。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章