首页 > 其他 > 详细

nginx+tomcat 负载均衡

时间:2018-05-24 01:00:17      阅读:480      评论:0      收藏:0      [点我收藏+]

第一篇nginx for windows 安装:https://www.cnblogs.com/blogxiao/p/8761734.html

第二篇: tomcat 的部署:

1.nginx可以作为反向代理服务器,作为客户端发送请求的门户,这个门户将接受的请求分均衡发给多个应用服务器,达到高负载的效果,典型的就比如tomcat应用服务器。

2.解释一下反向代理:正向和反向是针对服务器来说的,服务器发送给客户端的是正向,客户端向服务器发起的是反向。

3.先下载一个tomcat,然后拷贝成两份

技术分享图片

4.在eclipse新建一个简单的web项目,将生成的放到tomcat 的webapps 的目录下

5.开始配置tomcat ,tomcat 最主要的就是端口号 和 项目根路径,配置tomcat 的server 文件,文件的路径tomcat1->conf->server.xml

tomcat1端口号配置

<Server port="8006" shutdown="SHUTDOWN">

<Connector connectionTimeout="20000" port="8090" protocol="HTTP/1.1" redirectPort="8443"/>

<Connector port="8001" protocol="AJP/1.3" redirectPort="8443"/>

项目根路径配置

<Host appBase="D:\nginx\tomcat\tomcat1\webapps" autoDeploy="true" name="localhost" unpackWARs="true">

  <Context docBase="D:\nginx\tomcat\tomcat1\webapps\springmvcmaven" path="/" reloadable="true" />

</Host>

appBase 项目的根目录  docBase 是项目路径 ,这里的项目名称是springmvcmaven

springmvcmaven 文件

技术分享图片

测试访问http://localhost:8090/index.jsp

技术分享图片

 

 

 

 

 

 这里的tomcat1 是为了区分nginx 反向代理了哪个应用服务器

 

tomcat2 端口配置

<Server port="8007" shutdown="SHUTDOWN">

<Connector connectionTimeout="20000" port="8070" protocol="HTTP/1.1" redirectPort="8553"/>

<Connector port="8002" protocol="AJP/1.3" redirectPort="8553"/>

项目访问路径的配置

<Host appBase="D:\nginx\tomcat\tomcat2\webapps" autoDeploy="true" name="localhost" unpackWARs="true">

      <Context docBase="D:\nginx\tomcat\tomcat2\webapps\springmvcmaven" path="/" reloadable="true" />

</Host>

访问地址:http://localhost:8070/index.jsp

技术分享图片

 

6.nginx 的配置,找到nginx->conf->nginx.conf 文件进行修改

#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  localhost {  #服务器集群名字  
    #集群进行负载均衡有多重策略,这里使用权重的方式进行测试 
        server    127.0.0.1:8070  weight=1;#服务器配置   weight是权重的意思,权重越大,分配的概率越大。  
        server    127.0.0.1:8090  weight=2;  
    } 

    server {
        #nginx 监听的端口,这里的端口设置要注意不要有端口冲突,不然nginx启动会失败
        listen       8060;
        #访问的是服务名称 可以是域名,localhost,ip
        server_name  localhost;
     
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #设置nginx作为反向代理服务器
        location / {
            proxy_pass http://localhost;  
            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;
    #    }
    #}

}

 

在dos命令中,找到nginx 的路径,启动nginx

技术分享图片

7.测试访问:输入地址:http://localhost:8060

第一次返回的是

技术分享图片

 

第二次返回的是:

技术分享图片

多访问几次就可以看到效果。

总结:此篇文章的nginx+tomcat 负载均衡只是很简单的配置,仅供入门。

 

nginx+tomcat 负载均衡

原文:https://www.cnblogs.com/blogxiao/p/9074329.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!