Nginx配置反向代理的主要参数
upstream 服务池名 { }
服务端接收来自客户端的请求中,既有静态资源也有动态资源,静态资源由Nginx提供服务,动态资源Nginx转发至后端
分析
要求部署两台后端Tomcat服务器
为了进行负载均衡测试,搭建两个内容不同的网站
Tomcat部署与网站搭建步骤
在Nginx服务器上安装Nginx,反向代理两个Tomcat站点,并实现负载均衡
实验环境:
一台:Nginx 作为反向代理动静分离的静态页面 192.168.200.60
一台:tomcat 作为动态页面处理的 192.168.200.80
实验目的:
nginx 处理静态页面
tomcat01 处理动态界面
实验步骤:
1、配合nginx服务
[root@localhost ~]# hostnamectl set-hostname nginx
[root@localhost ~]# su
[root@nginx ~]# tar zxvf nginx-1.12.0.tar.gz -C /opt/
[root@nginx ~]# cd /opt/nginx-1.12.0/
[root@nginx nginx-1.12.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@nginx nginx-1.12.0]# iptables -F
[root@nginx nginx-1.12.0]# setenforce 0
[root@nginx nginx-1.12.0]# yum -y install pcre-devel zlib-devel gcc-c++
[root@nginx nginx-1.12.0]# useradd -M -s /sbin/nologin nginx
[root@nginx nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@nginx nginx-1.12.0]# make && make install
[root@nginx nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx nginx-1.12.0]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx nginx-1.12.0]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig:- 99 20
#description:Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage:$0{start|stop|restart|reload}"
exit 1
esac
exit 0
[root@nginx nginx-1.12.0]# cd /etc/init.d/
[root@localhost init.d]# chmod +x nginx //添加权限
[root@localhost init.d]# chkconfig --add nginx //添加到systemctl启动文件设置中
[root@nginx init.d]# netstat -antp | grep nginx
[root@nginx init.d]# service nginx start
[root@nginx init.d]# netstat -antp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 15902/nginx: master
2、配置tomcat01服务器
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 视频 文档 音乐
apache-tomcat-9.0.16.tar.gz jdk-8u201-linux-x64.rpm 模板 图片 下载 桌面
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# hostnamectl set-hostname tomcat01
[root@localhost ~]# su
[root@tomcat01 ~]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/ //解压Java
[root@tomcat01 ~]# 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
export PATH=${JAVA_HOME}/bin:$PATH
[root@tomcat01 ~]# source /etc/profile
[root@tomcat01 ~]# tar zxvf apache-tomcat-9.0.16.tar.gz -C /usr/local/
[root@tomcat01 ~]# cd /usr/local/
[root@tomcat01 local]# ls
apache-tomcat-9.0.16 bin etc games include jdk1.8.0_91 lib lib64 libexec sbin share src
[root@tomcat01 local]# mv apache-tomcat-9.0.16/ tomcat
[root@tomcat01 local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
[root@tomcat01 local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/
[root@tomcat01 local]# netstat -antp | grep 8080
[root@tomcat01 local]# startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8.0_91/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat01 local]# netstat -antp | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 13215/java
3、配置动静分离
#########配置nginx服务器##########
[root@nginx init.d]# vim /usr/local/nginx/conf/nginx.conf
44 location ~.*.jsp$ {
45 proxy_pass http://192.168.200.80:8080;
46 proxy_set_header Host $host;
47 }
[root@nginx init.d]# cd /usr/local/nginx/html/
[root@nginx html]# ls
50x.html index.html
[root@nginx html]# vim index
<!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 html]# service nginx start
##########配置tomcat01服务器##############
[root@tomcat01 local]# cd /usr/local/tomcat/webapps/
[root@tomcat01 webapps]# mkdir test
[root@tomcat01 webapps]# cd test/
[root@tomcat01 test]# vim 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/ html14/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>动态页面</title>
</head>
<body>
<div>动态页面</div>
</body>
</html>
######配置nginx服务器##############
图片属于静态资源,因为Nginx处理的是静态页面。所以图片应该放在Nginx目录中,但是要与Tomcat的JSP文件的路径相对应
[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
47 location ~.*\.(gif|jpg|jpeg|png|bmg|swf|css)$ {
48 root html;
49 expires 30d;
50 }
[root@nginx html]# mkdir test
[root@nginx html]# ls
50x.html index index.html test
[root@nginx test]# ls
game.jpg //插入的图片
#########配置tomcat01服务器##################
[root@tomcat01 test]# vim index.jsp //完整路径 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/ html14/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>动态页面</title>
</head>
<body>
<div>动态页面</div>
<img src="22.jpg"width=250> //添加
</body>
</html>
使用win10 访问192.168.200.60 访问静态页面 192.168.200.60/test/index.jsp访问动态页面
由于图片是静态的,所以图片存在nginx服务器中,客户端访问动态页面走nginx处理静态图片
实验环境:
Nginx 192.168.200.60 作为负载均衡服务器
Tomcat1 192.168.200.80 作为后端web服务器1
Tomcat 2 192.168.200.90 作为后端web服务器2
实验目的:
以Nginx作为负载均衡器,Tomcat作为web服务器,验证nginx反向代理
实验步骤:
1、配置tomcat02服务器
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# hostnamectl set-hostname tomcat02
[root@localhost ~]# su
[root@tomcat02 ~]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
[root@tomcat02 ~]# source /etc/profile
[root@tomcat02 ~]# tar zxvf apache-tomcat-9.0.16.tar.gz -C /usr/local/
[root@tomcat02 ~]# cd /usr/local/
[root@tomcat02 local]# ls
apache-tomcat-9.0.16 etc include lib libexec share
bin games jdk1.8.0_91 lib64 sbin src
[root@tomcat02 local]# mv apache-tomcat-9.0.16/ tomcat
[root@tomcat02 local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
[root@tomcat02 local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/
[root@tomcat02 local]# netstat -antp | grep 8080
[root@tomcat02 local]# startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8.0_91/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat02 local]# netstat -antp | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 69143/java
[root@tomcat02 local]# mkdir -pv /web/webapp1
mkdir: 已创建目录 "/web"
mkdir: 已创建目录 "/web/webapp1"
[root@tomcat02 local]# cd /web/webapp1
[root@tomcat02 webapp1]# vim 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/ html14/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP test1 page</title>
</head>
<body>
<div>welcome test1 web</div>
</body>
</html>
[root@tomcat02 webapp1]# vim /usr/local/tomcat/conf/server.xml
151 <Context docBase="/web/webapp1" path="" reloadable="false"> 插入
152 </Context>
[root@tomcat02 webapp1]# shutdown.sh
[root@tomcat02 webapp1]# startup.sh
3、配置tomcat01网页,并重启服务
[root@tomcat01 webapps]# mkdir -p /web/webapp1
[root@tomcat01 webapps]# cd /web/webapp1
[root@tomcat01 webapp1]# vim 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/ html14/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>web web web web web webb !!!!</title>
</head>
<body>
<div>web web web web web webb !!!!</div>
</body>
</html>
[root@tomcat01 webapp1]# vim /usr/local/tomcat/conf/server.xml
151 <Context docBase="/web/webapp1" path="" reloadable="false"> 插入
152 </Context>
[root@tomcat01 webapp1]# shutdown.sh
[root@tomcat01 webapp1]# startup.sh
4、nginx做反向代理
root@nginx test]# vim /usr/local/nginx/conf/nginx.conf
在http的全局添加
34 upstream tomcatserver { //名字里面不能加下划线,nginx中upstream后面的名称不能使用下滑线,Nginx不能识别会报400错误。
35 server 192.168.200.80:8080 weight=1;
36 server 192.168.200.90:8080 weight=2;
37 }
在server中添加
56 location / {
57 root html;
58 index index.html index.htm;
59 proxy_pass http://tomcatserver;
60 }
[root@nginx test]# service nginx stop
[root@nginx test]# service nginx start
报错详情请查看:https://blog.csdn.net/m0_47161295/article/details/108445524