Nginx+Tomcat负载均衡、动静分离
来源:互联网 发布:卖数据的联系方式 编辑:程序博客网 时间:2024/06/02 06:17
一、负载均衡+动静分离
修改nginx/conf/nginx.conf,修改完后如下:
Xml代码
- #user nobody;
- worker_processes 1;
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- error_log logs/error.log info;
- pid logs/nginx.pid;
- events {
- worker_connections 1024;
- }
- 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;
- #设定请求缓冲
- server_names_hash_bucket_size 128;
- client_header_buffer_size 32k;
- large_client_header_buffers 4 32k;
- 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;
- client_header_timeout 3m;
- client_body_timeout 3m;
- send_timeout 3m;
- gzip on;#开启gzip,节省带宽
- gzip_min_length 1100;
- gzip_buffers 4 8k;
- gzip_types text/plain text/css application/x-javascript image/bmp application/javascript;
- output_buffers 1 32k;
- postpone_output 1460;
- limit_rate_after 3m;#限速模块,前3M下载时不限速
- limit_rate 512k; #限速模块
- server {
- listen 80;
- server_name localhost;
- #charset koi8-r;
- access_log logs/host.access.log;
- #自动补全"/"
- if (-d $request_filename){
- rewrite ^/(.*)([^/])$ http://$host/$1$2/ last;
- }
- ###################动静分离配置#######################
- ###################动态访问转向tomcat处理#######################
- #location ~ \.(jsp|page|do)?$ {
- # proxy_set_header Host $host;
- # proxy_set_header X-Real-IP $remote_addr;
- # proxy_pass http://mcul;#mcul与负载均衡upstream配置名称mcul一致
- #}
- ###################设定访问静态文件直接读取不经过tomcat#########
- #location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
- # expires 30d;
- #}
- #location ~ .*\.(js|css)?$ {
- # expires 1h;
- #}
- ####################所有请求均有Tomcat处理############
- location / {
- root html;
- index index.html index.htm;
- proxy_pass http://mcul; #mcul与负载均衡upstream配置名称mcul一致
- }
- ###################动静分离配置#######################
- ##################404错误页#################
- error_page 404 /404.html;
- location = /40x.html {
- root html;
- }
- ##################404错误页#################
- # redirect server error pages to the static page /50x.html
- #
- ##################50x错误页#################
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- ##################50x错误页#################
- # 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;
- # }
- #}
- ############负载均衡配置###########
- upstream mcul {
- server 127.0.0.1:8080 weight=1;
- server 127.0.0.1:9080 weight=2;
- }
- ############负载均衡配置###########
- # 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;
- # }
- #}
- }
二、Session复制
1.修改Tomcat/conf/server.xml, 在<Engine name="." ..></Engine>标签中最后处添加以下代码:
注意:对于这一步各个版本的Tomcat代码可能,具体可看http://localhost:8080/docs/cluster-howto.html
Xml代码
- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
- channelSendOptions="6">
- <Manager className="org.apache.catalina.ha.session.BackupManager"
- expireSessionsOnShutdown="false"
- notifyListenersOnReplication="true"
- mapSendOptions="6"/>
- <!--
- <Manager className="org.apache.catalina.ha.session.DeltaManager"
- expireSessionsOnShutdown="false"
- notifyListenersOnReplication="true"/>
- -->
- <Channel className="org.apache.catalina.tribes.group.GroupChannel">
- <Membership className="org.apache.catalina.tribes.membership.McastService"
- address="228.0.0.4"
- port="45564"
- frequency="500"
- dropTime="3000"/>
- <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
- address="auto"
- port="5000"
- selectorTimeout="100"
- maxThreads="6"/>
- <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
- <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
- </Sender>
- <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
- <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
- <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
- </Channel>
- <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
- filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/>
- <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
- tempDir="/tmp/war-temp/"
- deployDir="/tmp/war-deploy/"
- watchDir="/tmp/war-listen/"
- watchEnabled="false"/>
- <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
- </Cluster>
2.修改应用的web.xml,在<web-app>结束标签前追加 <distributable/>
三、重启,测试
0 0
- Nginx+Tomcat负载均衡、动静分离
- nginx+tomcat负载均衡、动静分离
- Nginx + Tomcat 动静分离实现负载均衡
- Nginx + Tomcat 动静分离实现负载均衡
- Nginx + Tomcat 动静分离实现负载均衡
- Nginx + Tomcat 动静分离实现负载均衡
- Nginx + Tomcat 动静分离实现负载均衡
- Nginx + Tomcat 动静分离实现负载均衡
- Nginx + Tomcat 动静分离实现负载均衡
- Nginx + Tomcat 动静分离实现负载均衡
- Nginx + Tomcat 动静分离实现负载均衡
- Nginx + Tomcat 实现动静分离负载均衡
- Nginx+Tomcat 动静分离实现负载均衡
- Nginx+Tomcat 动静分离实现负载均衡
- Nginx + Tomcat 动静分离实现负载均衡
- Nginx+Tomcat 实现动静分离、负载均衡
- Nginx + Tomcat 动静分离实现负载均衡
- nginx+tomcat+memcache负载均衡动静分离
- Apache+Tomcat 负载均衡 Session共享
- Java调用C/C++编写的第三方dll动态链接库(非native API)--- JNI
- 不积跬步无以至千里(C语言笔记)
- java线程池
- JAVA新IO
- Nginx+Tomcat负载均衡、动静分离
- Java查漏补缺系列——循环中label的使用
- Servlet中doGet与doPost的区别..
- HttpURLConnection的URL包含中文字符时乱码的解决办法
- Nginx+Tomcat负载均衡Session复制解决方案
- 猫猫学IOS(四)UI之半小时搞定Tom猫
- 采用em标签制作的列表三角形标号
- 一个能支持一千人的系统,如果突然来了一百万用户该怎么办?
- 集群中几种SESSION同步解决方案的比较