What Nginx that? Can you do? Deleted invasion

Source: cnblogs.com/wcwnina/p/8728391.html

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

  • High degree of modularity and software licenses are free of third-party modules 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 excellent design brings 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 point it is to design 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 is called the process agent 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.

In summary: forward proxy, "which is the agent of the client," it is a server located between the client and the origin server (origin server), in order to obtain content from the origin server, the client sends a request to the proxy and specify target (the original server), and then forwarded the request to the original proxy server and content to get back to the client. The client must make some special settings to use the forward proxy.

Forward proxy uses:

  • The original resource access inaccessible, such as Google

  • You can do caching, to accelerate access to resources

  • For client access licensing, Internet authentication

  • Proxy can record user access records (access management), external users to hide information

Reverse Proxy

Understand what is forward proxy, we continue to look at ways for dealing with the reverse proxy, as my example of a large heavenly treasure site, access per day while the number of visits to the site has burst table, a single server can not meet the people's growing desire to buy, and this time there have been a familiar term: distributed deployment;

That is, the number of access restrictions to solve the problem by deploying multiple servers; a treasure site most of the functions are also directly Nginx reverse proxy to achieve, and then packaged by Nginx and other components of a name on tall: Tengine.

Interested shoes can be accessed Tengine official website to view specific information: http: //tengine.taobao.org/. Then the reverse proxy specifically distributed cluster operations by what means to achieve it, we look at a diagram of (I put the server and reverse proxy in a box, belong to the same environment, I have introduced later):

Through the above we can see the illustration, a plurality of client requests to the server to send, after receiving the Nginx server, according to certain rules distributed to the rear end of the service processing server for processing. At this point - that is the request source client is clear, but the request was not clear which server a specific treatment, Nginx play is a reverse proxy role.

The client is not aware of the existence of the proxy, reverse proxy outside are transparent, accessible to visitors do not know they are a proxy . Because the client does not need any configuration can be accessed.

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

反向代理的作用:

  • 保证内网的安全,通常将反向代理作为公网访问地址,Web服务器是内网

  • 负载均衡,通过反向代理服务器来优化网站的负载

项目场景

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

具体的拓扑图如下:

二者区别

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

图解:

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

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

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

负载均衡

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

这里提到的客户端发送的、Nginx反向代理服务器接收到的请求数量,就是我们说的负载量。请求数量按照一定的规则进行分发到不同的服务器处理的规则,就是一种均衡规则。

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

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

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

weight轮询(默认):接收到的请求按照顺序逐一分配到不同的后端服务器,即使在使用过程中,某一台后端服务器宕机,Nginx会自动将该服务器剔除出队列,请求受理情况不会受到任何影响。这种方式下,可以给不同的后端服务器设置一个权重值(weight),用于调整不同的服务器上请求的分配率;权重数据越大,被分配到请求的几率越大;该权重值,主要是针对实际工作环境中不同的后端服务器硬件配置进行调整的。

ip_hash:每个请求按照发起客户端的ip的hash结果进行匹配,这样的算法下一个固定ip地址的客户端总会访问到同一个后端服务器,这也在一定程度上解决了集群部署环境下session共享的问题。

fair:智能调整调度算法,动态的根据后端服务器的请求处理到响应的时间进行均衡分配,响应时间短处理效率高的服务器分配到请求的概率高,响应时间长处理效率低的服务器分配到的请求少;结合了前两者的优点的一种调度算法。但是需要注意的是Nginx默认不支持fair算法,如果要使用这种调度算法,请安装upstream_fair模块。

url_hash:按照访问的url的hash结果分配请求,每个请求的url会指向后端固定的某个服务器,可以在Nginx作为静态服务器的情况下提高缓存效率。同样要注意Nginx默认不支持这种调度算法,要使用的话需要安装Nginx的hash软件包。

几种常用web服务器对比

对比项 Apache Nginx Lighttpd
Proxy代理 非常好 非常好 一般
Rewriter 非常好 一般
Fcgi 不好 非常好
热部署 不支持 支持 不支持
系统压力 很大 很小 比较小
稳定性 非常好 不好
安全性 一般 一般
静态文件处理 一般 非常好
反向代理 一般 非常好 一般

 

(完)

Guess you like

Origin blog.csdn.net/qq_22167989/article/details/94591937