linux(ubuntu)下搭建nginx,tomcat负载均衡环境搭建

来源:互联网 发布:淘宝美工在哪里做 编辑:程序博客网 时间:2024/06/07 06:03

随着用户访问量增加以及单台服务器处理能力瓶颈,集群可以很好的解决这个问题,集群也可以保证在单台服务器宕机系统也可以正常运行。负载均衡就是让一个集群(多台机器处理相同的业务)的机器的使用率尽可能平均。比较常用场景:一是web服务器集群,例如用户访问了www.yasin.ac.cn这个网站,通过负载均衡分配的其中一个web容器服务器进行相关的处理。另一个场景就是RPC服务中客户端请求服务也会加入负载均衡功能。
关于nginx的作用的我就不详细介绍,网上资料很多,主要利用它的反向代理以及负载均衡算法实现软件的服务均衡环境。
这里写图片描述
这里在ubuntu16.04系统下搭建这个环境,由于没有多台机器环境,我这里采用了同一台电脑安装多个tomcat的方法(分配了两个端口)
tomcat下载地址,修改其端口,并修改webapps的ROOT的内容,用于区别两个tomcat
8080端口界面

8081端口界面
接下来安装nginx,nginx必须需要pcre,gzip模块,可选ssl模块
最后在编译安装nginx

获取pcre编译安装包,在http://www.pcre.org/上可以获取当前最新的版本
解压缩pcre-xx.tar.gz包。
进入解压缩目录,执行./configure。
编译安装命令:
make & make install
获取openssl编译安装包,在http://www.openssl.org/source/上可以获取当前最新的版本。
解压缩openssl-xx.tar.gz包。
进入解压缩目录,执行./config。
make & make install
获取zlib编译安装包,在http://www.zlib.net/上可以获取当前最新的版本。
解压缩openssl-xx.tar.gz包。
进入解压缩目录,执行./configure。
make & make install
获取nginx,在http://nginx.org/en/download.html上可以获取当前最新的版本。
解压缩nginx-xx.tar.gz包。
进入解压缩目录,执行./configure
make & make install
nginx在编译安装后到目录/usr/local/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 example.com{  #集群地址    server  127.0.0.1:8080 weight=1;#单个服务地址及其权重,这个权重是默认负载均衡算法的值,越大越容易被访问    server  127.0.0.1:8081 weight=2;    }       server {        listen       80;#nginx端口        server_name  localhost;#host name        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {#             root   html;#            index  index.html index.htm;        proxy_pass http://example.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;    #    }    #}}

启动两个tomcat,可以事先参看其效果,如上两图所示
接下来在/usr/local/nginx/sbin目中运行nginx脚本启动nginx,
这里写图片描述
这里写图片描述

这里可以看到通过这个nginx可以访问到两个tomcat

这里只是简单介绍了nginx环境的搭建,关于更多的负载均衡策略会继续深入学习,如有不当之处,需要巨巨们斧正。