码迷,mamicode.com
首页 > 其他好文 > 详细

tomcat-nginx动静分离

时间:2018-02-20 12:55:33      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:ip add   host   home   contex   tor   agent   环境   too   vim   

nginx安装:

安装依赖:

yum -y install make zlib zlib-devel gcc gcc-c++ libtool openssl openssl-devel

下载nginx

wget http://nginx.org/download/nginx-1.10.3.tar.gz

tar -xzvf nginx-1.10.3.tar.gz

cd nginx-1.10.3/

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

make && make install

nginx配置

vim /usr/local/nginx/conf/nginx.conf

server {

listen       80;

server_name  localhost;

 

location / {

root   html;

index  index.html index.htm index.jsp;

}

 

location ~ \.jsp$ {

proxy_pass http://127.0.0.1:8080;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#使后端tomcat获取用户真实IP

}

 java环境配置见http://liang-yao.cnblogs.com/p/8448739.html

tomcat安装

wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.0.M22/bin/apache-tomcat-9.0.0.M22.tar.gz

tar xzvf apache-tomcat-9.0.0.M22.tar.gz

mv apache-tomcat-9.0.0.M22/ /usr/local/tomcat

cd /usr/local/tomcat/bin/

tar xzvf commons-daemon-native.tar.gz

cd commons-daemon-1.0.15-native-src/unix/

./configure --with-java=/usr/local/java/jdk       #java安装路径

make

cp jsvc /usr/local/tomcat/bin/

 

修改Java安装路径和运行tomcat运行的用户

vim /usr/local/tomcat/bin/daemon.sh

89 test ".$TOMCAT_USER" = . && TOMCAT_USER=tomcat

90 #java安装路径

91  JAVA_HOME=/usr/local/java/jdk

 

创建用户tomcat

useradd -s /sbin/nologin tomcat

更改tomcat目录所有者和所属组

chown -R tomcat:tomcat /usr/local/tomcat/

 

启动tomcat

/usr/local/tomcat/bin/daemon.sh start

ss -ntlu

tcp LISTEN 0 100 :::8080 :::*

技术分享图片

启动nginx

/usr/local/nginx/sbin/nginx

ss -ntlu | grep 80

技术分享图片


动静分离

更改tomcat网站根目录

vim /usr/local/tomcat/conf/server.xml

<Host name="localhost"  appBase="webapps"

unpackWARs="true" autoDeploy="true">

<Context path="" docBase="/usr/local/nginx/html/" debug="0"  reloadable="true"/>

#使后端tomcat日志获取用户真实IP

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

prefix="localhost_access_log." suffix=".txt"

pattern="%{X-FORWARDED-FOR}i %l %u %t %r %s %b %D %q %{User-Agent}i %T" resolveHosts="false"/>

#使后端tomcat web和数据库获取用户真实IP

<Valve className="org.apache.catalina.valves.RemoteIpValve"

internalProxies="127\.0\.0\.1"

remoteIpHeader="x-forwarded-for"

remoteIpProxiesHeader="x-forwarded-by"

trustedProxies="127\.0\.0\.1"/>

 

删除末尾原有的日志设置:

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

prefix="localhost_access_log" suffix=".txt"

pattern="%h %l %u %t &quot;%r&quot; %s %b" />

 

 

 

添加a.jsp测试tomcat

vim /usr/local/nginx/html/a.jsp

<html>

<head><title>Hello World</title></head>

<body>

Hello World!<br/>

<%

out.println("Your IP address is " + request.getRemoteAddr());

%>

</body>

</html>

 

 

重新启动nginx

/usr/local/nginx/sbin/nginx -s reload

重新启动tomcat

/usr/local/tomcat/bin/daemon.sh stop

/usr/local/tomcat/bin/daemon.sh start

技术分享图片

技术分享图片

 

tomcat-nginx动静分离

标签:ip add   host   home   contex   tor   agent   环境   too   vim   

原文地址:https://www.cnblogs.com/liang-yao/p/8455014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!