Getting to know Nginx

Nginx generation

  Not heard Nginx? So it must have heard of "peer" Apache now! Nginx is a kind of the same with Apache WEB server. Based on the REST architectural style, uniform resource descriptors (Uniform Resources Identifier) ​​URI, or Uniform Resource Locator (Uniform Resources Locator) URL as a communication basis, providing a variety of network services through the HTTP protocol.

  However, these servers are confined environment was designed from the beginning, for example, when the user scale, network bandwidth, and other product characteristics and limitations of their positioning and development are different. It also makes various WEB servers have their own distinctive features.

  Apache development period is very long and is the undisputed world's largest server. It has many advantages: a stable, open source, cross-platform, and so on. Time it appears too long, it's the rise of the Internet industry, far less than now. It is designed to be a heavyweight. It does not support high-concurrency server. Apache running on tens of thousands of concurrent access, will cause the server to consume a lot of memory. Its operating system to switch between processes or threads also consumes a lot of CPU resources, resulting in lower average response rate of HTTP requests.

  All decisions of Apache WEB server can not become a high-performance, lightweight high-concurrency server Nginx came into being.

  Russian engineer Igor Sysoev, his work for the duration of Rambler Media, using the C language developed Nginx. Nginx as a WEB server has been providing excellent and stable service for Rambler Media.

  And then, Igor Sysoev Nginx will open source code, and give free software license.

due to:

  • Nginx based event-driven architecture, so that it can support millions-level TCP connection
  • Highly modular and free software licenses enable a third party module after another (which is an open source era ah ~)
  • Nginx is a cross-platform server, the operating system can run on Linux, Windows, FreeBSD, Solaris, AIX, Mac OS, etc.
  • These bring good design is great stability

So, Nginx fire!

Nginx arena

  Nginx is a free, open-source, high-performance HTTP server and reverse proxy server; also a IMAP, POP3, SMTP proxy server; Nginx can publish treatment site as an HTTP server, in addition Nginx as reverse proxy for load balancing.

About Acting

  Speaking of agents, we need a clear concept of the so-called proxy is a representative of a channel;

  At this process involving the two roles, one is acting role, a role is the goal, is the role of the agent to complete some tasks through this proxy access to the target role of a process called proxy operation; as in life - guests to store adidas store to buy a pair of shoes, this store is the agent, the agent role is adidas manufacturers, the goal is the role of the user.

Forward Proxy

  Said before the reverse proxy, we look forward proxy, forward proxy to the proxy model is the most commonly contacted, we will deal with in two ways forward proxy mode on, respectively, from the software and life aspects to explain what is meant by forward proxy.

  In today's network environment, if we need to go because the technology to access some sites abroad, then you will find us located abroad through a website is no way to access the browser, in which case we may all operations with a FQ access, FQ is the main way to find a proxy server to access foreign sites, we will send the request to the proxy server, proxy server to access foreign websites, and then access the data passed to us!

  Above such proxy mode called forward proxy, forward proxy server address the biggest feature is very clear to the client access ; server only clear from the request which proxy server, but do not know which particular client from; forward proxy mode shield or hide the true client information . Look at a schematic diagram (I put the client in a box and forward proxy, belong to the same environment, I have introduced later):

 The client must be set up forward proxy server , of course, the premise is to know the IP address of the forward proxy server, as well as port agent. As shown in FIG.

  总结来说:正向代理,"它代理的是客户端,代客户端发出请求",是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端必须要进行一些特别的设置才能使用正向代理。

正向代理的用途:
(1)访问原来无法访问的资源,如Google
(2) 可以做缓存,加速访问资源
(3)对客户端访问授权,上网进行认证
(4)代理可以记录用户访问记录(上网行为管理),对外隐藏用户信息

