Nginx服务器
重要通知
。
Nginx基本概况
Nginx是一款由C语言编写的Web服务器,聚焦于轻量级、高并发、高性能、高度模块化的设计与低内存消耗问题。
- 官网:http://nginx.org/en/
- 菜鸟教程:https://www.runoob.com/linux/nginx-install-setup.html
- 配置:https://www.runoob.com/?s=nginx
- 下载版本:http://nginx.org/download/
- HTTPS配置:https://nginx.org/en/docs/http/configuring_https_servers.html
- HTTP 负载均衡器:
- 文档:https://nginx.org/en/docs/
- 模块列表:https://nginx.org/en/docs/
- 重写规则:https://nginx.org/en/docs/http/converting_rewrite_rules.html
- WebSocket:https://nginx.org/en/docs/http/websocket.html
- Tengine:淘宝团队
安装编译
安装编译工具及库文件
yum -y install make zlib zlib-devel gcc gcc-c++ libtool openssl openssl-devel gd gd-devel pcre pcre-devel
安装 PCRE,让 Nginx 支持 Rewrite 功能 | 可以 yum -y install pcre安装pcre
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz tar zxvf pcre-8.35.tar.gz mv pcre-8.35 /home/app/ cd /home/app/pcre-8.35 ./configure make && make install pcre-config --version #查看版本
下载安装Nginx
wget http://nginx.org/download/nginx-1.22.0.tar.gz tar zxvf nginx-1.22.0.tar.gz mv nginx-1.22.0 /home/app/ cd /home/app/nginx-1.22.0 ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module make && make install
如果make && make install出错,解决方案如下
错误一:vim /home/app/nginx-1.22.0/objs/Makefile | 删除-Werror即可 | CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused -Werror -g 错误二:服务器或nginx版本过高:vim src/os/unix/ngx_user.c | 将该行注释掉 | cd.current_salt[0] = ~salt[0]; 步骤三:oppensl版本问题:make[1]: *** [objs/Makefile:793: objs/src/event/ngx_event_openssl.o] Error 1
windows安装
http://nginx.org/en/download.html
安装步骤
> D:\software\nginx-1.18.0 # 进入目录
> start nginx # 开启nginx服务
> nginx -s reopen # 重新打开日志文件命令
> nginx -v
> ./nginx.exe -s stop # 关闭nginx服务,快速停止nginx,可能并不保存相关信息
> ./nginx.exe -s quit # 关闭nginx服务,完整有序的停止nginx,并保存相关信息
> ./nginx.exe -s reload # 重载nginx服务,当你改变了nginx配置信息并需要重新载入这些配置时可以使用此命令重载nginx
> ./nginx.exe -s reopen
Mac安装Nginx
> nginx -v
> brew search nginx
> brew info nginx # 查看需要安装的信息
> sudo brew install nginx # 安装
> /usr/local/etc/nginx/servers/. # 查看安装目录
> brew services restart nginx
MAC报错
Error: No such file or directory @ rb_sysopen - /Users/apple/Library/Caches/Homebrew/downloads/4f91ccc3b48ccc78bee87e4d4e3d9bb90e5b2f512d7ce8eb85f62d903251ffd7--ca-certificates-2022-04-26.all.bottle.tar.gz 解决方案,单独安装:brew install ca-certificates
基础指令
> /usr/local/webserver/nginx/sbin/nginx -t #查找nginx.conf
> /usr/local/webserver/nginx/sbin/nginx -v #查看版本
> /usr/local/webserver/nginx/sbin/nginx #启动
> /usr/local/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件
> /usr/local/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx
> /usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx
配置文件
vim /usr/local/webserver/nginx/conf/nginx.conf
查看进程
ps -ef|grep nginx
客户端配置参数
在nginx的配置文件中进行配置,限制请求报文体的大小,nginx默认大小是1M。
# 对发自客户端的http头信息的大小进行了限制,超过其中一个值则服务器会返回错误状态码 414(Request-URI Too Large)
client_header_buffer_size
# 对nginx服务器接受客户端请求的头信息时所分配的最大缓冲区的大小,即nginx服务器一次接受一个客户端请求可就收的最大都信息大小。
large_client_header_buffers 7512k;
Nginx架构设计
配置参考示例
示例文件
# 服务器配置:百度云 2核4G
# 上传文件
# put D:/server/webserver/bdys/nginx.conf /usr/local/webserver/nginx/conf
#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;
#########################################
# 负载均衡
#########################################
upstream svrsrm {
server 49.235.198.78:8090; #腾讯云
server 110.43.33.21:8090; #金山云
}
server {
listen 8090;
server_name 106.13.47.239;
location / {
proxy_pass http://svrsrm;
}
}
#测试服务器入口
server {
listen 8080;
server_name 106.13.47.239;
#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;
#}
}
#########################################
# 资源上传服务器
#########################################
upstream staticupload {
server 127.0.0.1:8198;
}
server {
listen 8199;
server_name 106.13.47.239;
location / {
proxy_pass http://staticupload;
}
}
#########################################
# 静态资源服务器
#########################################
server {
keepalive_requests 100000;
listen 7898;
server_name 127.0.0.1;
root /home/depository;
location ~.*\.(jpg|jpeg|png|bmp|gif|ico|mp3|m4a|mp4|swf|flv)$ {
expires 7d; #过期时间为7天
}
}
upstream staticweb {
server 127.0.0.1:7898;
}
server {
listen 7899;
server_name 106.13.47.239;
location / {
proxy_pass http://staticweb;
}
}
#####################################
# www.fujinhuo.com
server {
listen 80;
server_name www.fujinhuo.com;
root /home/webserver/fujinhuo;
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
# m.fujinhuo.com
server {
listen 80;
server_name m.fujinhuo.com;
root /home/fujinhuo/webapp;
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
#####################################
# www.ysunlight.com
server {
listen 80;
server_name www.ysunlight.com;
root /home/webserver/ysunlight/web;
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
# m.ysunlight.com
server {
listen 80;
server_name m.ysunlight.com;
root /home/webserver/ysunlight/webapp;
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
# vscode.ysunlight.com
upstream vscode {
server 106.12.193.24:8012;
}
server {
listen 80;
server_name vscode.ysunlight.com;
location / {
proxy_pass http://vscode;
}
}
# 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;
# }
#}
}
include 修饰符
修饰符 处理方式
= 精确匹配且终止搜索 ~ 区分大小写 ~* 不区分大小写 ^~ 优先于正则匹配
导入文件
include blockip.conf;
user www www;
worker_processes 2;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 2048;
}
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 65;
# gzip压缩功能设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
# http_proxy 设置
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /usr/local/nginx/proxy_temp 1 2;
# 设定负载均衡后台服务器列表
upstream backend {
#ip_hash;
server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
}
# 很重要的虚拟主机配置
server {
listen 80;
server_name itoatest.example.com;
root /apps/oaapp;
charset utf-8;
access_log logs/host.access.log main;
#对 / 所有做负载均衡+反向代理
location / {
root /apps/oaapp;
index index.jsp index.html index.htm;
proxy_pass http://backend;
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
#静态文件,nginx自己处理,不去backend请求tomcat
location ~* /download/ {
root /apps/oa/fs;
}
location ~ .*/.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /apps/oaapp;
expires 7d;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.10.0/24;
deny all;
}
location ~ ^/(WEB-INF)/ {
deny all;
}
#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;
}
}
## 其它虚拟主机,server 指令开始
}
# ########################################
# #全局块
# ########################################
#
nobody nobody; # 用户组
pid /nginx/pid/nginx.pid; # nginx进程pid存放路径
error_log log/error.log debug; # 日志存放路径, 级别以此为:debug | info | notice | warn | error | crit | alert | emerg
# debug 级别的错误只有在编译时配置了--with-debug 选项才可以使用
# 配置文件引入
worker_processes 2; # 指定 worker 进程启动的数量, 设置该参数的值与 CPU 绑定的负载处理器核心的数量相同
worker connections; # 配置一个工作进程能够接受并发连接的最大数
# ########################################
# #events块
# ########################################
#
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
use epoll; #事件驱动模型, 选取哪种事件驱动模型处理连接请求, select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #每个进程的最大连接数,默认为512
}
# ########################################
# #http块
# ########################################
#
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
access_log off; #取消服务日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
access_log log/access.log myFormat; #combined为日志格式的默认值
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #热备
}
error_page 404 https://www.baidu.com; #错误页
server { # server块, 在http块内可以包含多个server块
keepalive_requests 120; #单连接请求上限次数。
listen 4545; #监听端口
server_name 127.0.0.1; #监听地址
location [PATTERN] { # location块, 在server块内可以包含多个location块
# PATTERN, 请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写
root path; #根目录
index vv.txt; #设置默认页
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 172.18.5.54; #允许的ip
}
}
}
功能模块配置示例
反向代理
# 项目岛
upstream projects {
server 139.9.198.198:8089;
}
# 资源岛
upstream resources {
server 139.9.198.198:9092;
}
# 微服务 | 主应用
server {
listen 8016;
server_name localhost;
root D:/devpt/qkhtml/dist;
location /api {
proxy_pass http://projects;
}
location /user-api {
proxy_pass http://resources/api;
}
location /admin-api {
proxy_pass http://resources/admin;
}
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
upstream pxyStrm {
server 49.235.198.78:8091;
}
server {
listen 80;
server_name www.fujinhuo.com;
location /api/goods-list {
proxy_set_header Host $host; #Host 头
# 客户端 IP 地址将会通过 X-Real-IP Forwarded-For 头来实现
proxy_set_header X-Real-IP $remote_addr; #真实客户端ip传给后端服务器
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://pxyStrm;
}
#
}
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
#access_log off; #取消服务日志
log_format myFormat ' $remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
access_log log/access.log myFormat; #combined为日志格式的默认值
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
proxy_connect_timeout 1; #nginx服务器与被代理的服务器建立连接的超时时间,默认60秒
proxy_read_timeout 1; #nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒。
proxy_send_timeout 1; #nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒。
proxy_http_version 1.0 ; #Nginx服务器提供代理服务的http协议版本1.0,1.1,默认设置为1.0版本。
#proxy_method get; #支持客户端的请求方法。post/get;
proxy_ignore_client_abort on; #客户端断网时,nginx服务器是否终端对被代理服务器的请求。默认为off。
proxy_ignore_headers "Expires" "Set-Cookie"; #Nginx服务器不处理设置的http相应投中的头域,这里空格隔开可以设置多个。
proxy_intercept_errors on; #如果被代理服务器返回的状态码为400或者大于400,设置的error_page配置起作用。默认为off。
proxy_headers_hash_max_size 1024; #存放http报文头的哈希表容量上限,默认为512个字符。
proxy_headers_hash_bucket_size 128; #nginx服务器申请存放http报文头的哈希表容量大小。默认为64个字符。
proxy_next_upstream timeout; #反向代理upstream中设置的服务器组,出现故障时,被代理服务器返回的状态值。error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off
#proxy_ssl_session_reuse on; 默认为on,如果我们在错误日志中发现“SSL3_GET_FINSHED:digest check failed”的情况时,可以将该指令设置为off。
负载均衡
#示例
upstream svrsrm {
server 49.235.198.78;
server 110.43.33.21:9011;
}
server {
location [PATTERN] {
proxy_pass http://svrsrm;
}
}
#参数配置
upstream svrsrm {
#down:表示当前的server暂时不参与负载均衡
#backup:预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。
#max_fails:允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误
#fail_timeout:在经历了max_fails次失败后,暂停服务的时间, 与max_fails一起使用
server 127.0.0.1:7878 weight=2 max_fails=2 fail_timeout=2;
}
#热备, 如果你有2台服务器,当一台服务器发生事故时,才启用第二台服务器给提供服务
upstream svrsrm {
server 49.235.198.78;
server 110.43.33.21:9011 backup;
}
#轮询, nginx默认就是轮询其权重都默认为1
upstream svrsrm {
server 49.235.198.78;
server 110.43.33.21:9011;
}
#加权轮询, 跟据配置的权重的大小而分发给不同服务器不同数量的请求, 默认为0
upstream svrsrm {
server 49.235.198.78 weight=1;
server 110.43.33.21:9011 weight=2;
}
#ip_hash, nginx会让相同的客户端ip请求相同的服务器
upstream svrsrm {
server 49.235.198.78;
server 110.43.33.21:9011;
ip_hash;
}
upstream sale {
server 192.168.1.100:8000 max_fails=2;
}
upstream matchmaker {
server 192.168.1.200:8080 max_fails=2;
}
server {
listen 80;
server_name www.000.com;
location /sale {
root /www
proxy_pass http://sale;
}
location /matchmaker {
root /www
proxy_pass http://matchmaker;
}
}
获取客户端参数
server {
location / {
#请求方法:$request_method
if ($request_method ~* "(GET|POST)") {
add_header "Access-Control-Allow-Origin" *;
}
}
}
location / {
return 200 "client real ip: $remote_addr\n";
}
proxy_set_header X-Real-I remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# $remote_addr 与 $http_x_forwarded_for 用以记录客户端的ip地址;
# $remote_user :用来记录客户端用户名称;
# $time_local : 用来记录访问时间与时区;
# $request : 用来记录请求的url与http协议;
# $status : 用来记录请求状态;成功是200;
# $body_bytes_s ent :记录发送给客户端文件主体内容大小;
# $http_referer :用来记录从那个页面链接访问过来的;
# $http_user_agent :记录客户端浏览器的相关信息;
#通过http获取客户的真实ip而不是获取代理服务器的ip地址
proxy_set_header Host $host; #只要用户在浏览器中访问的域名绑定了 VIP VIP 下面有RS;则就用$host ;host是访问URL中的域名和端口 www.taobao.com:80
proxy_set_header X-Real-IP $remote_addr; #把源IP 【$remote_addr,建立HTTP连接header里面的信息】赋值给X-Real-IP;这样在代码中 $X-Real-IP来获取 源IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#在nginx 作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来,用 【,】隔开;代码中用 echo $x-forwarded-for |awk -F, '{print $1}' 来作为源IP
集群
静态资源 压缩与缓存
server {
keepalive_requests 100000;
listen 7099;
server_name 127.0.0.1;
location ~.*\.(jpg|jpeg|png|bmp|gif|ico|mp3|m4a|mp4|swf|flv)$ {
root /home/depository/;
expires 7d; #过期时间为7天
}
}
#所有js,css相关的静态资源文件的请求由Nginx处理
location ~.*\.(js|css)$ {
root /opt/static-resources; #指定文件路径
expires 12h; #过期时间为12小时
}
#所有图片等多媒体相关静态资源文件的请求由Nginx处理
location ~.*\.(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ {
root /opt/static-resources; #指定文件路径
expires 7d; #过期时间为7天
}
# Gzip压缩配置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/css text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
跨域CORS
location / {
# 此处仅仅示例,不可用
set $cors_origin $http_origin;
add_header Access-Control-Allow-Origin $cors_origin;
#跨域配置,如果不生效请先清除浏览器缓存数据
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods 'GET, POST, PUT, PATCH, POST, DELETE, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
}
日志服务log
server {
access_log /usr/local/client.log; #日志地址
error_log /usr/local/error.log; #错误日志地址
}
限流与屏蔽
# 屏蔽
deny 165.91.122.67;
# 限制IP的连接和并发
limit_req_zone 用来限制单位时间内的请求数,即速率限制,采用的漏桶算法 "leaky bucket"。
limit_req_conn 用来限制同一时间连接数,即并发限制。
# 代码示例
> limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
第一个参数:$binary_remote_addr 表示通过remote_addr这个标识来做限制,“binary_”的目的是缩写内存占用量,是限制同一客户端ip地址。
第二个参数:zone=one:10m表示生成一个大小为10M,名字为one的内存区域,用来存储访问的频次信息。
第三个参数:rate=1r/s表示允许相同标识的客户端的访问频次,这里限制的是每秒1次,还可以有比如30r/m的。
limit_req zone=one burst=5 nodelay;
第一个参数:zone=one 设置使用哪个配置区域来做限制,与上面limit_req_zone 里的name对应。
第二个参数:burst=5,重点说明一下这个配置,burst爆发的意思,这个配置的意思是设置一个大小为5的缓冲区当有大量请求(爆发)过来时,超过了访问频次限制的请求可以先放到这个缓冲区内。
第三个参数:nodelay,如果设置,超过访问频次而且缓冲区也满了的时候就会直接返回503,如果没有设置,则所有请求会等待排队。
cookie
location ~* \.(gif|jpg|png|bmp)$ {
expires 10d; # 10天
}
error错误
#设置 404 页面导向地址
error_page 404 https://www.runnob.com; #错误页
proxy_intercept_errors on; #如果被代理服务器返回的状态码为400或者大于400,设置的error_page配置起作用。默认为off。
#代理只允许接受get,post请求方法的一种
proxy_method get; #支持客户端的请求方法。post/get;
#设置支持的http协议版本
proxy_http_version 1.0 ; #Nginx服务器提供代理服务的http协议版本1.0,1.1,默认设置为1.0版本
#解决 响应连接时间过长,就会导致客户端的页面一直在等待响应
proxy_connect_timeout 1; #nginx服务器与被代理的服务器建立连接的超时时间,默认60秒
proxy_read_timeout 1; #nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒。
proxy_send_timeout 1; #nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒。
proxy_ignore_client_abort on; #客户端断网时,nginx服务器是否终端对被代理服务器的请求。默认为off。
#如果使用upstream指令配置啦一组服务器作为被代理服务器,服务器中的访问算法遵循配置的负载均衡规则,同时可以使用该指令配置在发生哪些异常情况时,将请求顺次交由下一组服务器处理。
proxy_next_upstream timeout; #反向代理upstream中设置的服务器组,出现故障时,被代理服务器返回的状态值。
# 状态值可以是:error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off
# error:建立连接或向被代理的服务器发送请求或读取响应信息时服务器发生错误。
# timeout:建立连接,想被代理服务器发送请求或读取响应信息时服务器发生超时。
# invalid_header:被代理服务器返回的响应头异常。
# off:无法将请求分发给被代理的服务器。
# http_400,....:被代理服务器返回的状态码为400,500,502,等。
Web缓存
location ~ .*\.(htm|html)$ {
expires 24h;
root /opt/app/code;
}
# 动态缓存设置
HTTPS基础配置
配置HTTPS服务
安装http_ssl_module模块
配置
server { listen 80; server_name www.fujinhuo.com; rewrite ^(.*)$ https://${server_name}$1 permanent;; } server { listen 443 ssl; server_name www.fujinhuo.com; ssl_certificate /usr/local/ssl/nginx.crt; #证书文件 ssl_certificate_key /usr/local/ssl/nginx.key; #私钥文件 ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; #禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击 server_tokens off; #如果是全站 HTTPS 并且不考虑 HTTP 的话,可以加入 HSTS 告诉你的浏览器本网站全站加密,并且强制用 HTTPS 访问 fastcgi_param HTTPS on; fastcgi_param HTTP_SCHEME https; access_log /usr/local/nginx/logs/httpsaccess.log; }
###################################################################################################
百度云配置示例
###################################################################################################
云服务器部署SSL证书-Nginx;https://cloud.baidu.com/doc/BCC/s/yjytsvy5y
登录百度云控制台 | SSL证书 -> 普通SSL证书 -> 购买新证书
配置:查看证书 -> 下载证书(PEM_Nginx) -> 上传证书:使用FTP或者其他工具将上一步中解压得到的crt以及key文件上传到服务器的nginx配置目录/etc/nginx/中。 | put /Users/apple/Downloads/web.ysunlight.com.zip /home/data | put D:/Download/code.ysunlight.com.key /etc/nginx cp -r -f -p /home/meta/Configure/pem-web /home/data mv -iu /home/meta/Configure/webserver/pem-test /home/data
修改配置文件:vim /etc/nginx/nginx.conf server { listen 80; server_name vscode.ysunlight.com; rewrite ^(.*)$ https://${server_name}$1 permanent; } server { listen 443; server_name ********.com; #更换上所绑定的域名,一定要是申请了证书的域名 ssl on; #这一行是另外添加的,意思是打开ssl功能,一定要添加。 ssl_certificate /etc/nginx/vscode.ysunlight.com.crt; #这是下载下来的nginx证书的crt文件路径,绝对或者相对路径都可以 ssl_certificate_key /etc/nginx/vscode.ysunlight.com.key; #和crt的规则一样 ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on;
root /home/meta/List/infinitown;
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
保存配置后,重启nginx服务 此时可通过netstat -anplt查看到443端口已经开放。使用https://域名也可正常打开网站。 设置http强制跳转https:vim /etc/nginx/nginx.conf | 在80的server中,先将server_name后的localhost修改为此证书域名。然后在下面添加上一句: rewrite ^(.*)$ https://${server_name}$1 permanent; 保存配置后,重启nginx服务
Ngxin配置HTTPS
具体配置见document/nginx/configure/https.md
反向代理HTTPS到另外的HTTPS IP
upstream vscode { server 106.12.193.24:8011; } server { listen 80; server_name vscode.ysunlight.com; rewrite ^(.*)$ https://${server_name}$1 permanent; } server { listen 443; server_name ********.com; #更换上所绑定的域名,一定要是申请了证书的域名 ssl on; #这一行是另外添加的,意思是打开ssl功能,一定要添加。 ssl_certificate /etc/nginx/vscode.ysunlight.com.crt; #这是下载下来的nginx证书的crt文件路径,绝对或者相对路径都可以 ssl_certificate_key /etc/nginx/vscode.ysunlight.com.key; #和crt的规则一样 ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on;
location / {
proxy_pass https://vscode;
try_files $uri $uri/ /index.html;
}
}
###################################################################################################
HTTPS基础配置 | Linux环境
###################################################################################################
安装配置
下载:wget --no-check-certificate https://www.openssl.org/source/openssl-3.0.0.tar.gz tar zxvf openssl-3.0.0.tar.gz mv openssl-3.0.0 /home/app/ cd /home/app/openssl-3.0.0 ./config --prefix=/usr/local/openssl [注意:是./config,而不是./configure] make depend # 检查依赖 make && make install
生成证书
cd /usr/local/openssl/bin openssl req -nodes -newkey rsa:1024 -out myreq.pem -keyout privatekey.pem openssl req -in myreq.pem -x509 -key privatekey.pem -out mycert.pem -days 365 # 生成数字证书
基本命令
查看版本:openssl version
配置Nginx
openssl的windows版本
下载:http://slproweb.com/products/Win32OpenSSL.html 安装配置步骤:https://www.cnblogs.com/cx59244405/p/9327461.html
模块配置
--with-http_v2_module
配置参数释义
# 基础指令
> --prefix:指定编译后的存放路径
--sbin-path= 指定执行程序文件存放位置。
--modules-path= 指定第三方模块的存放路径。
--conf-path= 指定配置文件存放位置。
--error-log-path= 指定错误日志存放位置。
--pid-path= 指定pid文件存放位置。
--lock-path= 指定lock文件存放位置。
--user= 指定程序运行时的非特权用户。
--group= 指定程序运行时的非特权用户组。
--builddir= 指向编译目录。
--with-rtsig_module 启用rtsig模块支持。
--with-select_module 启用select模块支持,一种轮询处理方式,不推荐在高并发环境中使用,禁用:--without-select_module。
--with-poll_module 启用poll模块支持,功能与select相同,不推荐在高并发环境中使用。
--with-threads 启用thread pool支持。
--with-file-aio 启用file aio支持。
--with-http_ssl_module 启用https支持。
--with-http_v2_module 启用ngx_http_v2_module支持。
--with-ipv6 启用ipv6支持。
--with-http_realip_module 允许从请求报文头中更改客户端的ip地址,默认为关。
--with-http_addition_module 启用ngix_http_additon_mdoule支持(作为一个输出过滤器,分部分响应请求)。
--with -http_xslt_module 启用ngx_http_xslt_module支持,过滤转换XML请求 。
--with-http_image_filter_mdoule 启用ngx_http_image_filter_module支持,传输JPEG\GIF\PNG图片的一个过滤器,默认不启用,需要安装gd库。
--with-http_geoip_module 启用ngx_http_geoip_module支持,用于创建基于MaxMind GeoIP二进制文件相配的客户端IP地址的ngx_http_geoip_module变量。
--with-http_sub_module 启用ngx_http_sub_module支持,允许用一些其他文本替换nginx响应中的一些文本。
--with-http_dav_module 启用ngx_http_dav_module支持,增加PUT、DELETE、MKCOL创建集合,COPY和MOVE方法,默认为关闭,需要编译开启。
--with-http_flv_module 启用ngx_http_flv_module支持,提供寻求内存使用基于时间的偏移量文件。
--with-http_mp4_module 启用ngx_http_mp4_module支持,启用对mp4类视频文件的支持。
--with-http_gzip_static_module 启用ngx_http_gzip_static_module支持,支持在线实时压缩输出数据流。
--with-http_random_index_module 启用ngx_http_random_index_module支持,从目录中随机挑选一个目录索引。
--with-http_secure_link_module 启用ngx_http_secure_link_module支持,计算和检查要求所需的安全链接网址。
--with-http_degradation_module 启用ngx_http_degradation_module 支持允许在内存不足的情况下返回204或444代码。
--with-http_stub_status_module 启用ngx_http_stub_status_module 支持查看nginx的状态页。
--without-http_charset_module 禁用ngx_http_charset_module这一模块,可以进行字符集间的转换,从其它字符转换成UTF-8或者从UTF8转换成其它字符。它只能从服务器到客户端方向,只有一个字节的字符可以转换。
--without-http_gzip_module 禁用ngx_http_gzip_module支持,同--with-http_gzip_static_module功能一样。
--without-http_ssi_module 禁用ngx_http_ssi_module支持,提供了一个在输入端处理服务器包含文件(SSI)的过滤器。
--without-http_userid_module 禁用ngx_http_userid_module支持,该模块用来确定客户端后续请求的cookies。
--without-http_access_module 禁用ngx_http_access_module支持,提供了基于主机ip地址的访问控制功能。
--without-http_auth_basic_module 禁用ngx_http_auth_basic_module支持,可以使用用户名和密码认证的方式来对站点或部分内容进行认证。
--without-http_autoindex_module 禁用ngx_http_authindex_module,该模块用于在ngx_http_index_module模块没有找到索引文件时发出请求,用于自动生成目录列表。
--without-http_geo_module 禁用ngx_http_geo_module支持,这个模块用于创建依赖于客户端ip的变量。
--without-http_map_module 禁用ngx_http_map_module支持,使用任意的键、值 对设置配置变量。
--without-http_split_clients_module 禁用ngx_http_split_clients_module支持,该模块用于基于用户ip地址、报头、cookies划分用户。
--without-http_referer_module 禁用ngx_http_referer_modlue支持,该模块用来过滤请求,报头中Referer值不正确的请求。
--without-http_rewrite_module 禁用ngx_http_rewrite_module支持。该模块允许使用正则表达式改变URI,并且根据变量来转向以及选择配置。如果在server级别设置该选项,那么将在location之前生效,但如果location中还有更进一步的重写规则,location部分的规则依然会被执行。如果这个URI重写是因为location部分的规则造成的,那么location部分会再次被执行作为新的URI,这个循环会被执行10次,最后返回一个500错误。
--without-http_proxy_module 禁用ngx_http_proxy_module支持,http代理功能。
--without-http_fastcgi_module 禁用ngx_http_fastcgi_module支持,该模块允许nginx与fastcgi进程交互,并通过传递参数来控制fastcgi进程工作。
--without-http_uwsgi_module 禁用ngx_http_uwsgi_module支持,该模块用来使用uwsgi协议,uwsgi服务器相关。
--without-http_scgi_module 禁用ngx_http_scgi_module支持,类似于fastcgi,也是应用程序与http服务的接口标准。
--without-http_memcached_module 禁用ngx_http_memcached支持,用来提供简单的缓存,提高系统效率。
--without-http_limit_conn_module 禁用ngx_http_limit_conn_module支持,该模块可以根据条件进行会话的并发连接数进行限制。
--without-http_limit_req_module 禁用ngx_limit_req_module支持,该模块可以实现对于一个地址进行请求数量的限制。
--without-http_empty_gif_module 禁用ngx_http_empty_gif_module支持,该模块在内存中常驻了一个1*1的透明gif图像,可以被非常快速的调用。
--without-http_browser_module 禁用ngx_http_browser_mdoule支持,创建依赖于请求报头的值 。如果浏览器为modern,则$modern_browser等于modern_browser_value的值;如果浏览器为old,则$ancient_browser等于$ancient_browser_value指令分配的值;如果浏览器为MSIE,则$msie等于1。
--without-http_upstream_ip_hash_module 禁用ngx_http_upstream_ip_hash_module支持,该模块用于简单的负载均衡。
--with-http_perl_module 启用ngx_http_perl_module支持,它使nginx可以直接使用perl或通过ssi调用perl。
--with-perl_modules_path= 设定perl模块路径
--with-perl= 设定perl库文件路径
--http-log-path= 设定access log路径
--http-client-body-temp-path= 设定http客户端请求临时文件路径
--http-proxy-temp-path= 设定http代理临时文件路径
--http-fastcgi-temp-path= 设定http fastcgi临时文件路径
--http-uwsgi-temp-path= 设定http scgi临时文件路径
--http-scgi-temp-path= 设定http scgi临时文件路径
--without-http 禁用http server功能
--without-http-cache 禁用http cache功能
--with-mail 启用POP3、IMAP4、SMTP代理模块
--with-mail_ssl_module 启用ngx_mail_ssl_module支持
--without-mail_pop3_module 禁用pop3协议。
--without-mail_iamp_module 禁用iamp协议。
--without-mail_smtp_module 禁用smtp协议。
--with-google_perftools_module 启用ngx_google_perftools_mdoule支持,调试用,可以用来分析程序性能瓶颈。
--with-cpp_test_module 启用ngx_cpp_test_module支持。
--add-module= 指定外部模块路径,启用对外部模块的支持。
--with-cc= 指向C编译器路径。
--with-cpp= 指向C预处理路径。
--with-cc-opt= 设置C编译器参数,指定--with-cc-opt="-I /usr/lcal/include",如果使用select()函数,还需要同时指定文件描述符数量--with-cc-opt="-D FD_SETSIZE=2048"。 (PCRE库)
--with-ld-opt= 设置连接文件参数,需要指定--with-ld-opt="-L /usr/local/lib"。(PCRE库)
--with-cpu-opt= 指定编译的CPU类型,如pentium,pentiumpro,...amd64,ppc64...
--without-pcre 禁用pcre库。
--with-pcre 启用pcre库。
--with-pcre= 指向pcre库文件目录。
--with-pcre-opt= 在编译时为pcre库设置附加参数 。
--with-md5= 指向md5库文件目录。
--with-md5-opt= 编译时为md5库设置附加参数。
--with-md5-asm 使用md5汇编源。
--with-sha1= 指向sha1库文件目录。
--with-sha1-opt= 编译时为sha1库设置附加参数。
--with-sha1-asm 使用sha1汇编源。
--with-zlib= 指向zlib库文件目录。
--with-zlib-opt= 在编译时为zlib设置附加参数。
--with-zlib-asm= 为指定的CPU使用汇编源进行优化。
--with-libatomic 为原子内存的更新操作的实现提供一个架构。
--with-libatomic= 指向libatomic_ops的安装目录。
--with-openssl= 指向openssl安装目录。
--with-openssl-opt= 在编译时为openssl设置附加参数。
--with-debug 启用debug日志。
Nginx配置应用
HTTP/2
- 基础条件
- nginx >= 1.9.5,配置ngx_http_v2_module模块。
- 安装配置步骤
wget http://nginx.org/download/nginx-1.9.10.tar.gz
tar zxvf nginx-1.9.10.tar.gz
mv nginx-1.9.10 /home/app/
cd /home/app/nginx-1.9.10
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module
make && make install
- 配置nginx.conf
server {
listen 443 ssl http2 default_server;
server_name www.ysunlight.com;
#这是下载下来的nginx证书的crt文件路径,绝对或者相对路径都可以
ssl_certificate /etc/nginx/www.ysunlight.com.crt;
#和crt的规则一样
ssl_certificate_key /etc/nginx/www.ysunlight.com.key;
}
- 重新启动nginx
/usr/local/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件
/usr/local/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx
heartbeat
实现双机热备, 高可用集群系统 心跳监测、源接管
主备模式
通过修改Heartbeat的配置文件,可以指定哪台Heartbeat服务器作为主服务器,则另一台服务器自动成为热备服务器,然后在热备服务器上配置Heartbeat守护程序来监听来自主服务器的心跳消息。如果热备服务器在指定的时间内未监听到来自主服务器的心跳,就会启动故障转移程序,并取得主服务器上的相关资源服务的所有权,接替主服务器继续不间断的提供服务,从而达到资源及服务高可用性的目的。
主主模式
即两台服务器互为主备,这时它们之间会相互发送报文来告诉对方自己当前的状态,如果在指定的时间内未收到对方发送的心跳报文,那么,一方就会认为对方失效或者宕机了,这每个运行正常的主机就会启动自身的资源接管模块来接管运行在对方主机上的资源或者服务,继续为用户提供服务。
- 官网:http://www.linux-ha.org/wiki/Download
下载依赖库
yum install -y gcc gcc-c++ autoconf automake libnet libtool glib2-devel libxml2-devel bzip2-devel e2fsprogs-devel libxslt-devel libtool-ltdl-devel make wget docbook-dtds docbook-style-xsl
keepalived
资源
官网:https://www.keepalived.org/
环境
主服务器:49.235.198.78 备服务器:110.43.33.21 虚拟IP:106.13.47.239
简介与核心思想
单点故障 keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议。 1.管理LVS 2.对LVS集群节点检查 3.作为系统网络服务的高可用功能
安装配置
#下载安装
> yum install popt-devel #提前安装
> wget https://www.keepalived.org/software/keepalived-2.0.10.tar.gz
> tar -zxvf keepalived-2.0.10.tar.gz
> mv keepalived-2.0.10 /usr/local/src/
> cd /usr/local/src/keepalived-2.0.10
> ./configure
> make && make install
#基础生成路径
> /usr/local/etc/keepalived/keepalived.conf
> /usr/local/etc/sysconfig/keepalived
> /usr/local/sbin/keepalived
#yum安装-这里选择此类安装
> yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
> yum install -y keepalived
> systemctl start keepalived #启动keepalived
> systemctl enable keepalived #加入开机启动keepalived
> systemctl restart keepalived #重新启动keepalived
> systemctl stop keepalived #停止keepalived
> systemctl status keepalived #查看keepalived状态
#依次启动MASTER、BACKUP主备节点
> service keepalived start
>
#查看IP信息, 如果配置成功后启动,即会出现虚拟IP
> ip addr
check_nginx.sh
#!/bin/sh
if [ $(ps -C nginx --no-header | wc -l) -eq 0 ]; then
systecmctl start nginx
fi
sleep 2
#未运行, 则退出此主机上keepalived, 由其他keepalived接管
if [ $(ps -C nginx --no-header | wc -l) -eq 0 ]; then
systecmctl stop keepalived
fi
backup
#备节点
# 金山云
# /etc/keepalived/keepalived.conf
# put /Users/ysun/project/server/docment/nginx/keepalived/backup/keepalived.conf /etc/keepalived/keepalived.conf
# get
global_defs {
router_id LVS_V0001
}
vrrp_instance VI_CLUSTER {
state BACKUP
nopreempt
interface eth0
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
106.13.47.239
}
}
#虚拟服务器定义块, 配置HA时可不选
virtual_server 106.13.47.239 8099 {
delay_loop 6 #健康检查间隔,单位为秒
lb_algo rr #负载均衡调度算法,一般用wrr、rr、wlc
lb_kind NAT #负载均衡转发规则。一般包括DR,NAT,TUN 3种
persistence_timeout 50 #会话保持时间
rotocol TCP #转发协议, 有TCP和UDP两种
#真实服务器,包括IP和端口号
real_server 49.235.198.78 8090 {
weight 99 #权重,数值越大,权重越高
TCP_CHECK { #通过tcpcheck判断RealServer的健康状态
connect_timeout 3 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连时间间隔
connect_port 80 #检测端口
}
}
#真实服务器,包括IP和端口号
real_server 110.43.33.21 8090 {
weight 1 #权重,数值越大,权重越高
TCP_CHECK { #通过tcpcheck判断RealServer的健康状态
connect_timeout 3 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连时间间隔
connect_port 80 #检测端口
}
}
}
master
#主节点
# 腾讯云
# /etc/keepalived/keepalived.conf
# put /Users/ysun/project/server/docment/nginx/keepalived/master/keepalived.conf /etc/keepalived/keepalived.conf
# get
global_defs {
router_id LVS_V0002
}
vrrp_instance VI_CLUSTER {
state MASTER
nopreempt
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
106.13.47.239
}
}
#虚拟服务器定义块, 配置HA时可不选
virtual_server 106.13.47.239 8099 {
delay_loop 6 #健康检查间隔,单位为秒
lb_algo rr #负载均衡调度算法,一般用wrr、rr、wlc
lb_kind NAT #负载均衡转发规则。一般包括DR,NAT,TUN 3种
persistence_timeout 50 #会话保持时间
rotocol TCP #转发协议, 有TCP和UDP两种
#真实服务器,包括IP和端口号
real_server 49.235.198.78 8090 {
weight 99 #权重,数值越大,权重越高
TCP_CHECK { #通过tcpcheck判断RealServer的健康状态
connect_timeout 3 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连时间间隔
connect_port 80 #检测端口
}
}
#真实服务器,包括IP和端口号
real_server 110.43.33.21 8090 {
weight 1 #权重,数值越大,权重越高
TCP_CHECK { #通过tcpcheck判断RealServer的健康状态
connect_timeout 3 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连时间间隔
connect_port 80 #检测端口
}
}
}
#配置HA和负载均衡
#全局定义块
global_defs {
notification_email {
ysungod@163.com
}
router_id LVS_V0001 #每个节点保证不相同
}
#检测 nginx 运行状态的脚本
vrrp_script chknginx {
script "/home/keepalived/check_nginx.sh"
interval 10
}
#VRRP实例定义块, 如果keepalived只用来做HA, 则可以忽略VRRP实例定义块
vrrp_instance VI_CLUSTER {
state MASTER #角色是master, MASTER与BACKUP要大写
nopreempt #不抢占模式,必须配合BACKUP。在配置了该模式后优先级则失去作用。谁先启动谁就是MASTER,失败后必须杀死当前keepalived进程,再次重启不会立即抢占资源。实际场合中推荐使用该方式而不是使用优先级的抢占模式。抢占模式会出现一些数据同步的问题。
interface eth0 #vip 绑定端口
virtual_router_id 50 #让master 和backup在同一个虚拟路由里,id 号必须相同;
priority 100 #设置优先级,优先级高的会被竞选为Master,Master要高于BACKUP至少50
advert_int 1 #心跳间隔时间
authentication {
auth_type PASS #认证方式,支持PASS和AH,官方建议使用PASS
auth_pass 123456 #密码, 据说AH 使用时有问题
}
#虚拟ip,对外提供,客户端访问此ip即为访问 keepalived 中MASTER 所在主机
virtual_ipaddress {
106.13.47.239 #虚拟ip
}
#检测脚本, 关联vrrp_script chknginx
track_script {
chknginx
}
}
#虚拟服务器定义块, 配置HA时可不选
#这里配置主要是为了让一台realserver上的某个服务可以属于多个Virtual Server,并且只做一次健康检查:
virtual_server 106.13.47.239 443 {
delay_loop 6 #健康检查间隔,单位为秒
lb_algo rr #负载均衡调度算法,一般用wrr、rr、wlc
lb_kind NAT #负载均衡转发规则。一般包括DR,NAT,TUN 3种
persistence_timeout 50 #会话保持时间
rotocol TCP #转发协议, 有TCP和UDP两种
#真实服务器,包括IP和端口号
real_server 49.235.198.78 8090 {
weight 99 #权重,数值越大,权重越高
TCP_CHECK { #通过tcpcheck判断RealServer的健康状态
connect_timeout 3 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连时间间隔
connect_port 80 #检测端口
}
}
#真实服务器,包括IP和端口号
real_server 110.43.33.21 8090 {
weight 1 #权重,数值越大,权重越高
TCP_CHECK { #通过tcpcheck判断RealServer的健康状态
connect_timeout 3 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连时间间隔
connect_port 80 #检测端口
}
}
}
Nagios
Nagios是一款开源的免费网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设备,打印机等。在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知。
常见问题
- The 'Access-Control-Allow-Origin' header contains multiple values '', but only one is allowed.