zoukankan      html  css  js  c++  java
  • Nginx+tomcat 负载均衡

     

    一、系统版本

    Nginx使用版本、tomcat使用版本:

    Nginx:nginx-1.10.2.tar.gz

    Java :Java version: 1.8.0_60, vendor: Oracle Corporation

    Tomcat:apache-tomcat-7.0.52.tar.gz apache-tomcat-7.0.52.tar.gz

    二、nginx安装

    1.安装依赖模块

    pcrezlibopensslmd5 /sha1(如果系统中没有安装相应模块,需要按照下列方式安装) 

    为依赖而安装:

    nginx-all-modules noarch 1.10.2-1.el6 epel 7.7 k

    nginx-mod-http-geoip x86_64 1.10.2-1.el6 epel 14 k

    nginx-mod-http-image-filter x86_64 1.10.2-1.el6 epel 16 k

    nginx-mod-http-perl x86_64 1.10.2-1.el6 epel 26 k

    nginx-mod-http-xslt-filter x86_64 1.10.2-1.el6 epel 16 k

    nginx-mod-mail x86_64 1.10.2-1.el6 epel 43 k

    nginx-mod-stream

     

    2.安装nginx模块(1.)

    3.安装tomcat

    从官网上直接解压下载后的tomcat,我使用了两个版本的tomcat分别装在不同机器上。

     在一台服务器(192.168.7.40)上:

     解压:tar zxvf  apache-tomcat-7.0.52.tar.gz

    启动:进入apache-tomcat-7.0.52/bin/startup.sh启动

    在一台服务器(192.168.7.221)上:

    解压:tar zxvf  apache-tomcat-7.0.63.tar.gz 

    启动:进入apache-tomcat-7.0.63/bin/startup.sh启动

    在浏览器中输入:

    http://192.168.7.221:8080/ 可以看到版本为7.0.52的界面

    http://192.168.7.40:8080/可以看到版本为7.0.63的界面

    三、配置

    当前用到的功能Nginx的功能请求转发与负载均衡。

    1. 请求转发

      conf目录下有一个nginx.conf文件,有如下配置:

       

    user root;

    worker_processes auto;

    error_log /var/log/nginx/error.log;

    pid /var/run/nginx.pid;

     

    # Load dynamic modules. See /usr/share/nginx/README.dynamic.

    include /usr/share/nginx/modules/*.conf;

     

    events {

    worker_connections 1024;

    }

     

     

    http {

    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 /var/log/nginx/access.log main;

     

    sendfile on;

    tcp_nopush on;

    tcp_nodelay on;

    keepalive_timeout 65;

    types_hash_max_size 2048;

     

    include /etc/nginx/mime.types;

    default_type application/octet-stream;

     

    # Load modular configuration files from the /etc/nginx/conf.d directory.

    # See http://nginx.org/en/docs/ngx_core_module.html#include

    # for more information.

    include /etc/nginx/conf.d/*.conf;

    }

    通过该配置文件我们可以找到默认的配置文件的路径在/etc/nginx/conf.d/*.conf;

    通过vim /etc/nginx/conf.d/default.conf得到如下配置:

    #

    # The default server

    #

     

    server {

    listen 80 default_server;

    listen [::]:80 default_server;

    server_name _;

    root /usr/share/nginx/html;

     

    # Load configuration files for the default server block.

    include /etc/nginx/default.d/*.conf;

     

    location / {

    proxy_pass http://192.168.7.221:8080;

    }

     

    error_page 404 /404.html;

    location = /40x.html {

    }

     

    error_page 500 502 503 504 /50x.html;

    location = /50x.html {

    }

     

    }

    配置介绍:

    listen 80;表示监听80端口. 
    server_name localhost; 表示转到本地,这里指Nginx的文件夹里。 
    location / 表示匹配的路径, 斜杠代表匹配所有的请求。 
    root html; 静态文件的路径,其实就是指 /usr/share/nginx/html这个路径。 
    index 指当没有指定主页时,默认的指定文件。

    开启7.221上面的tomcat之后通过访问192.168.7.201就能访问到http://192.168.7.221:8080;

    当然请求转发还有一些高级的用法在这了我就不介绍了,可以参考第四章常见问题2.

    1. 负载均衡

       

      负载均衡功能故名思议将请求按照不同的权重来转发,效果如下:

      详细配置

    #

    # The default server

    #

    upstream dis{

    server 192.168.7.40:8080;

    server 192.168.7.221:8080;

    }

     

    server {

    listen 80 default_server;

    listen [::]:80 default_server;

    server_name _;

    root /usr/share/nginx/html;

     

    # Load configuration files for the default server block.

    include /etc/nginx/default.d/*.conf;

     

    location / {

    proxy_pass http://dis;

    }

     

    error_page 404 /404.html;

    location = /40x.html {

    }

     

    error_page 500 502 503 504 /50x.html;

    location = /50x.html {

    }

    }

    开启7.221、7.40上面的tomcat之后通过访问192.168.7.201就能相互访问到http://192.168.7.221:8080; http://192.168.7.40:8080;

    四、常见问题

    1.nginx配置完毕之后添加请求分发功能,但是访问nginx对应的机器之后失效并未出现请求转发

    查看nginx错误日志:tail -n 100 /var/log/nginx/error.log

    2017/02/09 17:24:43 [crit] 11025#0: *1 connect() to 192.168.7.221:8080 failed (13: Permission denied) while connecting to upstream,

    client: 192.168.7.40, server: _, request: "GET / HTTP/1.1", upstream: "http://192.168.7.221:8080/", host: "192.168.7.221"

    问题原因:

    已确认本地防火墙已关闭,后经过一番查询之后,是因为Selinux

     SeLinux是2.6版本的Linux系统内核中提供的强制访问控制(MAC)系统。算是内置的安全系统,防火墙什么的应该算是外配的。

    So:解决方法

    1.关闭SeLinux

    1.临时关闭(不用重启机器):

    setenforce 0                  ##设置SELinux 成为permissive模式

    ##setenforce 1 设置SELinux 成为enforcing模式

    2.修改配置文件需要重启机器:

    修改/etc/selinux/config 文件

    将SELINUX=enforcing改为SELINUX=disabled

    重启机器即可

    3.执行下面的命令

    setsebool -P httpd_can_Network_connect 1

    2.参考博客文章

    http://blog.csdn.net/kooalle_cln/article/details/49591493

    3.nginx的其他功能包括fair策略、session共享、动静分离在这也不过多的讲,可以参考

    http://www.cnblogs.com/jalja/p/6118782.html

    4. tomcat中的appBase 和docBase的区别

    http://blog.csdn.net/liuxuejin/article/details/9104055

     

     

  • 相关阅读:
    导入导出
    封装本地文件路径
    读书书单
    Spring源码阅读-BeanFactory体系结构分析
    Spring源码阅读-ApplicationContext体系结构分析
    Spring源码阅读-IoC容器解析
    Spring源码阅读环境搭建
    【spring实战第五版遇到的坑】第14章spring.cloud.config.uri和token配置项无效
    【spring实战第五版遇到的坑】4.2.3中LDAP内嵌服务器不启动的问题
    【spring实战第五版遇到的坑】3.2中配置关系映射时,表名和3.1中不一样
  • 原文地址:https://www.cnblogs.com/joqk/p/6383536.html
Copyright © 2011-2022 走看看