nginx與tomcat結合javascript
Nginx與Tomcat整合的好處以下所示:css
◆靜態分離,加快用戶訪問網站的速度。html
◆整個負載均衡層和Web層的工做流程爲LVS/DR+Keeaplived→Nginx反向代理(動靜分離)→Tomcat集羣,能夠保證整個網站不會由於某一臺LVS或Nginx+tomcat機器掛掉而影響網站的運營。java
◆Nginx穩定,宕機的可能性微乎其乎。node
下面是實施的具體步驟nginx
因爲服務器均採用的是最小化安裝,因此先安裝一些基礎的編譯庫,命令以下:c++
[root@localhost ~]# yum -y install gcc gcc-c++ autoconf libjpeglibjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-develzlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncursesncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidnlibidn-devel openssl openssl-develweb
(1)首先分別在xxx.xxx.xxx.146和xxx.xxx.xxx.147上安裝Nginx和Tomcat,而後整合它們。apache
在安裝Tomcat 以前,必須先安裝JDK(參考文檔). vim
而後下載並安裝apache-tomat,以下所示。
wget http://mirror.bjtu.edu.cn/apache/tomcat/tomcat-7/v7.0.12/bin/apache-tomcat7.0.12.tar.gz
tar zxvf apache-tomcat-7.0.12.tar.gz
mv apache-tomcat-7.0.12 /usr/local/tomcat/
cp –rf /usr/local/tomcat/webapps /data/htdocs/www
vim /usr/local/tomcat/conf/server.xml
修改tomcat的根路徑位置,測試網站地址爲/data/htdocs/www/shop,這個虛擬主機須要在/usr/local/tomcat/conf/server.xml裏指定,改動後的內容以下所示:
<Host name="www.my22.com" appBase="webapps"
unpackWARs="true"autoDeploy="true">
<Context path=""docBase="/data/htdocs/www/shop/" />
若是咱們要繼續增長虛擬主機,按照如上格式繼續添加內容便可,Host name後面接虛擬主機名稱,docBase後面接虛擬主機對應的路徑位置。
安裝完成後,啓動tomcat,默認即監聽了8080端口,啓動命令以下:
/usr/local/tomcat/bin/startup.sh
成功運行後,用lsof -i:8080來進行驗證,以下所示:
lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE NODENAME
java 23731 root 40u IPv6 3347645 TCP *:webcache (LISTEN)
最後安裝pcre及Nginx,整合Nginx與Tomcat, Nginx的安裝再也不詳細說明了。靜態HTML頁面、圖片、CSS等由Nginx來處理,jsp、do內容由後端的Tomcat處理,nginx.conf配置文件的內容以下所示(這裏爲了調試方便,咱們首先將域名www.my22.com指向了xxx.xxx.xxx.146,另外一臺Web配置跟xxx.xxx.xxx.146同樣的,我這裏以xxx.xxx.xxx.146舉例說明,等此架構中的Web環境均順利後,再將域名www.my22.com指向咱們的VIP地址):
user myweb myweb;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /tmp/nginx.pid;
#指定的文件描述符的值最大.
worker_rlimit_nofile 65535;
#工做模式及鏈接數上限
events
{
use epoll;
worker_connections 65535;
}
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http
{
#設定mime類型
include mime.types;
default_type application/octet-stream;
#charset gb2312;
#設定請求緩衝
server_names_hash_bucket_size128;
client_header_buffer_size 32k;
large_client_header_buffers 432k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
server_tokens off;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_send_timeout 60;
proxy_read_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
# 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.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascripttext/css application/xml;
gzip_vary on;
#禁止經過ip訪問站點
server{
server_name _;
return 404;
}
server
{
listen 80;
server_name www.my22.com;
index index.html index.htmindex.jsp index.do; #設定訪問的默認首頁地址
root /data/htdocs/www/shop; #設定網站的資源存放路徑
if (-d $request_filename)
{
rewrite ^/(.*)([^/])$http://$host/$1$2/ permanent;
}
#全部jsp的頁面均交由tomcat處理
location ~\.(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:8080; #轉向tomcat處理
}
location ~ .*\.(htm|html|gif|jpg|jpeg|bmp|swf|ioc|rar|zip|txt|flv|doc|ppt|pdf|xls|mp3|wma)$
#設定訪問靜態文件直接讀取不通過tomcat
{
expires 30d;
}
location ~.*\.(js|css)?$
{
expires 1h;
}
#定義訪問日誌的寫入格式
log_format wwwlog '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent"$http_x_forwarded_for';
access_log /var/log/www_my22.log wwwlog; #設定訪問日誌的存放路徑
}
}
配置完成後,咱們用以下命令啓動Nginx進程,讓其監聽80端口,以下所示:
/usr/local/nginx/nginx
而後咱們在/data/htdocs/www/shop/下面寫一段Java代碼,驗證以上的配置是否成功,寫完後咱們能夠用cat 命令來查看/data/htdocs/www/shop/mem.jsp文件,文件內容以下所示:
<%
Runtime lRuntime = Runtime.getRuntime();
out.println("*** BEGIN MEMORY STATISTICS ***<br/>");
out.println("Free Memory:"+lRuntime.freeMemory()+"<br/>");
out.println("Max Memory:"+lRuntime.maxMemory()+"<br/>");
out.println("Total Memory:"+lRuntime.totalMemory()+"<br/>");
out.println("Available Processors : "+lRuntime.availableProcessors()+"<br/>");
out.println("*** END MEMORY STATISTICS ***");
%>
咱們能夠輸入http://www.my22.com/mem.jsp來驗證nginx+tomcat整合成功了。
簡約配置以下:
user myweb myweb;
worker_processes 1;
error_log off;
pid /tmp/nginx.pid;
events {
use epoll;
worker_connections 10;
}
http {
include mime.types;
default_type application/octet-stream;
access_log off;
sendfile on;
keepalive_timeout 65;
server_names_hash_bucket_size128;
client_header_buffer_size 32k;
large_client_header_buffers 432k;
tcp_nodelay on;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size128k;
upstream tomcat_server{
server 127.0.0.1:8080;
}
server {
listen xxx.xxx.xxx.146:80;
server_name xxx.xxx.xxx.146 default;
index index.html index.htmindex.jsp index.do;
root /data/htdocs/www/shop;
if (-d $request_filename)
{
rewrite^/(.*)([^/])$ http://$host/$1$2/permanent;
}
location ~\.(jsp|jspx|do)?$ {
proxy_set_headerHost $host;
proxy_set_headerX-Forwarded_For $remote_addr;
proxy_pass http://tomcat_server;
}
error_page 404 500 502 503 504 /50x.html;
location /50x.html{
root /usr/local/nginx/html;
}
}
}
參考網站: