tomcat-nginx动静分离

Posted liang-yao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tomcat-nginx动静分离相关的知识,希望对你有一定的参考价值。

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动静分离的主要内容,如果未能解决你的问题,请参考以下文章

中间件 | Nginx实现动静分离

Linux里面为啥nginx要做动静分离?

nginx动静分离工作原理

nginx动静分离+资源分离

Nginx 动静分离概述

Nginx 动静分离概述