反向代理

  明白了什么是正向代理,我们继续看关于反向代理的处理方式,举例如我大天朝的某宝网站,每天同时连接到网站的访问人数已经爆表,单个服务器远远不能满足人民日益增长的购买欲望了,此时就出现了一个大家耳熟能详的名词:分布式部署;也就是通过部署多台服务器来解决访问人数限制的问题;某宝网站中大部分功能也是直接使用Nginx进行反向代理实现的,并且通过封装Nginx和其他的组件之后起了个高大上的名字:Tengine,有兴趣的童鞋可以访问Tengine的官网查看具体的信息:http://tengine.taobao.org/。那么反向代理具体是通过什么样的方式实现的分布式的集群操作呢,我们先看一个示意图(我把服务器和反向代理框在一块,同属于一个环境,后面我有介绍):

 

  通过上述的图解大家就可以看清楚了,多个客户端给服务器发送的请求,Nginx服务器接收到之后,按照一定的规则分发给了后端的业务处理服务器进行处理了。此时~请求的来源也就是客户端是明确的,但是请求具体由哪台服务器处理的并不明确了,Nginx扮演的就是一个反向代理角色。  

  客户端是无感知代理的存在的,反向代理对外都是透明的,访问者并不知道自己访问的是一个代理。因为客户端不需要任何配置就可以访问。

  反向代理,"它代理的是服务端,代服务端接收请求",主要用于服务器集群分布式部署的情况下,反向代理隐藏了服务器的信息

反向代理的作用:
 (1)保证内网的安全,通常将反向代理作为公网访问地址,Web服务器是内网
 (2)负载均衡,通过反向代理服务器来优化网站的负载

项目场景

  通常情况下,我们在实际项目操作时,正向代理和反向代理很有可能会存在在一个应用场景中,正向代理代理客户端的请求去访问目标服务器,目标服务器是一个反向单利服务器,反向代理了多台真实的业务处理服务器。具体的拓扑图如下:

二者区别

截了一张图来说明正向代理和反向代理二者之间的区别,如图。

 

图解:

  在正向代理中,Proxy和Client同属于一个LAN(图中方框内),隐藏了客户端信息;

  在反向代理中,Proxy和Server同属于一个LAN(图中方框内),隐藏了服务端信息;

  实际上,Proxy在两种代理中做的事情都是替服务器代为收发请求和响应,不过从结构上看正好左右互换了一下,所以把后出现的那种代理方式称为反向代理了。

负载均衡

  我们已经明确了所谓代理服务器的概念,那么接下来,Nginx扮演了反向代理服务器的角色,它是以依据什么样的规则进行请求分发的呢?不用的项目应用场景,分发的规则是否可以控制呢?

  这里提到的客户端发送的、Nginx反向代理服务器接收到的请求数量,就是我们说的负载量。

  请求数量按照一定的规则进行分发到不同的服务器处理的规则,就是一种均衡规则。

  所以~将服务器接收到的请求按照规则分发的过程,称为负载均衡。

  负载均衡在实际项目操作过程中,有硬件负载均衡和软件负载均衡两种,硬件负载均衡也称为硬负载,如F5负载均衡,相对造价昂贵成本较高,但是数据的稳定性安全性等等有非常好的保障,如中国移动中国联通这样的公司才会选择硬负载进行操作;更多的公司考虑到成本原因,会选择使用软件负载均衡,软件负载均衡是利用现有的技术结合主机硬件实现的一种消息队列分发机制。

 

Nginx支持的负载均衡调度算法方式如下:

  1. weight轮询(默认,常用):接收到的请求按照权重分配到不同的后端服务器,即使在使用过程中,某一台后端服务器宕机,Nginx会自动将该服务器剔除出队列,请求受理情况不会受到任何影响。 这种方式下,可以给不同的后端服务器设置一个权重值(weight),用于调整不同的服务器上请求的分配率;权重数据越大,被分配到请求的几率越大;该权重值,主要是针对实际工作环境中不同的后端服务器硬件配置进行调整的。
  2. ip_hash(常用):每个请求按照发起客户端的ip的hash结果进行匹配,这样的算法下一个固定ip地址的客户端总会访问到同一个后端服务器,这也在一定程度上解决了集群部署环境下session共享的问题。
  3. fair:智能调整调度算法,动态的根据后端服务器的请求处理到响应的时间进行均衡分配,响应时间短处理效率高的服务器分配到请求的概率高,响应时间长处理效率低的服务器分配到的请求少;结合了前两者的优点的一种调度算法。但是需要注意的是Nginx默认不支持fair算法,如果要使用这种调度算法,请安装upstream_fair模块。
  4. url_hash:按照访问的url的hash结果分配请求,每个请求的url会指向后端固定的某个服务器,可以在Nginx作为静态服务器的情况下提高缓存效率。同样要注意Nginx默认不支持这种调度算法,要使用的话需要安装Nginx的hash软件包。

 几种常用web服务器对比

 

 

出处:https://www.cnblogs.com/wcwnina/p/8728391.html

 

Guess you like

Origin www.cnblogs.com/myseries/p/11270634.html