【技术干货】Nginx配置反向代理
什么是反向代理
Nginx实现反向代理的过程
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
cp -r apache-tomcat-7.0.94 tomcat8081cp -r apache-tomcat-7.0.94 tomcat8082
<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" />
<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" />
<!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>
<%@ 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>
/root/tomcat8081/bin/startup.sh/root/tomcat8082/bin/startup.sh
此时当访问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 47.91.248.236:8081;}server {listen 80;server_name www.sina.com;location / {proxy_pass http://tomcat1;#配置默认访问页,这里就会访问到tomcat1里面的那个index.jsp文件里面index index.jsp;}}#配置www.houhu.com:80对应的服务器监听端口upstream tomcat2 {server 47.91.248.236:8082;}server {listen 80;server_name www.houhu.com;location / {proxy_pass http://tomcat2;#配置默认访问页,这里就会访问到tomcat2里面的那个index.jsp文件里面index index.jsp;}}}
然后我们访问 www.huohu.com。
内容来源于网络,侵删
鉴于篇幅有限,更多详细课程大纲/知识体系/实体书籍/网工必备工具包等请扫码加群获取!
领取福利
