nginx代理

一。概念

 

1、什么是代理服务器

代理服务器,客户机在发送请求时,不会直接发送给目的主机,而是先发送给代理服务器,代理服务接受客户机请求之后,再向主机发出,并接收目的主机返回的数据,存放在代理服务器的硬盘中,再发送给客户机。

2、为什么要使用代理服务器

1)提高访问速度

    由于目标主机返回的数据会存放在代理服务器的硬盘中,因此下一次客户再访问相同的站点数据时,会直接从代理服务器的硬盘中读取,起到了缓存的作用,尤其对于热门站点能明显提高请求速度。

2)防火墙作用

    由于所有的客户机请求都必须通过代理服务器访问远程站点,因此可在代理服务器上设限,过滤某些不安全信息。

3)通过代理服务器访问不能访问的目标站点

    互联网上有许多开发的代理服务器,客户机在访问受限时,可通过不受限的代理服务器访问目标站点,通俗说,我们使用的翻墙浏览器就是利用了代理服务器,虽然不能出国,但也可直接访问外网。

3、什么是正向代理?什么是反向代理?

正向代理,架设在客户机与目标主机之间,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中。

反向代理服务器架设在服务器端,通过缓冲经常被请求的页面来缓解服务器的工作量,将客户机请求转发给内部网络上的目标服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器与目标主机一起对外表现为一个服务器。

2、反向代理有哪些主要应用?

现在许多大型web网站都用到反向代理。除了可以防止外网对内网服务器的恶性攻击、缓存以减少服务器的压力和访问安全控制之外,还可以进行负载均衡,将用户请求分配给多个服务器。

 

二。反向代理

1.简单反向代理一个tomcat

启动一个tomcat

