Nginx feature: Simple use of upstream modules and caches
Objective:
This article goes on to nginx feature: from compile to install to URL rewrite to introduce the use of Nginx load Balancer module, the experiment in this paper does not consider the majority of cases, such as data synchronization between two Web servers, etc., mainly write how nginx is used as a load balancer and cache
Experimental topology
650) this.width=650; "src=" Http://www.178linux.com/ueditor/php/upload/image/20160407/1460035215620288.png "title= "1460035215620288.png" alt= "Blob.png" style= "height:auto;vertical-align:middle;border:0px;"/>
Experimental environment
Host |
IP Address |
function |
Lb.anyisalin.com |
172.16.1.2 |
Load balancing and caching of static resources |
Web1.anyisalin.com |
172.16.1.3 |
Provide Web services |
Web2.anyisalin.com |
172.16.1.4 |
Provide Web services |
注意: 本文所有操作都在SELinux和iptables关闭的情况下进行
Nginx_upstream Module Introduction
The Ngx_http_upstream_module module is used to define groups of servers and can be referenced by the Proxy_pass, Fastcgi_ Pass, Uwsgi_pass, Scgi_pass, and memcached_pass directives. Excerpted from official documents
In short, it is through upstream
module defines a server group that can be combined with other proxy modules to provide load balancing effects
Upstream how to use
upstream servers {
< Span class= "Hljs-title" style= "line-height:1.6;font-weight:bold;" >server 172.16.1.2 ;
172.16.1.3 ;
&NBSP;}
location /{
< Span class= "Hljs-title" style= "line-height:1.6;font-weight:bold;" >proxy_pass http://servers ;
&NBSP;}
Introduction of common parameters of upstream
Upstream name{
[Ip_hash]
Server Address [Weight=number] [Max_fails=number] [Fail_timeout=number] [Down|up|backup];
...
}
#ip_hash: Similar toLVsOfSHAlgorithm that will be the sameIPRequests are directed to the same host
#weight: Weight Setting
#max_fails:UpstreamThe ability to automatically check the health status of servers in each group, which represents the maximum number of connection failures that can be removed when checking for health status
#fail_timeout: How long does the timeout count fail
#down: A host is offline
#backup: Set a host to backup mode, as long as not all hosts are down, it will not be online
Upstream Use example
The process of installing nginx here does not show, interested can see my previous blog
在nginx主配置文件中添加以下几列
http段添加
upstream servers {
server 172.16.1.3;
server 172.16.1.4;
}
server段添加
location / {
proxy_pass http://servers;
}
重载nginx
Look at the picture! We implemented a simple load balancing effect, so we used a different page to demonstrate the effect
650) this.width=650; "src=" Http://www.178linux.com/ueditor/php/upload/image/20160407/1460035896287733.gif "title= "1460035896287733.gif" alt= "If water gif_2016 April 7 19:35 7 seconds." GIF "style=" height:auto;vertical-align:middle;border:0px; "/ >
Nginx_cache Introduction
we all know that in today's online world, Cache is King
, the cache can reduce the burden on the server and speed up the response, the Nginx_proxy module can support the caching function, we use here to cache static resources
Caching Static Resources
We have a set of static Web pages on the WEB1 server that are reverse proxies through the Load Balancer Scheduler, and we use AB for stress testing before we set up the cache.
650) this.width=650; "src=" Http://www.178linux.com/ueditor/php/upload/image/20160407/1460035486186934.png "title= "1460035486186934.png" alt= "Blob.png" style= "height:auto;vertical-align:middle;border:0px;"/>
Proxy_cache_path/cache levels=1:1Keys_zone=mycache: -M#http段添加
location/index.html {#Location段配置
Proxy_pass http://172.16.1.3;
Proxy_cache Mycache;#使用mycache进行缓存
Proxy_cache_valid $ 1D#响应码为 cache for 1 days
Proxy_cache_valid - 501 502 503 1M#响应码为... Cache for 1 minutes
Proxy_cache_use_stale error;#如果请求源主机的时候出问题, the cache is still used
}
Saving overloads
Mkdir/cache#创建缓存存放文件夹
Chown Nginx:nginx/cache#修改属主和属组为nginx
650 ) this.width=650; "src=" Http://www.178linux.com/ueditor/php/upload/image/20160407/1460035490911410.png "title=" 1460035490911410.png "alt=" Blob.png "style=" height:auto;vertical-align:middle;border:0px; "/>
从上面测试可以看出缓存对于静态资源响应的提升是非常明显的
Summarize
Because of the time is tight, all this article simply introduced the Nginx_upstream module and the use of the cache, many configuration parameters are not clearly explained, the future should have the opportunity and everyone in detail, nginx topic is not over, please look forward to
Author: Anyisalin qq:1449472454
Thank you: mageedu
This article is from the "Road of Operations Engineers" blog, please be sure to keep this source http://anyisalin.blog.51cto.com/10917514/1761500
Nginx feature: Simple use of upstream modules and caches