最近研究了一下 tomcat+nginx负载均衡,在这里写一下日志,省得过段时间忘记了,也供大家学习吧,入门级的适合新手哦
不明白的可以加我QQ 695438455
我是搭建的windows版本的,懒得搭建linux服务器了,原理都差不多,将来有时间再补充一下linux版本的吧
两个tomcat 分别是
tomcat1:8080和tomcat2:8081(64位的解压缩版)根据自己的机器,自己下载吧
nginx1.2.9 下载地址 http://nginx.org/en/download.html
先说一下 【tomcat1】 的配置
修改两三个地方就可以了
<Server port="8005" shutdown="SHUTDOWN"> 这个地方的端口多个tomcat的时候就得修改了,自己随便改就可以了,别冲突就行
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />这个端口大家都知道怎么修改,要是一台机器多个tomcat 就把这个端口改掉就可以了 自己随便修改
<Connector port="8029" protocol="AJP/1.3" redirectPort="8443" /> 这个地方的端口多个tomcat的时候就得修改了,自己随便改就可以了,别冲突就行
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2"> 注意一下 jvmRoute="tomcat1"
【tomcat2】 的配置
修改两三个地方就可以了
<Server port="8006" shutdown="SHUTDOWN"> 这个地方的端口多个tomcat的时候就得修改了,自己随便改就可以了,别冲突就行
<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />这个端口大家都知道怎么修改,要是一台机器多个tomcat 就把这个端口改掉就可以了 自己随便修改
<Connector port="8019" protocol="AJP/1.3" redirectPort="8443" /> 这个地方的端口多个tomcat的时候就得修改了,自己随便改就可以了,别冲突就行
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2"> 注意一下 jvmRoute="tomcat2"
修改完毕以后,把两个tomcat同时启动一下,看看能否启动成功,如果成功了就可以了,接下来说一下nginx的配置
修改 conf文件夹 下的【nginx.conf【文件我直接把我的配置贴上来,供大家看一下吧
#user nobody;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 2048;
}
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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include gzip.conf;
include Proxy.conf;
#这个地方配置的就是我们弄得两个tomcat,启动loclhost根据自己情况自行修改例如修改为自己的域名:www.xxx.com
upstream localhost {
#ip_hash
#ip_hash;
server localhost:8080 weight=1;
server localhost:8081 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm index.jsp;
proxy_pass http://localhost;#这个地方记得配置,不配置是不会转发的
}
#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 HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
下面是其他的两个conf文件,仔细看上面的内容的话,我导入了两个conf文件 include 分别是 Gzip和Proxy
这两个文件是没有的,需要自己创建
<pre name="code" class="html">Gzip.conf
gzip on;
gzip_min_length 1000;
gzip_types text/plain text/css application/x-javascript;
</pre><pre code_snippet_id="1582015" snippet_file_name="blog_20160218_8_9209302" name="code" class="html">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 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
都添加完以后,就可以启动 nginx看效果了
下面附上我的效果图
<strong>第一次访问</strong>
<img src="https://img-blog.csdn.net/20160218231337729?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
test是我创建的一个测试项目,都会把资源放到百度云盘 有个test的文件,下载了直接放到 tomcat的webapps下就可以了,两个tomcat都要放
好了就写到这里吧,下面给大家提供几个 nginx详细的配置链接
http://www.cnblogs.com/xiaogangqq123/archive/2011/03/02/1969006.html