Nginx+Tomcat搭建集群

环境:

Windows7 64位
nginx/Windows-1.12.0
Tomcat(Springboot中内置)

Nginx

nginx使用

  1. 在官网下载,解压到磁盘e。
  2. 启动:

    e:\nginx-1.12.0>start nginx
    或
    e:\nginx-1.12.0>>nginx.exe

    注:建议使用第一种,第二种会使你的cmd窗口一直处于执行中,不能进行其他命令操作。

    默认使用80端口,日志见文件夹nginx\logs文件夹。

  3. 停止:

    e:\nginx-1.12.0>>nginx.exe -s stop
    或
    e:\nginx-1.12.0>>nginx.exe -s quit

    注:stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息。

  4. 重新载入Nginx:

    e:\nginx-1.12.0>>nginx.exe -s reload

    当配置信息修改,需要重新载入这些配置时使用此命令。

修改Nginx核心配置文件nginx.conf

  1. Nginx的基本配置:

    • 监听端口一般都为http端口:80;
    • 域名可以有多个,用空格隔开:例如 server_name www.wsyw126.cc www.wsyw126.cn;
  2. 负载均衡列表基本配置:

    • location/ {}:对什么样的后缀进行负载均衡请求,假如我们要对所有的aspx后缀的文件进行负载均衡时,可以这样写:location ~ .*.aspx$ {}
    • proxy_pass:请求转向自定义的服务器列表,这里我们将请求都转向标识为http://test.com; (需要加上http://)的负载均衡服务器列表;
    • 在负载均衡服务器列表的配置中,weight是权重,可以根据机器配置定义权重(如果某台服务器的硬件配置十分好,可以处理更多的请求,那么可以为其设置一个比较高的weight;而有一台的服务器的硬件配置比较差,那么可以将前一台的weight配置为weight=2,后一台差的配置为weight=1)。weigth参数表示权值,权值越高被分配到的几率越大;
  3. 配置如下:

    
    
    #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;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
    
        #自己定义的集群名和对应的服务器地址
        upstream test.com {#服务器集群名字
            server 127.0.0.1:8080 weight=1;
            server 127.0.0.1:8081 weight=2;
        }
    
        server {
            listen       80; #监听80端口
            server_name  localhost;#当前服务的域名,本测试为本地测试
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            #location / {
            #    root   html;
            #    index  index.html index.htm;
            #}
    
            location / {
                proxy_pass http://test.com;
                proxy_redirect default;
            }
    
    
    
            #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;
        #    }
        #}
    
    }

服务器demo

SpringBoot 项目:demo

  1. 一个指定端口号为8080,一个指定为8081。
  2. 打包生成jar包。

测试

  1. 然后运行java -jar demo-0.0.1-SNAPSHOT.jar 和demo-0.0.2-SNAPSHOT.jar。
    运行图
  2. 运行nginx。
  3. 访问http://localhost/webapi/login
    运行结果

说明

由于权重的不同,在我们不断刷新页面的过程中,显示server two的可能性会大一些


参考资料:
网上资源
备注:
转载请注明出处:http://blog.csdn.net/wsyw126/article/details/71242675
作者:WSYW126

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值