linux下的nginx+tomcat负载均衡

本文介绍了如何在Linux环境下通过Nginx实现对两台Tomcat服务器的负载均衡配置。首先在两台Redhat虚拟机上分别安装JDK和Tomcat,然后在一台上安装Nginx。详细讲述了Nginx的安装过程,包括解决编译错误。接着重点讲解了Nginx的配置文件`nginx.conf`的修改,特别是upstream模块的设置,确保即使关闭一台Tomcat服务器,应用仍能正常访问。最后,强调了负载均衡后的访问方式,并提醒读者注意端口设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

试验环境:

   两台redhat虚拟机

一台:192.168.1.20

二台:192.168.1.11

(1.20上面装nginx,jdk,tomcat ; 1.11上面装jdk,tomcat。)



1.安装jdk(这是必须的,网上版本很多,这里也不详细说明)

2.安装tomcat(不要问我tomcat怎么弄,特意转载了一个说的很详细的博客,如果你说看不懂我只能是醉了

3.安装nginx

 1.下载pcre  (http://www.pcre.org/ 官网下载)

   tar jxvf pcre-8.12.tar.bz2

   cd pcre-8.12

  ./configure

  make && make install 

 

 2.下载nginx ( http://nginx.org/官网下载)

 3.解压 tar zxvf nginx-1.7.8.tar.gz

   cd nginx-1.7.8

  ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

 make && make install 


编译时报这样的错:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.


yum -y install openssl openssl-devel 安装完再重新编译就Ok了。


 4. nginx+tomcat负载:

 整合主要是修改nginx.conf配置,给一个完整的nginx.conf线上配置,部分参数可以根据实际需求修改。

  



#user  nobody;
worker_processes  4;


#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


pid        /usr/local/nginx/logs/nginx.pid;




events {
    use epoll;
    worker_connections  10240;
}




http {
    include       mime.types;
    default_type  application/octet-stream;
    fastcgi_intercept_errors on;   
    charset  utf-8;   
    server_names_hash_bucket_size 128;   
    client_header_buffer_size 4k;   
    large_client_header_buffers 4 32k;   
    client_max_body_size 300m;   
    sendfile on;   
    tcp_nopush     on;  


   
     keepalive_timeout 60; 


     tcp_nodelay on;   
     client_body_buffer_size  512k;   
    
       proxy_connect_timeout    5;   
       proxy_read_timeout       60;   
       proxy_send_timeout       5;   
       proxy_buffer_size        16k;   
       proxy_buffers            4 64k;   
       proxy_busy_buffers_size 128k;   
       proxy_temp_file_write_size 128k;




    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;


   
    #tcp_nopush     on;


    #keepalive_timeout  0;
    
 
    #gzip on
      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-javascript text/css application/xml;   
      gzip_vary on;   
  


   
    upstream web {
              server 192.168.1.20:8080 weight=1;
              server 192.168.1.11:8080 weight=1;  
             }
       
        
    server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / 
{
         proxy_next_upstream http_502 http_504 error timeout invalid_header;   
         proxy_set_header Host  $host;   
         proxy_set_header X-Real-IP $remote_addr;   
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   
         proxy_pass http://web;   
         expires      3d;     
         
        }
          


  
        #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 ssl;
    #    server_name  localhost;


    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;


    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;


    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


}



这样就Ok了。

 关闭其中一台的tomcat,去访问192.168.1.20也是能正常访问的。


注意:

 访问192.168.1.11是不行的,nginx已经整合了,所以访问1.20就可以了。因为我对Nginx不是很熟悉,所以当我搭建完nginx+tomcat负载之后,我以为能得到的结果并没有出现,我以为我做的有问题,纠结了半天,最后只能呵呵了。


 因为我的两个tomcat端口都被我改成了8080,所以我访问tomcat的时候是要加8080的,也就是http://192.168.1.20:8080 这样可以访问到应用,nginx+tomcat负载完成之后,访问http://192.168.1.20 也可以直接访问到应用,其实这样就对了,你可以试着把20上面的tomcat关了,继续访问,也是可以访问到的。


最近学习Nginx+tomcat实现 负载均衡。 首先大家注意: 本文章中没有session共享,关于session共享我会在下一篇中讲解,先实现Nginx+tomcat负载均衡再实现session共享。 从网上查了好多资料,多走了很多弯路,现在把自己成功的方法拿出来与大家分享。 Window7 我是在Win7上做的。不是什么Linux,网上好多资料,特别麻烦。 Nginx Nginx 比较好找到,直接去网上下载 网址: http://nginx.org/en/download.html 版本不作要求了,(比如1.2.9版本),都有。 JDK JAVA的各种环境都要有。 版本不要求 Tomcat 这里我给大家提供tomcat6 Tomcat各种版本的下载地址我也提供给大家:http://tomcat.apache.org/download-60.cgi 大家可以先用我tomcat6 学会了,在下载自己需要的版本。 词条科普 另外我把实现过程中遇到的知识点都总结好了,一起提供给大家学习。 步骤: 注:本例程以一台win7机器为例子,即同一台机器上装一个nginx2个Tomcat。 且安装了JDK。 便于管理将用到的资料放在一个文件夹下 我在D盘 创建 server 文件夹 . 1. Nginx 下载直接解压缩到server,点nginx.exe 执行 安装后如果可用,可在任务管理其中找到如图类似,并且在浏览器中输入 http://localhost/ 浏览器显示如下两个图 说明成功 2.Tomcat 同样将自己下载的或者我提供的tomcat 放到D盘的server下不过要复制成两份或者多份。 命名如:(便于区别 我们只用两个来讲解 ,多个tomcat两个原理是一样的) 1、server.xml配置 我们需要在一台机器上跑 2 个不同的 tomcat ,避免出现端口被占用的情况,为了规范统一,我们修改全部tomca端口。分别找到tomcat6的12 的conf下的 server.xml。 修改Server端口 找到Server将: 改为 XXXX 在这里表示不同的端口:我的两个 tomcat 分别使用 80058006; 2.1.2、修改Connector端口 找到Connector将: 改为 XXXX 在这里表示不同的端口:我的两个 tomcat 分别使用 80818082; 2.1.3、修改Engine端口 找到Engine将: 改为 tomcatX 在这里表示不同的tomcat,我的两个 tomcat 分别使用 tomcat1tomcat2;来区分。 这个设置是主要用以tomcat的集群。 如果看不懂可以去看我提供的tomcat我已经改好了。 启动tomcat服务 分别到两个tomcat下,直接双击D:\server\apache-tomcat-6.0.39_1\bin\startup.bat启动tomcat1 D:\server\apache-tomcat-6.0.39_2\bin\startup.bat启动tomcat2 出现以下页面表示启动成功 在浏览器中输入 http://localhost:8081 http://localhost:8082 出现 标示成功 3、Nginx+Tomcat负载均衡配置 首先创建两个文件,这两个文件 我来提供,将这两个文件拷入Nginx的conf文件夹下 1.proxy.conf 文件内容 #负责代理转发 proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值