初次使用nginx,tomcat项目中做负载均衡

这个是自己理解来做的难过

1,首先准备了2个tomcat服务器

第一个tomcat修改每一个的配置文件Server.xml端口修改为8080

第二个tomcat修改server.xml文件修改的地方比较多,如下

端口修改为6666

2,每一个tomcat的webapps下面应该都有一个examples项目,就用这个项目了,打开examples项目,修改index.html文件,里面的body文件内容不一样就行了,等访问他们的时候能识别即可

3,下载nginx的win版本,安装到c盘根目录

4,打开nginx,C:\nginx\conf,里面找到nginx.conf,有的文件好像叫mynginx.conf文件

这个是主要配置

这个配置是我看别人怎么配置,在加上自己理解,可能存在问题

5,
worker_processes  2;   #这个应该是线程数

error_log   c:\\nginx\\logs\\error.log crit;   #日志的输出位置


pid        c:\\nginx\\logs\\nginx.pid;     #pid的位置


worker_rlimit_nofile 51200;         


events {
    worker_connections  51200;
}


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"';
    charset gb2312,utf-8;


    server_names_hash_bucket_size 128;
    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay on;

    keepalive_timeout  300;

    upstream demo {                                                                     #这个demo配置是自己可以随便来填写的

        #weigth参数表示权值,权值越高被分配到的几率越大

        #本机上的Squid开启3128端口

        #server 192.168.8.1:3128 ;

        server localhost:8080 weight=2;                                       #这个代表你要负载的一个服务器地址,我的地址都是localhost,端口是8080

        server localhost:6666 weight=2;                                       #这个代表你要负载的一个服务器地址,我的地址都是localhost,端口是6666  2个tomcat代表不同的服务器了

        #server 192.168.0.119:92 weight=2;

    }
    server {
        listen       80;                                      
        server_name  127.0.0.1;                                                     #这个是你要访问的主机的地址,然后动态的分配到上面的2个地址,当然这个主机的地址也是本机

        #charset koi8-r;
        log_format  nobody_chenlb_com  '$remote_addr - $remote_user [$time_local] $request '  
                      '"$status" $body_bytes_sent "$http_referer" "$http_x_forwarded_for"';
        access_log  c:\\nginx\\logs\\access.log main;
  
        location / {
            proxy_pass http://demo;                                                  #这个demo也跟上面的demo一样,但是不知道有什么作用,待以后开发
            proxy_redirect default;
            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;
        }
    }
    
}


7,这个配置有很少的地方是自己来填的,基本都是固定的写法.明白主要的意思,别的配置属性也可以慢慢来研究

8,测试,到tomcat的bin目录双击startup.bat,启动2个tomcat,到nginx目录双击nginx.exe,打开nginx

9,访问地址http://127.0.0.1/examples/  就可以负载均衡的分配到2个服务器了,如果你刷新页面就可以看到每次访问的tomcat都不一样

因为你tomcat下的examples下的页面是不一样的

10,访问nginx服务器地址,他关联的2个tomcat地址,会自动的分配




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值