Common load balancing strategy are: (high performance cost more down the better)
- Independent Ngnix load or HAProxy
- LVS (DR: one kind of embodiment lvs) + Nginx Program
- DNS polling scheme + LVS + Ngnix
- Intelligent DNS (DNS routing) + LVS + Nginx program
About Nginx
nginx scheduling mode: hash selection, polling, weighted round-robin, etc.
Hash algorithms hash core consistency choose:
Common modules of Nginx
Key: nginx is to support third-party plug-in modules by way of
gzip compression module (carrying)
Scenario: In response to the request back to the client compression
#开启gzip的压缩服务
gzip on;
#设置临时内存空间
gzip_buffers 2 8k;
#设置需要压缩的最小大小 超过此值的话,就对其进行压缩
gzip_min_length 5k;
#压缩基于的http协议
gzip_http_version 1.1;
#设置压缩级别 级别越高 压缩效果好 但是消耗CPU越高
gzip_comp_level 5;
#设置需要压缩的数据类型 图片没必要
gzip_types text/HTML text/plain application/x-javascript text/css application/xml;
rewrite module (comes)
Scenario: for URL redirection (redirect part of client behavior)
获取请求的全局变量
#https://yq.aliyun.com/ziliao/29302
$remote_addr 客户端ip
$remote_port 客户端port
$remote_user 等同于用户名,由ngx_http_auth_basic_module认证
$request_filename 当前请求的文件的路径名,由root或alias和URI request组合而成
$request_body_file
$request_uri 含有参数的完整的初始URI
重定向关键字
redirect:通知客户端重新定向到rewrite后面的地址
permanent:通知客户端永久定位到rewrite后面的地址
last:将rewrite后面的地址重新在server中执行
break:将rewrite的地址重新在当前的location标签中执行
server
{
location ...{
if(...) #通过获取nginx全局变量进行条件判断
{
rewirte URL 重定向关键字;
}
}
}
proxy_cache module (comes)
Scenario: cache static files
https://www.cnblogs.com/maxomnis/p/5534374.html
server {
listen 80;
server_name www.test.com;
charset utf-8;
access_log logs/test.access.log main;
#location /
#{
# proxy_set_header Host $host;
# proxy_set_header X-Forwarded-For $remote_addr;
# proxy_pass http://my_server_pool;
#
#}
#用于清除缓存,假设URL为my.domain.com/test.gif 通过http://my.domain.com/purge/test.gif可以清除该URL的缓存
location ~ /purge(/.*)
{
#return 402;
#设置值允许指定的IP或IP可以清除URL缓存
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
#proxy_pass http://my_server_pool;
#proxy_cache cache_one;
#proxy_cache_key $host$uri$is_args$args;
#proxy_cache_purge PURGE from 127.0.0.1;
}
#使用nginx作为缓存,缓存静态文件
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
{
#使用web缓存区cache_one (在nginx.conf文件定义的)
proxy_cache cache_one;
expires 1h;
#对不同http状态马缓存设置不同的缓存时间
proxy_cache_valid 200 304 12h; #这里表示如果访问的返回的是200或者304状态码,那么缓存12h,在这个12之内,即使静态资源有改变,也会返回缓存的,所以资源改变了,需要删除缓存的资源
proxy_cache_valid 301 302 1m;
proxy_cache_valid any 1m;
#设置web缓存的key值,nginx根据key值md5哈希存储缓存,这里根据域名,uri, 参数 组合合成key.
proxy_cache_key $host$uri$is_args$args;
#反向代理,访问后端内容源服务器
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://my_server_pool;
}
#扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://my_server_pool;
}
}
}
Health check module (third-party support)
Scenario: cluster node health check of third-party modules
https://www.cnblogs.com/felixzh/p/9016116.html
Thumbnail images dynamic module (third-party support)
application scenarios: problem solving picture displayed in different pixels in different modules
https://blog.csdn.net/huangbaokang/article/details/79965405
nginx.conf brief overview
#表示操作系统启动多少进程数来启动ngnix a
worker_processes 1;
#由于Nginx选择的I/O模型都是NIO (基于事件驱动的)
events{
#表示每个nginx线程在工作时可建立的最大连接数 b
#作为服务器 nginx总连接数量=a*b 作为反向代理服务器=a*b/4
worker_connections 4;
#epoll是一种NIO
use epoll;
}