nginx的conf


 
 
  1. server {
  2. listen 80;
  3. server_name server8.test.com;
  4. location / {
  5. proxy_pass http://localhost:9000;
  6.             root html;
  7.     index index.html index.htm;
  8. }

listen 80,监听端口是80,访问localhost:80 代理了proxy_pass 的地址

server_name 是域名的意思,但需要在hosts文件中配置。

host文件 ip 域名,意思是,访问后面的域名会先查找本地host文件中的ip

(在微服务注册中心多节点,也是  多个 localhost peer1    localhost peer2,访问peer1 会访问到localhost)

现在访问http://server8.test.com/index.html  。 但必须是在listen是80 的情况下,因为访问域名默认端口是80

更改nginx配置后,在命令行 输入 nginx -s reload重载配置,如果你的配置有问题,他会提示哪写错了

2.多个server 代理多个服务器


 
 
  1. server {
  2. listen 90;
  3. server_name server8.test.com;
  4. location / {
  5.     proxy_pass http://localhost:9001;
  6.             root html;
  7.     #proxy_pass http://proxyserver_melo;
  8.     index index.html index.htm;
  9. }
  10. }
  11. server {
  12. listen 80;
  13. server_name server8.test.com;
  14. location / {
  15.     proxy_pass http://localhost:9000;
  16.     root html;
  17.     #proxy_pass http://proxyserver_melo;
  18.     index index.html index.htm;
  19. }
  20. }

3.域名、服务器IP、nginx访问流程。

1.最初 http://IP:端口 ,访问服务器上某网站,http默认80端口,如果项目本身是80端口,访问路径可以不写端口。

2.有了域名,域名解析到一台服务器IP(其实可以解析到多台服务器):  http://域名:端口。如果项目启动在80端口,则直接访问http://域名

3.有了nginx,nginx监听服务器上的一些端口比如80,将访问到80的请求转发到其他地址(配置:proxy_pass http://localhost:9000 也可以是其他服务器地址)。

为什么要转发?nginx上存放静态资源页面图片等,缓解压力;只暴露nginx所在服务器的地址(而且是域名,需要经过dns后才能找到ip,用域名安全),不暴露项目所在服务器ip和端口,防止攻击;

比如下面这个配置,会将server8.test.com这个域名80端口的请求转发到http://localhost:9001。因为这个域名不是真实的,所以在host文件配置了下,如果是真实的域名,不需要再host再写。

如果用户知道项目启动在了9001端口,其实通过http://server8.test.com:9001也能访问,之所以监听80转发,因为http默认80端口,用域名就是图方便,用户不用记端口ip。


 
 
  1. server {
  2. listen 80;
  3. server_name server8.test.com;
  4. location / {
  5.     proxy_pass http: //localhost: 9001;
  6.             root html;
  7.      #proxy_pass http://proxyserver_melo;
  8.      index index.html index.htm;
  9. }
  10. }

4.前后端分离。

比如vue本地开发,通过启动node本地访问,但放在服务器上会直接把 html页面和js 图片放在nginx上,或者php也经常放在nginx上访问。

而前端代码在生产环境中,经常也单独部署在一台服务器上,后端不同服务部署在各自的服务器上,这样单个服务器压力小,某个服务出问题,容易排查,立刻知道是哪个服务器的项目有问题,某个服务器宕机也不会影响别的服务。当然图省钱的话,就会把所有服务、前后端都放在一个项目里。

说回代理的问题,比如以下代码:把前端文件放在nginx服务器的/opt/vue目录下, 下面root配置指定了前端文件目录,因为文件放在了nginx不需要proxy_pass转发, 用户访问xxx.cn就访问到了前端文件。


 
 
  1. server {
  2. listen 80;
  3. server_name xxx.cn
  4. root /opt/vue/;
  5. location / {
  6. index index.html;
  7. try_files $uri $uri/ /index.html;
  8. }
  9. }

如果前端js里,访问后端接口地址写 yyy.cn ,所以后端代理要这么写,访问yyy.cn就会转发到 http://127.0.0.1:8080/的项目。

即使前后端代码都在一台服务器上, xxx.cn  yyy.cn都解析到了这台服务器,nginx对两个域名监听的端口都是80,这也互不影响。通过xxx.cn:80会访问前端代码,通过yyy.cnh:80会访问后端


 
 
  1. server {
  2. listen 80;
  3. server_name yyy.cn;
  4. client_max_body_size 100M;
  5. location / {
  6. proxy_pass http: //127.0.0.1:8080/;
  7. }
  8. }

 

 

4.代理多个服务器,负载均衡

 


 
 
  1. upstream proxyserver_melo{
  2. server localhost:9000;
  3. server localhost:9001;
  4. }
  5. server {
  6. listen 80;
  7. server_name server8.test.com;
  8. location / {
  9.     root html;
  10.     proxy_pass http://proxyserver_melo;
  11.     index index.html index.htm;
  12. }

 

1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 
upstream backserver { 
server 192.168.0.14; 
server 192.168.0.15; 


2、指定权重
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 
upstream backserver { 
server 192.168.0.14 weight=10; 
server 192.168.0.15 weight=10; 


3、IP绑定 ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 
upstream backserver { 
ip_hash; 
server 192.168.0.14:88; 
server 192.168.0.15:80; 

三.REDISt托管session

本文主要介绍如何使用redis对tomcat7的session进行托管。

1、安装Redis
redis安装比较简单,此处略过。

2、配置两个Tomcat
在本机上配置两个Tomcat,分别为tomcat7-8081、tomcat7-8082。

编制两个index.jsp页面,分别放入tomcat7-8081\webapps\ROOT、tomcat7-8082\webapps\ROOT目录下,index.jsp页面内容如下:

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
 
 
    pageEncoding="UTF-8"%>
 
 
<!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=ISO-8859-1">
 
 
<title>获取session id</title>
 
 
</head>
 
 
<body>
 
 
    Session Id : <%= request.getSession().getId() %>
 
 
</body>
 
 
</html>
 
 

 

3、拷贝tomcat需要的jar
将如下几个jar拷贝到${TOMCAT_HOME}/lib下

tomcat-redis-session-manager-VERSION.jar 
 
 
jedis-2.5.2.jar 
 
 
commons-pool2-2.2.jar
 
 

备注:jar已上传到博客园中,有需要的点击下载

4、配置tomcat 
编辑${TOMCAT_HOME}/conf/context.xml,在context中加入

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
 
 
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
 
 
       host="localhost"
 
 
       port="6379"
 
 
       database="0"
 
 
       maxInactiveInterval="60" />
 
 

其中host和port为redis的ip和端口

至此配置完成,tomcat会使用redis来托管session。

 

 

 

  •                     <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count">1</span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/u014203449">
                    <img src="https://profile.csdnimg.cn/D/C/8/3_u014203449" class="avatar_pic" username="u014203449">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/1x/6.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/u014203449" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">科氏加速度</a></span>
                                            </div>
                    <div class="text"><span>发布了88 篇原创文章</span> · <span>获赞 65</span> · <span>访问量 15万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://im.csdn.net/im/main.html?userName=u014203449" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                        </a>
                                                            <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">已关注</a>
                                    </div>
                            </div>
                    </div>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值