目標:同一臺Linux主機上 安裝 Nginx 和 兩個 Tomcat 的集羣
1. 下載安裝 Nginx
地址:http://nginx.org/download/nginx-1.0.4.tar.gz
cd /usr/local/src/nginx wget http://nginx.org/download/nginx-1.0.4.tar.gz tar zxvf nginx-1.0.4.tar.gz cd nginx-1.0.4 ./configure make make install
2. Tomcat的下載安裝配置,請參照 http://pengranxiang.iteye.com/admin/blogs/1135072
3. 配置 Nginx 負載均衡 來集成 兩個Tomcat
修改配置文件:$NGNINX_HOME/conf/nginx.conf
#Nginx所用用戶和組,window下不指定 user nobody; #工作的子進程數量(通常等於CPU數量或者2倍於CPU) worker_processes 1; #錯誤日誌存放路徑 #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/error.log info; #指定pid存放文件 pid logs/nginx.pid; events { #使用網絡IO模型linux建議epoll,FreeBSD建議採用kqueue,window下不指定。 use epoll; #允許最大連接數 worker_connections 1024; } http { #mine.types內定義各文件類型映像 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; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; #負載均衡集羣設置 upstream tomcats { server localhost:8080 weight=1; server localhost:9080 weight=1; #根據ip計算將請求分配各那個後端tomcat,許多人誤認爲可以解決session問題,其實並不能。 #同一機器在多網情況下,路由切換,ip可能不同 ip_hash; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { index index.shtml; proxy_pass http://tomcats; proxy_set_header X-Real-IP $remote_addr; client_max_body_size 100m; } #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; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
安裝以上配置即可實現集羣。
關於Nginx啓動
直接執行: $NGINX_HOME/sbin/nginx 即可
爲了方便,可以自定義一個腳本文件
vim nginx.sh
#!/bin/bash case $1 in start) /usr/local/nginx/sbin/nginx; ;; stop) kill -2 `ps -ef|grep "/usr/local/nginx/sbin/nginx" | grep -v "grep"|awk '{print $2}'` ;; restart) $0 stop $0 start ;; *) echo $"Usage; $0{start|stop|restart}" exit 1 esac exit 0