nginx+tomcat部署集群

拓扑环境:

服务器名称 系统版本 预装软件 ip地址
Nginx服务器 centos-6.5 nginx软件 192.168.xx.xxx
tomcat服务器 centos-6.5 tomcat     192.168.xx.xxx
tomcat服务器1 centos-6.5 tomcat 192.168.18.xxx

安装nginx

    1.解压nginx:tar -zxvf nginx

    2.编译nginx

        a.进入到nginx源码目录:cd /home/environment/nginx

        b.执行./configure --prefix=/home/environment/其他的名字(编译后存放的路径)

        c.make & make install

    3.启动nginx

        a.进入到编译后的目录:cd /home/environment/其他名字

        b.进入到启动目录:cd sbin

扫描二维码关注公众号,回复: 2535971 查看本文章

        c.启动nginx:./nginx

    4.测试nginx是否启动成功,通过浏览器查看192.168.xx.xxx:80,出现欢迎来到nginx则启动成功,反之失败


在另外两台服务器部署tomcat,在webapps中手动创建一个testNginx目录加入login.html,并启动,过程略过

修改ngix配置文件,用来做负载均衡

    a.进入到编译后的目录:cd /home/environment/其他名字

    b.进入到conf目录,编辑nginx.conf文件:vi conf/nginx.conf

    c.修改后文件如下:

#user  nobody;
worker_processes  2;

#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 mycluster{
         #集群有几台服务器即可配置几台,weight表示权重,权重越大被访问到的几率越大
        #这里添加的是上面启动好的两台Tomcat服务器
         server 192.168.xx.xxx:8080 weight=1;
         server 192.168.xx.xxxx:8080 weight=1;
    }


    server {
        listen       8888;//监听的端口
        server_name  192.168.xx.xxx;//nginx装的服务器ip

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #将访问请求转向至服务器集群,mycluster和上面upstream mycluster 对应
            proxy_pass http://mycluster;
            # 真实的客户端IP
            proxy_set_header   X-Real-IP        $remote_addr;
            # 请求头中Host信息
            proxy_set_header   Host             $host;
            # 代理路由信息,此处取IP有安全隐患
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            # 真实的用户访问协议
            proxy_set_header   X-Forwarded-Proto $scheme;
        }

        #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;
    #    }
    #}

}

编译nginx可能会出现的错误

在进行./configure 命令的时候经常会因为缺少库文件而报错,如下:

错误一:缺少gc++库文件

解决方式:在线安装gcc gcc-++

yum -y install gcc  gcc-++ autoconf automake
  • 1


错误二:缺少PCRE库 
./configure: error: the HTTP rewrite module requires the PCRE library.

解决方式:安装pcre-devel解决问题

yum -y install pcre-devel
  • 1

错误三: 
错误提示:./configure: error: the HTTP cache module requires md5 functions 
from OpenSSL library. You can either disable the module by using 
–without-http-cache option, or install the OpenSSL library into the system, 
or build the OpenSSL library statically from the source with nginx by using 
–with-http_ssl_module –with-openssl= options.

解决方式:

yum  -y install openssl openssl-devel
  • 1

安装后继续执行configure命令,即可完成Nginx的安装

./configure --prefix=/usr/local/nginx
  • 1




猜你喜欢

转载自blog.csdn.net/u013164612/article/details/80402210