nginx+tomcat

  1. 云栖社区>
  2. 博客>
  3. 正文

nginx+tomcat

余二五 2017-11-06 14:23:00 浏览742
展开阅读全文
全部系统采用centos7.1 

各节点配置好hosts文件
cat /etc/hosts
172.16.92.1    node1.centos7.com    node1
172.16.92.2    node2.centos7.com    node2
172.16.92.3    node3.centos7.com    node3


node2, node3 --> tomcat ( jsp网站服务器 )
node1 --> nginx反向代理至tomcat


================ node2 172.16.92.2/16 ===================
安装JDK(光盘源)
yum -y install java-1.7.0-openjdk

安装二进制tomcat

wget http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.23/bin/apache-tomcat-8.0.23.tar.gz
tar xf apache-tomcat-8.0.23.tar.gz -C /usr/local
cd /usr/local
ln -sv apache-tomcat-8.0.23 tomcat
vim /etc/profile.d/tomcat.sh
(:set paste)
export CATALINA_HOME=/usr/local/tomcat
export PATH=$PATH:$CATALINA_HOME/bin
================
. /etc/profile.d/tomcat.sh
catalina.sh --help
catalina.sh version
catalina.sh start

windows浏览器访问 http://172.16.92.2:8080/ 能看到tomcat首页即可
linux 访问 curl -I  http://172.16.92.2:8080/ 看到 HTTP/1.1 200 OK 即可

catalina.sh stop
mkdir -pv /data/webapps/ROOT
cd tomcat/conf
vim server.xml
#添加虚拟主机
<Engine name="Catalina" defaultHost="node2.centos7.com">   #修改
:set nu
:123,139y  -> p -> 修改成如下
      <Host name="node2.centos7.com"  appBase="/data/webapps" autoDeploy="true">
        <Context path="" docBase="ROOT" />
          <Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"
               prefix="tomcat1_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
==============
mkdir /data/logs
catalina.sh configtest
>全部都是INFO开头的就正常了
(难道是把jar放在 /data/webapps/ROOT/lib 下?)
mkdir -pv /data/webapps/ROOT/{lib,classes,META-INF,WEB-INF}
vim /data/webapps/ROOT/index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>  
        <head>                  
                <title>JSP Test Page on web 1</title>
        </head> 
        <body>  
                <% out.println("JSP Test Page on tomcat 1"); %>
        </body> 
</html>
================
catalina.sh start
ss -tnal | grep 8080

windows 浏览器访问 http://172.16.92.2:8080/
出现 "JSP Test Page on tomcat 1"

linux 访问方式  curl  http://172.16.92.2:8080/
出现 html的网页代码


================ node3 172.16.92.3/16 ===================
安装JDK(光盘源)
yum -y install java-1.7.0-openjdk

安装tomcat
scp node2:/root/apache-tomcat-8.0.23.tar.gz ./
tar xf apache-tomcat-8.0.23.tar.gz -C /usr/local
cd /usr/local
ln -sv apache-tomcat-8.0.23 tomcat
vim /etc/profile.d/tomcat.sh
(:set paste)
export CATALINA_HOME=/usr/local/tomcat
export PATH=$PATH:$CATALINA_HOME/bin
================
. /etc/profile.d/tomcat.sh
catalina.sh --help
catalina.sh version
catalina.sh start

windows浏览器访问 http://172.16.92.3:8080/ 能看到tomcat首页即可
linux 访问 curl -I  http://172.16.92.3:8080/ 看到 HTTP/1.1 200 OK 即可

catalina.sh stop

cd /usr/local/tomcat/conf/
cp server.xml{,.bak}
scp node2:/usr/local/tomcat/conf/server.xml ./
vim server.xml
<Engine name="Catalina" defaultHost="node3.centos7.com">    #修改
<Host name="node3.centos7.com"  appBase="/data/webapps" autoDeploy="true">    #修改
prefix="tomcat2_access_log" suffix=".txt"        #修改
=================
scp -rp node2:/data /
vim /data/webapps/ROOT/index.jsp
<% out.println("JSP Test Page on tomcat 2"); %>    #修改
==========
catalina.sh configtest
>全部都是INFO开头的就是正常了
catalina.sh start

windows 浏览器访问 http://172.16.92.3:8080/
出现 "JSP Test Page on tomcat 2"

linux 访问方式  curl  http://172.16.92.3:8080/
出现 html的网页代码



================ node1 172.16.92.1/16 ===================
nginx做tomcat1,2的反向代理

安装epel源
wget -O /etc/yum.repos.d/epel7.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install nginx
cd /etc/nginx
cp nginx.conf{,.bak}
vim nginx.conf
:1,$d -- 删除全部内容
:set paste --> 再粘贴如下内容
################
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

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;
    include /etc/nginx/conf.d/*.conf;
    upstream tcsrvs {
        server node2.centos7.com:8080;
        server node3.centos7.com: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 / {
        }
        location ~* \.(jsp|do)$ {
                proxy_pass http://tcsrvs;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
==================end of nginx.conf
nginx -t
>出现ok和successful相关句子就没问题了
systemctl start nginx.service
ss -tanl | grep 80

windows浏览器访问 http://172.16.92.1/index.jsp 
>JSP Test Page on tomcat 2 
>JSP Test Page on tomcat 1 
在上面两个网页之间轮询


nginx实现会话绑定 (基于源地址哈希方式绑定)
vim /etc/nginx/nginx.conf
=============
......                #其余内容不变
    upstream tcsrvs {
        ip_hash;            #添加此句
        server node2.centos7.com:8080;
        server node3.centos7.com:8080;
    }
......                #其余内容不变
=============
nginx -t
systemctl reload nginx.service

http://172.16.92.1/index.jsp
>JSP Test Page on tomcat 1         #固定在此页面不再轮询







本文转自 zhuhc1988 51CTO博客,原文链接:http://blog.51cto.com/changeflyhigh/1707496,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论