什么是反向代理
反向代理服务器决定哪台服务器提供服务。返回代理服务器不提供服务器。只是请求的转发。
正向代理如下
反向代理如下
Nginx实现反向代理的过程
首先安装两个tomcat服务器,都放到nginx服务器里面,两个端口分别是8081和8082
首先是下载tomcat,使用下面的命令
wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.94/bin/apache-tomcat-7.0.94.tar.gz
然后是加载这个压缩包
tar -xvf apache-tomcat-7.0.94.tar.gz
然后把apache-tomcat-7.0.94复制成两份,一份是tomcat8081,一份是tomcat8082,过程如下
cp -r apache-tomcat-7.0.94 tomcat8081
cp -r apache-tomcat-7.0.94 tomcat8082
然后通过Editplus远程连接修改tomcat8081里面的server.xml配置里面修改端口号
<Server port="8006" shutdown="SHUTDOWN">
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
然后通过Editplus远程连接修改tomcat8082里面的server.xml配置里面修改端口号
<Server port="8007" shutdown="SHUTDOWN">
<Connector port="8082" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />
然后把tomact8081里面的webapps文件夹里面的ROOT文件夹里面的index.jsp变成下面这样,此时可以看到下面输入的内容是8081
<!DOCTYPE html>
<%@ page session="false" %>
<%
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
request.setAttribute("year", sdf.format(new java.util.Date()));
request.setAttribute("tomcat7Url", "http://tomcat.apache.org/");
request.setAttribute("tomcat7DocUrl", "/docs/");
request.setAttribute("tomcat7ExamplesUrl", "/examples/");
%>
<html lang="en">
<head>
<title><%=request.getServletContext().getServerInfo() %></title>
<link href="favicon.ico" rel="icon" type="image/x-icon" />
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="tomcat.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>tomcat8081index.jsp<h1>
</body>
</html>
然后把tomact8082里面的webapps文件夹里面的ROOT文件夹里面的index.jsp变成下面这样,此时可以看到下面输入的内容是8082
<!DOCTYPE html>
<%@ page session="false" %>
<%
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
request.setAttribute("year", sdf.format(new java.util.Date()));
request.setAttribute("tomcat7Url", "http://tomcat.apache.org/");
request.setAttribute("tomcat7DocUrl", "/docs/");
request.setAttribute("tomcat7ExamplesUrl", "/examples/");
%>
<html lang="en">
<head>
<title><%=request.getServletContext().getServerInfo() %></title>
<link href="favicon.ico" rel="icon" type="image/x-icon" />
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="tomcat.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>tomcat8082index.jsp<h1>
</body>
</html>
然后就是启动tomcat8081和tomcat8082,启动如下所示
/root/tomcat8081/bin/startup.sh
/root/tomcat8082/bin/startup.sh
然后访问http://47.91.248.236:8081/ 路径结果如下,成功了,然后访问http://47.91.248.236:8082/ 路径结果如下,成功了
然后我们配置本地电脑里面的host文件变成下面这样
默认在一下目录(windows10)
C:\Windows\System32\drivers\etc
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 activate.navicat.com
192.168.199.131 www.sina.com
192.168.199.131 www.guojun.com
192.168.199.131 www.shaonian.com
Linux在一下目录,类似上面进行添加
/etc/hosts
然后配置nginx服务器里面的conf文件夹里面的nginx.conf配置文件,配置完之后记得要重启nginx服务器
此时当访问www.sina.com 的时候,就会访问host文件,然后就会去找47.91.248.236 这个ip对应的linux服务器,然后www.sina.com 默认的端口就是80,所以访问www.sina.com 的时候,就会找到下面的upstream tomcat1,然后下面的upstream tomcat1就会去找server 47.91.248.236:8081,就会找到8081端口的tomcat服务器,然后因为upstream tomcat1的默认访问页是index.jsp,所以就会访问8081端口的tomcat服务器的index.jsp页面(也就是http://47.91.248.236:8081/index.jsp)
此时当访问www.huohu.com 的时候,就会访问host文件,然后就会去找47.91.248.236 这个ip对应的linux服务器,然后www.huohu.com 默认的端口就是80,所以访问www.huohu.com 的时候,就会找到下面的upstream tomcat2,然后下面的upstream tomcat2就会去找server 47.91.248.236:8082,就会找到8082端口的tomcat服务器,然后因为upstream tomcat2的默认访问页是index.jsp,所以就会访问8082端口的tomcat服务器的index.jsp页面(也就是http://47.91.248.236:8082/index.jsp)
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#配置www.sina.com:80对应的服务器监听端口
upstream tomcat1 {
server 192.168.199.131:8081;
}
server {
listen 80;
server_name www.sina.com;
location / {
proxy_pass http://tomcat1;
#配置默认访问页,这里就会访问到tomcat1里面的那个index.jsp文件里面
index index.jsp;
}
}
#配置网站服务器
server {
listen 80;
server_name www.shaonian.com;
location / {
root /data/app/ruoyi-ui;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /prod-api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#配置www.houhu.com:80对应的服务器监听端口
upstream tomcat2 {
server 192.168.199.131:8082;
}
server {
listen 80;
server_name www.guojun.com;
location / {
proxy_pass http://tomcat2;
#配置默认访问页,这里就会访问到tomcat2里面的那个index.jsp文件里面
index index.jsp;
}
}
}
然后我们访问 www.sina.com
此时访问到的就是tomcat8081对应的tomcat服务器
然后我们访问 www.guojun.com
此时访问到的就是tomcat8082对应的tomcat服务器
到此nginx的反向代理就完成了,感谢大家的观看!