小成本的负载均衡器-nginx
Posted 花鸟在线
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小成本的负载均衡器-nginx相关的知识,希望对你有一定的参考价值。
“ 相遇是一种缘分”
初衷
—
其实,如论是开发、运维还是安全,讲的都是计算机知识。作为人类的智慧产物。知识可以分类,但不能过于强调技能分类。而应该分清深浅,并且最终应该得是串联。如果我们一味强调学习什么,那么可能从一开始的认知上就很局限性了。
我相信在大多数企业在建立负载均衡器方面是有曾考虑过nginx的。因此ngnix的活跃性才丝毫没有被淡忘。从个人来讲nginx是自由软件且口碑又好,足够为私人网站提供高可靠和便利条件,如果将其作为反向代理,能够直接屏蔽服务器的真实IP。如果不能应用这么好用的东西往后有何颜面跟人家夸夸其谈。
目前网上有许多部署nginx的方案,更甚有docker的一部署策略支持,可谓是开箱即用了。但是我们今天还是选择通过编译安装的方式来熟悉一下这套产品。
实践
—
以上是结构
0x01 准备:
作为nginx服务器:
CentOS Linux release 7.8.2003 (Core)
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
再准备两三台后端服务器:
metasploitable
0x02 nginx搭建:
1.到网上平台下载Nginx相关的组件
2.然后解压并编译安装
make && make install
分别在四个目录进行执行
[root@localhost openssl-fips-2.0.10]# ./config &&make &&make install
[root@localhost pcre-8.40]# ./configure &&make &&make install
[root@localhost zlib-1.2.11]# ./configure &&make &&make install
[root@localhost nginx-1.10.2]# ./configure &&make &&make install
注:make作为编译安装之所以能进行,是当前或制定路径下的有Makefile规则文件,该文件按一定规则将软件包通过gcc-c++去组合并编译
default: build
clean:
rm -rf Makefile objs
build:
$(MAKE) -f objs/Makefile
install:
$(MAKE) -f objs/Makefile install
modules:
$(MAKE) -f objs/Makefile modules
upgrade:
/usr/local/nginx/sbin/nginx -t
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
所以文件更新的位置等可以通过修改Makefile规则去改变。
3.启动服务
编译后默认会启动nginx服务,或者手动启动:/usr/local/nginx/sbin/nginx
更多操作:
[root@localhost nginx-1.10.2]# /usr/local/nginx/sbin/nginx -h
nginx version: nginx/1.10.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the php scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
0x03 负载均衡:
配置规则
# 负载均衡规则
upstream htmlserver{
server 192.168.43.38:80;
server 192.168.43.165:80;
}
server {
# 监听80端口
listen 8080;
# 主机地址
server_name www.xxx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# 被代理服务器映射规则
proxy_pass http://htmlserver;
}
}
视频演示效果(可能看不了):
传递思路(nginx作为前置代理,后端布置两套应用,当中断任意一套后端系统,客户端基本无感):
看几组图片效果:
小结
—
以上是关于小成本的负载均衡器-nginx的主要内容,如果未能解决你的问题,请参考以下文章