Nginx reverse proxy and load balancing of Web services

Source: Internet
Author: User
Tags nginx reverse proxy
<span id="Label3"></p><p><p><strong><span style="font-size:18px;">first, the agent</span></strong></p></p><p><p><strong>Forward Proxy:</strong><br></p></p><p><p>A forward proxy is a server that is located between the client and the destination server, in order to obtain content from the target server, the client sends a request to the agent and specifies the target server, and the agent forwards the request to the destination server and returns the obtained content to the Client. The client must make some special settings to use the forward Proxy.<span style="color:#333333;font-family:arial, ‘宋体‘, sans-serif;font-size:14px;line-height:24px;text-indent:28px;background-color:#ffffff;"></span></p></p><p><p><strong>Role:</strong></p></p> <ul class="list-paddingleft-2" style="list-style-type:disc;"> <ul class="list-paddingleft-2" style="list-style-type:disc;"> <li><p>Access to inaccessible servers (turn over the wall, Understand)</p></li> <li><p>Accelerated access to target server (link Acceleration)</p></li> <li><p>Cache caching (access Acceleration)</p></li> <li><p>Implementing Client Access Authorization</p></li> <li><p>Hide Visitors</p></li> </ul> </ul><p><p><br></p></p><p><p><strong>Reverse Proxy:</strong><br></p></p><p><p>the <span style="color:#333333;font-family:arial, ‘宋体‘, sans-serif;font-size:14px;line-height:24px;text-indent:28px;background-color:#ffffff;">Reverse Proxy method</span> refers to a proxy server that accepts connection requests on the internet, then forwards the request to a server on the internal network and returns the results from the server to the client requesting the connection on the internet, Reverse. At this point the proxy server is represented as a server externally.<span style="color:#333333;font-family:arial, ‘宋体‘, sans-serif;font-size:14px;line-height:24px;text-indent:28px;background-color:#ffffff;"></span></p></p><p><p><strong>Role:</strong><br></p></p> <ul class="list-paddingleft-2" style="list-style-type:disc;"> <ul class="list-paddingleft-2" style="list-style-type:disc;"> <li><p>Protection box hides the original resource server</p></li> <li><p>Achieve load Balancing</p></li> <li><p>Transparent proxy</p></li> </ul> </ul><p><p><br></p></p><p><p><strong><span style="font-size:18px;">second, Nginx Reverse Proxy</span></strong></p></p><p><p><strong>1.HTTP Proxy Module</strong></p></p><p><p>The HTTP proxy module of Nginx is HTTP Proxies.</p></p><p><p>http://www.howtocn.org/nginx:nginx%E6%A8%A1%E5%9D%97%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C%E4%B8%AD%E6%96%87%E7% 89%88:standardhttpmodules:httpproxy#proxy_pass</p></p><p><p>This module can forward requests to other Servers.<br>http/1.0 cannot use keepalive (the back-end server will create and delete connections for each request). Nginx sends http/1.1 to the browser and sends http/1.0 to the backend server so that the browser can handle keepalive for the Browser.<span style="font-family:‘lucida grande‘, verdana, lucida, helvetica, arial, sans-serif;font-size:13px;text-align:justify;background-color:#ffffff;"></span></p></p><p><p>Note When using HTTP proxy modules (even fastcgi), All connection requests are cached before they are sent to the backend server, so the progress of the data transmitted from the backend may not be displayed correctly when it is Measured.<span style="font-family:‘lucida grande‘, verdana, lucida, helvetica, arial, sans-serif;font-size:13px;text-align:justify;background-color:#ffffff;"></span></p></p><p><p>The proxy module is very powerful and commands a Lot.<br></p></p><p><p>This is all about Proxy_pass and proxy_set_header.</p></p><pre class="brush:bash;toolbar:false;"><pre class="brush:bash;toolbar:false;">location/{ proxy_pass http://localhost:8000; #使用字段: location, IF field in location proxy_set_header x-real-ip $remote _addr; #代理服务器发代理给后端服务器的请求, the client host IP #指令允许将发送到被目标服务器的请求头重新定义或者增加一些字段 is not included by default and is defined as #注 to add a client ip}: Backend httpd server profile only log file definition modification to display client IP in log file # modified: Logformat "%h%l%t \"%r\ "%>s%b \"%{referer}i\ "\"%{user-agent}i\ "" Combined#: logformat "%{x-real-ip}i%l%t \"%r\ "%>s%b \"%{referer}i\ "\"%{user-agent}i\ "" combined</pre></pre><p><p><br></p></p><p><p><strong>2. Load Balancing</strong></p></p><p><p>Load balancing is also implemented by the reverse proxy, except that the target server for the agent is a group. The load-balancing scheduling algorithm is then used to process requests according to different business Requirements.</p></p><p><p>Nginx through the Upstream module to achieve load balancing, define the target server group, set up scheduling algorithm.</p></p><pre class="brush:bash;toolbar:false;"><pre class="brush:bash;toolbar:false;">Upstream backend { round-robin; #调度算法 server backend1.example.com weight=5; #设置服务器权重, The default is 1 (cannot be used with the Ip_hash directive) server backend2.example.com:8080; #自定义端口 server backend3.example.com down ; #down-the tag server is offline and is typically used with Ip_hash. #backup-(0.6.7 or Higher) If all Non-backup servers are down or busy, use this server (cannot be used in conjunction with Ip_hash directives). server backend4.example.com max_fails=3 fail_timeout=30s; #max_fails-check the Server's maximum number of failed requests within a certain amount of time (fail_timeout definition), default to 1, set it to 0 to turn off the time to pause the service after checking #fail_timeout-max_fails failures, The default is 10s server unix:/tmp/backend5; The server that #可同时监听Unix the Socket. }server {location /{ proxy_pass http://backend; #反向代理 }}</pre></pre><p><p><br></p></p><pre class="brush:bash;toolbar:false;"><pre class="brush:bash;toolbar:false;">#调度算法1. Round-robin Polling (default): each request is assigned to a different Back-end server in chronological order, and if a server on the backend is down, the fault system is automatically rejected, leaving the user access Unaffected. Weight Specifies a polling weight value, the higher the Weight value, the higher the access probability assigned to it, which is primarily used in the case of uneven performance per server in the Backend. 2.ip_hash: each request is allocated according to the hash result of the access ip, so that visitors from the same IP have fixed access to a back-end server, which effectively solves the session sharing problem of dynamic web Pages. 3.fair: more intelligent load balancing algorithms than the above two. This algorithm can be intelligently load balanced according to the page size and load time, that is, the response time of the backend server to allocate the request, the response time is short priority allocation. Nginx itself is not support fair, if you need to use this scheduling algorithm, you must download the Nginx Upstream_fair module. 4.url_hash: This method allocates requests according to the hash result of the access url, directing each URL to the same back-end server, which can further improve the efficiency of the backend cache server. Nginx itself is not support url_hash, if you need to use this scheduling algorithm, you must install Nginx hash package.</pre></pre><p><p><br></p></p><p><p><br></p></p><p><p>This article is from "handsome brother is a prophet" blog, please be sure to keep this source http://hoolee.blog.51cto.com/7934938/1413522</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.