`

搭建nginx+tomcat+Java的负载均衡环境【转】

阅读更多

一、简介:

Tomcat在高并发环境下处理动态请求时性能很低,而在处理静态页面更加脆弱。虽然Tomcat的最新版本支持epoll,但是通过Nginx来处理静态页面要比通过Tomcat处理在性能方面好很多。

二、下载安装:

下载nginx

http://nginx.org/en/download.html

下载解压后放到C:\nginx-1.0.4(官网这样要求的,不知道放其它盘有没有问题)

启动nginx.exe,然后在浏览器输入127.0.0.1即可

配置自己的项目测试

第二环节我们使用了默认的nginx.conf 。Nginx的配置文件都存于目录conf文件下,其中nginx.conf是它的主配置文件。

以下为我加上注释并配置的新的虚拟server]

Java代码  收藏代码
  1. #运行用户  
  2. #user  nobody;  
  3. #开启进程数 <=CPU数  
  4. worker_processes  1;  
  5. #错误日志保存位置  
  6. #error_log  logs/error.log;  
  7. #error_log  logs/error.log  notice;  
  8. #error_log  logs/error.log  info;  
  9. #进程号保存文件  
  10. #pid        logs/nginx.pid;  
  11.    
  12. #等待事件  
  13. events {  
  14.     #Linux下打开提高性能  
  15.     #use epoll;  
  16.     #每个进程最大连接数(最大连接=连接数x进程数)  
  17.     worker_connections  1024;  
  18. }  
  19.    
  20.    
  21. http {  
  22.     #文件扩展名与文件类型映射表  
  23.     include       mime.types;  
  24.     #默认文件类型  
  25.     default_type  application/octet-stream;  
  26.     #日志文件输出格式 这个位置相于全局设置  
  27.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  28.     #                  '$status $body_bytes_sent "$http_referer" '  
  29.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  30.        
  31.     #请求日志保存位置  
  32.     #access_log  logs/access.log  main;  
  33.        
  34.     #设定请求缓冲  
  35.     client_header_buffer_size 1k;  
  36.     large_client_header_buffers 4 4k;  
  37.    
  38.     #打开发送文件  
  39.     sendfile        on;  
  40.     #tcp_nopush     on;  
  41.    
  42.     #keepalive_timeout  0;  
  43.     keepalive_timeout  65;  
  44.        
  45.     #客户端上传文件大小控制  
  46.     client_max_body_size 8m;  
  47.        
  48.     #打开gzip压缩  
  49.     #gzip  on;  
  50.        
  51.     #设定负载均衡的服务器列表  
  52.     #upstream mysvr {  
  53.     #    #weigth参数表示权值,权值越高被分配到的几率越大  
  54.     #    #本机上的Squid开启3128端口  
  55.     #    #server 192.168.8.1:3128 weight=5;  
  56.     #    #server 192.168.8.2:80 weight=1;  
  57.     #    #server 192.168.8.3:80 weight=6;  
  58.     #}  
  59.    
  60.     #第一个虚拟主机  
  61.     server {  
  62.         #监听IP端口  
  63.         listen       80;  
  64.         #主机名  
  65.         server_name  localhost;  
  66.         #root    
  67.            
  68.         #设置字符集  
  69.         #charset koi8-r;  
  70.         #本虚拟server的访问日志 相当于局部变量  
  71.         #access_log  logs/host.access.log  main;  
  72.         #日志文件输出格式  
  73.         #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  74.         #                  '$status $body_bytes_sent "$http_referer" '  
  75.         #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  76.            
  77.         location / {  
  78.             root   html;  
  79.             index  index.html index.htm;  
  80.         }  
  81.            
  82.         #静态文件缓存时间设置  
  83.         #location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${           
  84.         #    expires 30d;  
  85.         #}  
  86.            
  87.         #静态文件缓存时间设置  
  88.         #location ~ .*\.(js|css)?${           
  89.         #    expires 1h;  
  90.         #}  
  91.            
  92.         #对本server"/"启用负载均衡  
  93.         #location / {  
  94.         #    proxy_pass http://mysvr;  
  95.         #    proxy_redirect off;  
  96.         #    proxy_set_header Host $host;  
  97.         #    proxy_set_header X-Real-IP $remote_addr;  
  98.         #    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  99.         #    client_max_body_size 10m;  
  100.         #    client_body_buffer_size 128k;  
  101.         #    proxy_connect_timeout 90;  
  102.         #    proxy_send_timeout 90;  
  103.         #    proxy_read_timeout 90;  
  104.         #    proxy_buffer_size 4k;  
  105.         #    proxy_buffers 4 32k;  
  106.         #    proxy_busy_buffers_size 64k;  
  107.         #    proxy_temp_file_write_size 64k;  
  108.         #}  
  109.            
  110.         #设定查看Nginx状态的地址  
  111.         #location /NginxStatus {  
  112.         #    stub_status on;  
  113.         #    access_log on;  
  114.         #    auth_basic “NginxStatus”;  
  115.         #    auth_basic_user_file conf/htpasswd;  
  116.         #}  
  117.    
  118.    
  119.    
  120.         #error_page  404              /404.html;  
  121.    
  122.         # redirect server error pages to the static page /50x.html  
  123.         #  
  124.         error_page   500 502 503 504  /50x.html;  
  125.         location = /50x.html {  
  126.             root   html;  
  127.         }  
  128.    
  129.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  130.         #  
  131.         #location ~ \.php$ {  
  132.         #    proxy_pass   http://127.0.0.1;  
  133.         #}  
  134.    
  135.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  136.         #  
  137.         #location ~ \.php$ {  
  138.         #    root           html;  
  139.         #    fastcgi_pass   127.0.0.1:9000;  
  140.         #    fastcgi_index  index.php;  
  141.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  142.         #    include        fastcgi_params;  
  143.         #}  
  144.    
  145.         # deny access to .htaccess files, if Apache's document root  
  146.         # concurs with nginx's one  
  147.         #  
  148.         #location ~ /\.ht {  
  149.         #    deny  all;  
  150.         #}  
  151.     }  
  152.    
  153.    
  154.     # another virtual host using mix of IP-, name-, and port-based configuration      
  155.     server {  
  156.         #多监听         
  157.         listen       localhost:8666;  
  158.         #主机名  
  159.         server_name  LIULJ2576;  
  160.         #WEB文件路径  
  161.         root         E:/Portal;  
  162.         #默认首页  
  163.         index        HomePage.html;          
  164.         #location / {  
  165.         #    #这里相当于局部变量  
  166.         #    root   E:/Portal;  
  167.         #    index  HomePage.html;  
  168.         #}  
  169.     }  
  170.    
  171.    
  172.     # HTTPS server HTTPS SSL加密服务器  
  173.     #  
  174.     #server {  
  175.     #    listen       443;  
  176.     #    server_name  localhost;  
  177.    
  178.     #    ssl                  on;  
  179.     #    ssl_certificate      cert.pem;  
  180.     #    ssl_certificate_key  cert.key;  
  181.    
  182.     #    ssl_session_timeout  5m;  
  183.    
  184.     #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
  185.     #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;  
  186.     #    ssl_prefer_server_ciphers   on;  
  187.    
  188.     #    location / {  
  189.     #        root   html;  
  190.     #        index  index.html index.htm;  
  191.     #    }  
  192.     #}  
  193.    
  194. }  

 #号为注释内容,我们在cmd下运行nginx

启动成功,出错的话,可以查询日志(日志路径是配置文件指定的,你可以修改存到其它位置)

访问一下第二个server 配置的localhost:8666地址,结果出现

 

三、Nginx可以通过以下两种方式来实现与Tomcat的耦合:

将静态页面请求交给Nginx,动态请求交给后端Tomcat处理。

将所有请求都交给后端的Tomcat服务器处理,同时利用Nginx自身的负载均衡功能进行多台Tomcat服务器的负载均衡。

下面通过两个配置实例分别讲述这两种实现

一、动态页面和静态页面分离的实例

这里假定Tomcat服务器的IP地址为192.168.12.130,同时Tomcat服务器开放的服务器端口为8080。Nginx相关配置代码如下:

Java代码  收藏代码
  1. server {   
  2.       listen 80;    
  3.       server_name www.ixdba.net;    
  4.       root /web/www/html;    
  5.     
  6. location /img/ {    
  7.       alias /web/www/html/img/;    
  8. }    
  9.     
  10. location ~ (\.jsp)|(\.do)$ {    
  11.      proxy_pass http://192.168.12.130:8080;    
  12.      proxy_redirect off;    
  13.      proxy_set_header Host $host;    
  14.      proxy_set_header X-Real-IP $remote_addr;    
  15.      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    
  16.      client_max_body_size 10m;    
  17.      client_body_buffer_size 128k;    
  18.      proxy_connect_timeout 90;    
  19.      proxy_send_timeout 90;    
  20.      proxy_read_timeout 90;    
  21.      proxy_buffer_size 4k;    
  22.      proxy_buffers 4 32k;    
  23.      proxy_busy_buffers_size 64k;    
  24.      proxy_temp_file_write_size 64k;    
  25. }    
  26.     
  27. }  

 在这个实例中,首先定义了一个虚拟主机www.ixdba.net,然后通过location指令将/web/www/html/img/目录下的静态文件交给Nginx来完成。最后一个location指令将所有以.jsp、.do结尾的文件都交给Tomcat服务器的8080端口来处理,即http://192.168.12.130:8080

需要特别注意的是,在location指令中使用正则表达式后,proxy_pass后面的代理路径不能含有地址链接,也就是不能写成http://192.168.12.130:8080/,或者类似http://192.168.12.130:8080/jsp的形式。在location指令不使用正则表达式时,没有此限制。

2、多个tomcat负载均衡的实例

这里假定有3台Tomcat服务器,分别开放不同的端口,地址如下:

1
2
3
192.168.12.131:8000 
192.168.12.132:8080 
192.168.12.133:8090

Nginx的相关配置代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
upstream mytomcats { 
      server 192.168.12.131:8000
      server 192.168.12.132:8080
      server 192.168.12.133:8090
  
server { 
      listen 80
      server_name www.ixdba.net; 
  
location ~* \.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ { 
       root /web/www/html/; 
  
location / { 
          proxy_pass http://mytomcats; 
          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; 

分享到:
评论

相关推荐

    Windows+Nginx+Tomcat搭建负载均衡和集群环境同时实现

    很好的学习Windows+Nginx+Tomcat搭建负载均衡和集群环境教材,里面有详细的Demo。

    nginx+tomcat+memcached负载均衡集群搭建许jar包大全

    Tomcat要支持memcached管理Session,需要调用一些jar库文件如下(网上有的文章中可能所说的jar包不全,或者版本不样的会报错,但这里我已经经过验证了): 1) couchbase-client-1.2.2.jar 2) javolution-5.5.1....

    Nginx+Tomcat负载均衡配置教程

    手把手教你搭建Nginx+Tomcat 集群的搭建,多个节点,实现负载均衡最全教程,图文教程

    Nginx实现tomcat与weblogic集群的负载均衡及故障处理

    1、 介绍安装步骤,重点注意事项; 2、 集群规划,搭建主要步骤,重点注意事项; 3、 编制简单脚本进行优化,参数调优。 4、 最全面、最实用 5、 教程适用于各种版本 ...脚本进行优化,tomcat参数调优

    Linux运维从入门到高级全套案例v3.rar

    目录 Linux入门篇 操作系统简介 1.2 Linux发展趋势 3 Linux系统安装 4 Linux学习技巧 2. Linux系统篇…… 2.1 Linux系统管理… 2.1.1 Linux目录初识 2.1.2 Linux常用命令 ...5.8Ls+ Keepalived负载均衡

    Linux运维从入门到高级全套案例v3

    3.1. 1 构建NTP时间服务器 25 3.1. 2 构建DHCP服务器 27 3.1. 3 搭建Samba服务器 29 3.1. 4 搭建NFS服务器 32 ...7. Nginx负载均衡机制及常见问题 124 8. Linux运维职业规划 127 9. Linux运维面试总结 127

    开涛高可用高并发-亿级流量核心技术

    2.9 Nginx四层负载均衡 39 2.9.1 静态负载均衡 39 2.9.2 动态负载均衡 41 参考资料 42 3 隔离术 43 3.1 线程隔离 43 3.2 进程隔离 45 3.3 集群隔离 45 3.4 机房隔离 46 3.5 读写隔离 47 3.6 动静隔离 48 3.7 爬虫...

    基于SSM的校园订餐平台+源代码+文档说明

    #### 使用Nginx实现负载均衡,使用Redis缓存技术 ### License Apache 2.0 -------- &lt;项目介绍&gt; 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! 1...

    趁早(quickearly)B2C早餐外卖微信小程序

    配置三个节点的Nginx作为负载均衡器,第一个Nginx负责请求分发给后面两个Nginx,最后再把请求发送到不同的Tomcat容器 使用Redis Cluster作为早餐详细页面的第一级缓存 配置使用Ehcache作为页面的第二级缓存,并设置...

    2-21 第1周总结

    寒假的情况 这个假期过得超级舒服,因为疫情的原因连拜年也省了呢(^ ▽ ^),每天大胆地发呆连划水...后端暂时用的是原始的Serverlet,其他的东西也就是顺手搭建了一个Tomcat+Nginx负载均衡,给Tomcat编译了一个APR的ap

    平台设计方案.doc

    ( 中间件技术 系统采用的中间件技术为当前最主流的中间件技术(nginx+tomcat),同时满足系统可 用性及稳定性,且相应的维护人员在市场中较为充裕,可以快速上手 服务器搭建建议 建设方案图 平台服务器搭建需4台...

    SpringBoot微服务

    只要系统架构设计合理,大型项目也能用,加上nginx负载均衡,轻松实现横向扩展 spring boot 要解决的问题, 精简配置是一方面, 另外一方面是如何方便的让spring生态圈和其他工具链整合(比如redis, email, elastic...

    网络架构师148讲视频课程

    │ 第38节:Nginx的负载均衡模块.avi │ 第39节:Nginx的Rewrite模块.avi │ 第40节:更多模块的基本功能和配置.avi │ 第41节:Nginx的配置优化以及使用建议.avi │ 第42节:应用上Nginx过后的体系结构.avi │ 第43...

    Eclipse开发分布式商城系统+完整视频代码及文档

    │ 04.nginx的反向代理及负载均衡.avi │ 05.FastDFS介绍.avi │ 06.FastDFS安装步骤-文件上传.avi │ 07.配置nginx插件访问图片.avi │ 08.测试图片上传.avi │ 09.FastDFS工具类的使用.avi │ 10.图片上传过程分析...

Global site tag (gtag.js) - Google Analytics