The basic functions and principles of nginx

A, Nginx modules and works

  nginx by the kernel and modules:

  1. Core: its design is very small and simple, the work done is very simple. Lookup profile only by the client request is mapped to a location Block (nginx location is a configuration command, a URL matching cases), and each instruction in this location will be configured in different modules takes startup completion of the corresponding jobs.

  2 is divided into 'core module, the base module, the third party module' from the structures:

   Core modules: HTTP module, EVENT module, MAIL module.

   The base module: HTTP Access module, HTTPFastCGI module, HTTP Proxy module, HTTP Rewrite module.

   Third-party modules: HTTP Upstream Request Hash module, Notice module, HTTP Access Key modules and module development according to their own needs.

  3. from the division function 'Handlers, Filters, Proxies':

   The Handlers (processor modules): direct the module out of such a request, and outputs the content information and the like headers and modification operations. Handlers processor modules typically have only one.

   The Filters (filter module): This module is mainly the content of the other processor modules to modify operation of the output, the last output nginx.

   Proxies (proxy class module): This module is nginx HTTP Upstream quality of the modules, which primarily interact with back-end some services (eg FastCGI), to achieve load balancing service agents and other functions.

  Below, shows a conventional module Nginx HTTP request and response process:

  nginx itself very little work done, when it is connected to an HTTP request, it is only by looking this profile request Block mapped to a location, and each instruction in this location will be configured to start in different modules takes to complete work, so the module can be seen as nginx real labor workers. A location generally involves a command handler module and a plurality of filter modules (of course, a plurality of modules can be multiplexed location). handler module is responsible for processing the request, generates the corresponding content is completed, and the filter processing module corresponding contents.

  nginx modules are compiled directly nginx only, hide compile a static way. After starting nginx, nginx modules are loaded automatically, you do not want to Apache, first of all modules compiled into a file so, then specify whether to load the configuration file. When parsing the configuration file, nginx each module are likely to deal with a request, but can only be done by a module with a processing request.

 Two ,, what do nginx

  1. Forward Proxy

  Simply put, I am a user, I can not access a Web site directly, but I was able to access a proxy server, the proxy server can access sites I can not access the budget I would like to connect to the proxy server, telling it I need to access that can not be content of the site visit, the proxy server to get back, and then return to me. From the perspective of the site, just in time to take the contents of the proxy server has a record. The conclusion is, forward proxy, the server is located between the client and the origin server (origin server), in order to obtain the client from the origin server transmits a request to the proxy and specify the original destination (origin server), then the agent original content server to transmit the request and get back to the client. The client must make some special settings to use the forward proxy.

  2 . Reverse Proxy

  Such as the need to access localhost: 8080 / views / test1 this page, but not the resources on the server test1 view corresponding resources it is called from another server. In this way, we view the corresponding server on the use of reverse proxy. That is, the user need only request to a specific reverse proxy server, users do not need to know who specifically request processing, unified treatment by the proxy server. Reverse proxy for clients, it looks like the original server, the client does not need to make any special settings. Reverse proxy client to the content namespace (namespace) in the normal request is sent, followed by a reverse proxy server requests to speak judgment where (the original server) to transmit and returned content (like these is its own original content Like) returned to the client.

  Typical uses forward proxy is to provide a way to access the Internet LAN clients inside the firewall. Forward proxy can also use the buffer feature reduces network utilization. Typical uses reverse proxy is to provide a server behind the firewall to the Internet users. The reverse proxy can also provide back-end load balancing multiple servers, or provide a buffer for the back-end service slow servers. In short, forward proxy, a corresponding client, the client proxy agent, forward the request and the contents get back to the client; reverse proxy, the client, the proxy server as an origin server, proxy web server cluster node returns the result. nginx is a reverse proxy server.

   3. Load Balancing

 

   Nginx load balancing is a common feature, namely the so-called load balancing share for execution on multiple operating units, such as web servers, ftp servers, business-critical application servers and other mission-critical servers, so as to work together to complete the task. Briefly it is, when there are two or more servers, according to the rules randomly distribute requests to the server process making, load-balancing configuration generally need to configure the reverse proxy, to jump through the reverse proxy load balancer. Nginx currently offers three built-in load balancing, there are two kinds of popular third-party policy.

  3.1 RR: mounting polling (default) mode load, each request individually assigned to a different time order back-end server, if the back-end server is down, can be automatically removed. While this approach is simple, low cost, but the drawback is: reliability, low and uneven load distribution.

  3.2 权重:指定轮询几率,weight和访问比例成正比,用于后端服务器性能不均的情况。

  此时,8080和8081分别占90%和10%。

  3.3 ip_hash:上面2种方式都有一个问题,就是下一个请求来的时候,请求可能分发到另外一个服务器,当我们的程序不是无状态的时候(采用session保存数据),这个时候,就有一个很大的问题,比如把登录信息保存到了session中,那么跳转到另外一个服务器的时候就需要重新登录了,所以很多时候,我们只需要一个客户只访问一个服务器,那么就需要用到iphash。iphash的每个请求按访问ip的hash结果分配,这个每个访客访问一个后端服务器,可以解决session问题。

  3.4 fair(第三方):按后端服务器的响应时间来分配请求,响应时间短的优先分配。

   3.5 url_hash(第三方):按访问URL的hash结果进行分配请求,使每个URL定向到同一个后端服务器,后端服务器为缓存时比较有效。在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用hash算法。

  4.HTTP服务器(包括动静分离)

 

Guess you like

Origin www.cnblogs.com/fancy-yeah/p/9120240.html