Nginx Tomcat集群部署
下載的jdk文件為:jdk-6u45-linux-x64.bin,執(zhí)行如下命令進(jìn)行安裝:
#./jdk-6u12-linux-i586.bin
安裝tomcat
#tar zxvf apache-tomcat-6.0.18.tar.gz
#mv apache-tomcat-6.0.29 tomcat
這里我將解壓后的apache-tomcat-6.0.29重命名為了tomcat方便操作。
配置環(huán)境變量
編輯/etc下的profile文件,加上如下內(nèi)容:
JAVA_HOME="/opt/app/jdk1.6.0_45"
CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
PATH=".:$PATH:$JAVA_HOME/bin"
CATALINA_HOME="/opt/app/tomcat"
export JAVA_HOME CATALINA_HOME
執(zhí)行下面命令使變更生效:
# source /etc/profile
啟動(dòng)tomcat并輸入http://domain:8080,如果看到貓的頁面即tomcat和jdk安裝成功
新建文件目錄/home/www為網(wǎng)站存放目錄,設(shè)置server.xml文件,在Host name=”localhost”處將appBase=的指向路徑改為/home/www/web
創(chuàng)建index.jsp至/home/www/web/ROOT,內(nèi)容為:“hello!” 重新啟動(dòng)tomcat,重新訪問,如果看到index.jsp文件內(nèi)容hello!表示設(shè)置成功。
安裝Nginx
執(zhí)行如下命令解壓nginx:
# tar zxvf nginx-1.4.4.tar.gz
# mv nginx-1.4.4 nginx
同樣重命名了一下。
安裝nginx:
# ./configure --prefix=/opt/app/nginx
結(jié)果出現(xiàn)了錯(cuò)誤:error: C compiler cc is not found,按網(wǎng)上所說安裝編譯源碼所需的工具和庫:
#yum install gcc gcc-c++ ncurses-devel perl
再次安裝,發(fā)現(xiàn)還有錯(cuò)誤:the HTTP rewrite module requires the PCRE library.
執(zhí)行
# yum -y install pcre-devel openssl openssl-devel
終于成功,
# ./configure --prefix=/opt/app/nginx
# make
# make install
nginx安裝成功后的安裝目錄為/opt/app/nginx
在conf文件夾中新建proxy.conf,用于配置一些代理參數(shù),內(nèi)容如下:
#!nginx (-)
# proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; #獲取真實(shí)ip
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #獲取代理者的真實(shí)ip
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
編輯安裝目錄下conf文件夾中的nginx.conf,輸入如下內(nèi)容
#運(yùn)行nginx所在的用戶名和用戶組
#user www www;
#啟動(dòng)進(jìn)程數(shù)
worker_processes 8;
#全局錯(cuò)誤日志及PID文件
error_log /opt/app/nginx/logs/nginx_error.log crit;
pid /opt/app/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
#工作模式及連接數(shù)上限
events
{
use epoll;
worker_connections 65535;
}
#設(shè)定http服務(wù)器,利用它的反向代理功能提供負(fù)載均衡支持
http
{
#設(shè)定mime類型
include mime.types;
default_type application/octet-stream;
include /opt/app/nginx/conf/proxy.conf;
#charset gb2312;
#設(shè)定請(qǐng)求緩沖
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
# client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
# fastcgi_connect_timeout 300;
# fastcgi_send_timeout 300;
# fastcgi_read_timeout 300;
# fastcgi_buffer_size 64k;
# fastcgi_buffers 4 64k;
# fastcgi_busy_buffers_size 128k;
# fastcgi_temp_file_write_size 128k;
# 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;
#limit_zone crawler $binary_remote_addr 10m;
###禁止通過ip訪問站點(diǎn)
# server{
# server_name _;
# return 404;
# }
server
{
listen 80;
server_name localhost;
index index.html index.jsp;
root /opt/www/static;
#limit_conn crawler 20;
location ~ .*.(jsp|shtml)$ #所有shtml的頁面均交由tomcat處理
{
index index.jsp;
proxy_pass http://localhost:8080;#轉(zhuǎn)向tomcat處理
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ #設(shè)定訪問靜態(tài)文件直接讀取不經(jīng)過tomcat
{
expires 30d;
}
location ~ .*.(js|css)?$
{
expires 1h;
}
}
#定義訪問日志的寫入格式
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /opt/app/nginx/logs/localhost.log access;#設(shè)定訪問日志的存放路徑
}
修改/usr/local/nginx/conf/nginx.conf配置文件后,請(qǐng)執(zhí)行以下命令檢查配置文件是否正確:
#/opt/app/nginx/sbin/nginx -t
如果出現(xiàn)下面兩行,說明正確:
the configuration file /opt/app/nginx/conf/nginx.conf syntax is ok
the configuration file /opt/app/nginx/conf/nginx.conf was tested successfully
如果提示unknown host,則可在服務(wù)器上執(zhí)行:ping www.baidu.com如果也是同樣提示unknown host則有兩種可能:
a、服務(wù)器沒有設(shè)置DNS服務(wù)器地址,查看/etc/resolv.conf下是否設(shè)置,若無則加上
b、防火墻攔截
啟動(dòng)nginx的命令
#/opt/app/nginx/sbin/nginx
這時(shí),輸入以下命令查看Nginx主進(jìn)程號(hào):
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
停止nginx的命令
#/opt/app/nginx/sbin/nginx -s stop
在不停止Nginx服務(wù)的情況下平滑變更Nginx配置
輸入以下命令查看Nginx主進(jìn)程號(hào):
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
屏幕顯示的即為Nginx主進(jìn)程號(hào),例如:
6302
這時(shí),執(zhí)行以下命令即可使修改過的Nginx配置文件生效:
kill -HUP 6302
或者無需這么麻煩,找到Nginx的Pid文件:
kill -HUP `cat /usr/local/nginx/nginx.pid`
