Chinaunix
标题:
nginx+tomcat+jdk
[打印本页]
作者:
wcqniuniu
时间:
2008-07-28 20:45
标题:
nginx+tomcat+jdk
最近打算将公司的apache替换成nginx,突破apache4000个连接数的限制,以及实现负载均衡,和反向代理。首先在虚拟机上测试:一台nginx和两台tomcat(公司的网站是用java做的)
nginx只是起到负载均衡与代理的作用,tomcat上保存的是实际网站内容,以及连接到后面相应的数据库,这里与数据库基本无关,因此就不提了。
实验环境:三台机器装的都是centos 5.2 32bit
nginx:192.168.206.100
tomcat1:192.168.206.101
tomcat2:192.168.206.102
我装的是nginx的rpm包,方便简单(本人不喜欢装源码包,麻烦)装完后相关配置文件的目录是在
/usr/local/nginx/下
这里提几个有用的:/usr/local/nginx/conf/nginx.conf #nginx的配置文件
/usr/local/nginx/logs #相关日志文件存放的目录
别的我都没用到,就不提了,有兴趣者自己研究。
tomcat我是通过yum安装的,用的是cn99的源,由于最近中国科技大的源挂掉了,无法忍受官方源的速度,只得选择cn99的源,装的是tomcat5的版本(最新已到tomcat6)
# yum install tomcat* 装完tomcat同时装玩相关java与jdk的包(网上有种说法,说是有了nginx就不需要jdk环境了,本人认为不大现实,因此也就没有实验,还是老老实实装上所有需要的东西)
装完tomcat很简单,不需要配置任何东西。
# service tomcat5 start
Starting tomcat5: [ OK ]
访问
http://ip
:8080就可以看到tomcat的欢迎界面,就说明tomcat已安装正确,默认tomcat的监听端口是8080.当然可以修改,下面提下与tomcat有关的配置文件。
/etc/tomcat5/server.xml #tomcat的配置文件,可以修改一些服务的参数,当然修改监听端口也在这,对我没用,因为暂时不需要修改。
/var/lib/tomcat5/webapps/ROOT #这个便是tomcat的根目录,顾名思义是保存网页内容的地方,首先将默认的文件以及目录清空。
下面开始配置nginx:
nginx的配置文件如下:
user nobody;
worker_processes 4;
error_log logs/error.log;
error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include conf/mime.types;
default_type application/octet-stream;
include /usr/local/nginx/conf/proxy.conf;
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#开启gzip模块
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;
#设定access log
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# keepalive_timeout 65; (这个参数如果启用,会出现未知错误,因此暂时取消)
upstream mysvr {
server 192.168.206.101:8080 weight=1;(优先级,优先级高的先访问)
server 192.168.206.102:8080 weight=2;
}
server {
listen 80;
server_name localhost;
charset utf-8;
access_log logs/host.access.log main;
location / {
root html;
index index.jsp index.htm;
proxy_pass
http://mysvr
;
}
location /status {
stub_status on;
access_log off;
auth_basic "status";
auth_basic_user_file conf/htpasswd;
}
#访问:
http://192.168.206.100/status/
会提示输入账号密码
#htpasswd用法:首先在conf/下建立htpasswd文件,touch htpasswd
#htpasswd -b htpasswd user password 就ok了。
#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$ {
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include conf/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;
#
# }
}
其实后面#号开头的都可以去掉,因为这些功能目前我都用不上,对了,差点忘记上proxy.conf文件了
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
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;
# service nginx start
启动 nginx: [确定]
就说明没有问题了。
于是访问
http://192.168.206.100
就可以访问到两台tomcat上的相关网页内容了。以上是我一个星期的研究结果,配置文件我到现在也没有全部搞懂,有待继续研究,也有些小问题。还有我用webbench做了压力测试,因为是在虚拟机中测试的,不能完反映真实情况,不过效果还是不错滴,等着正式上线,继续优化,今天先到这了。哈哈!!!
本文来自ChinaUnix博客,如果查看原文请点:
http://blog.chinaunix.net/u2/70648/showart_1095075.html
作者:
qingheliu
时间:
2008-12-04 14:42
提示:
作者被禁止或删除 内容自动屏蔽
作者:
qingheliu
时间:
2008-12-04 14:42
提示:
作者被禁止或删除 内容自动屏蔽
作者:
qingheliu
时间:
2008-12-10 09:08
提示:
作者被禁止或删除 内容自动屏蔽
作者:
qingheliu
时间:
2008-12-10 09:08
提示:
作者被禁止或删除 内容自动屏蔽
作者:
todayboy
时间:
2009-01-12 18:47
不错,测试中!
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2