当客户端访问网站时,静态页面nginx自己处理,动态页面转交给Tomcat处理
二台虚拟机,一台配置nginx,一台配置Tomcat
[root@nginx ~]# yum install pcre-devel zlib-devel gcc gcc-c++ make -y
[root@nginx ~]# useradd -M -s /sbin/nologin nginx
[root@nginx ~]# cd /opt
[root@nginx opt]# ls
nginx-1.12.2.tar.gz rh
[root@nginx opt]# tar zxvf nginx-1.12.2
[root@nginx opt]# cd nginx-1.12.2/
[root@nginx nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@nginx nginx-1.12.2]# make && make install
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
[root@nginx nginx-1.12.2]# nginx
[root@tomcat ~]# cd /opt
[root@tomcat opt]# ls
apache-tomcat-9.0.16.tar.gz jdk-8u91-linux-x64.tar.gz rh
[root@tomcat opt]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
[root@tomcat opt]# vim /etc/profile
//在末行加入
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib # .代表当前位置/usr/local/
export PATH=${JAVA_HOME}/bin:$PATH
[root@tomcat opt]# source /etc/profile //使环境变量生效
[root@tomcat opt]# tar apache-tomcat-9.0.16.tar.gz -C /usr/local/
[root@tomcat opt]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
[root@tomcat opt]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/
[root@tomcat opt]# startup.sh
客户端的静态文件请求nginx自己处理,动态文件请求转发给Tomcat处理
[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
......
server {
...省略...
location ~.*.jsp$ {
proxy_pass http://192.168.209.146:8080; ##192.168.209.146 Tomcat服务器的地址
proxy_set_header Host $host;
}
[root@nginx nginx-1.12.2]# vim /usr/local/nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>静态页面</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family:Tahoma,Verdana,Arial,sans-serif;
}
</style>
</head>
<body>
<h1>静态页面</h1>
<p>这是个静态页面</p>
</body>
</html>
[root@nginx nginx-1.12.2]# killall -s QUIT nginx
[root@nginx nginx-1.12.2]# nginx
[root@tomcat opt]# mkdir /usr/local/tomcat/webapps/test
[root@tomcat opt]# vim /usr/local/tomcat/webapps/test/index.jsp
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/ html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>动态页面</title>
</head>
<body>
<div>动态页面</div>
</body>
</html>
//访问动态
http://192.168.209.145/test/index.jsp
[root@tomcat conf]# cd /usr/local/tomcat/webapps/
[root@tomcat webapps]# ls
docs examples host-manager manager ROOT test
[root@tomcat webapps]# cd test/
[root@tomcat test]# ls
index.jsp
[root@tomcat test]# vim index.jsp
[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
//添加以下字段
location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root html;
expires 30d;
}
[root@nginx html]# mkdir /usr/local/nginx/html/test
[root@nginx html]# cd /usr/local/nginx/html/test/
[root@nginx test]# rz -E
rz waiting to receive.
[root@nginx test]# ls
tomcat.jpg