Nginx-基础总结(下)

Nginx-基础总结(下)

罗小黑
2021-06-15 / 0 评论 / 252 阅读 / 正在检测是否收录...
广告
温馨提示:
本文最后更新于2021年06月15日,已超过1016天没有更新,若内容或图片失效,请留言反馈。

根据域名自定义跳转

if ( $host = 'www.baidu.com' ) {
  rewrite ^/(.*)$ http://baidu.com/$1 permanent;
}

浏览器的类型,作出相应的跳转

# 根据浏览器头部 URL 重写到指定目录
if ($http_user_agent ~* MSIE) {
  rewrite  ^(.*)$  /msie/$1  break;
}
# 判断是否是手机端
if ( $http_user_agent ~* "(Android|iPhone|Windows Phone|UC|Kindle)" ) {
  rewrite ^/(.*)$ http://m.qp.com$uri redirect;
}

禁止访问目录|文件

location ~* \.(txt|doc)${
    root /data/www/wwwroot/linuxtone/test;
    deny all;
}

如果前端是反向代理的情况下:

location /admin/ {
  allow 192.168.1.0/24;
  deny all;
}
# 后端
# set $allow false;
# if ($allow = false) { return 403;}
if ($http_x_forwarded_for !~* ^192\.168\.1\.*) {
  return 403;
}

添加模块–支持 Websock
Nginx 动态添加模块

版本平滑升级,和添加模块操作类似
准备模块
这里以 nginx-push-stream-module 为例,模块我放在 /data/module 下,你也可以放在其他位置

mkdir -p /data/module && cd /data/module/
git clone http://github.com/wandenberg/nginx-push-stream-module.git

查看 Nginx 已安装模块

/usr/local/nginx/sbin/nginx -V
--prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --with-pcre
备份源执行文件
备份原来的 nginx 可执行文件

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak

 有必要的话,可以再备份下配置文件,以防万一

下载源码编译
下载相同版本的 Nginx 源码包编译(以前安装时的源码包),如果已经删除了可重新下载,版本相同即可

wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar xf nginx-1.16.1.tar.gz
cd nginx-1.16.1
 ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --with-pcre --add-module=/data/module/nginx-push-stream-module

编译Nginx(千万不要make install,不然就真的覆盖了)

make
mv objs/nginx /usr/local/nginx/sbin/
查看是否安装
/usr/local/nginx/sbin/nginx -V
--prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --with-pcre --add-module=/data/module/nginx-push-stream-module
添加模块–支持健康检查模块

缺陷?

自带健康检查的缺陷:

Nginx 只有当有访问时后,才发起对后端节点探测。
如果本次请求中,节点正好出现故障,Nginx 依然将请求转交给故障的节点,然后再转交给健康的节点处理。所以不会影响到这次请求的正常进行。但是会影响效率,因为多了一次转发
自带模块无法做到预警
被动健康检查
使用第三访模块 nginx_upstream_check_module:

区别于 nginx 自带的非主动式的心跳检测,淘宝开发的 tengine 自带了一个提供主动式后端服务器心跳检测模块
若健康检查包类型为 http,在开启健康检查功能后,nginx 会根据设置的间隔向指定的后端服务器端口发送健康检查包,并根据期望的 HTTP 回复状态码来判断服务是否健康。
后端真实节点不可用,则请求不会转发到故障节点
故障节点恢复后,请求正常转发

准备模块

yum install patch git -y
cd /usr/local/src
git clone https://github.com/yaoweibin/nginx_upstream_check_module.git

打补丁

需进入源码包打补丁
个人习惯,源码放在 /usr/local/src
例如我的 nginx 源码包存放: /usr/local/src/nginx-1.16.1 , 若源码已经删除,那么去官网上再下载同版本

cd /usr/local/src/nginx-1.16.1
patch -p1 < ../nginx_upstream_check_module/check_1.16.1+.patch

重新编译

nginx -V
# configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx_upstream_check_module/
# 在运行中的 nginx 添加模块; 首先一点: 修改东西之前要先备份
mv /usr/loca/nginx/sbin/nginx{,_bak}
./configure --prefix=/usr/local/nginx \
--user=www --group=www \
--with-http_ssl_module \
--with-http_stub_status_module \
--add-module=../nginx_upstream_check_module
make
# **别手贱, 千万不要 make install**
cp objs/nginx /usr/local/nginx/sbin/
查看模块
nginx -V
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --add-module=../nginx_upstream_check_module

如何使用?

http {
    upstream cluster {
        server 192.168.0.1:80;
        server 192.168.0.2:80;
      server 127.0.0.1:80;
        check interval=5000 rise=1 fall=3 timeout=4000;
        #check interval=3000 rise=2 fall=5 timeout=1000 type=ssl_hello;
        #check interval=3000 rise=2 fall=5 timeout=1000 type=http;
        #check_http_send "HEAD / HTTP/1.0\r\n\r\n";
        #check_http_expect_alive http_2xx http_3xx;
    }
    server {
        listen 80;
        location / {
            proxy_pass http://cluster;
        }
        location /status {
            # 默认html,请求方式: check_status html|json|xml;
            # allow 允许的IP地址
            check_status;
            access_log   off;
            allow SOME.IP.ADD.RESS;
            deny all;
       }
    }
}

kill -USER2 cat /usr/local/nginx/logs/nginx.pid #热升级nginx,如果当前nginx不是用绝对路径下的nginx命令启动的话,热升级无效。
只能 nginx -s stop && /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf`
验证

curl http://127.0.0.1/status?format=http
curl http://127.0.0.1/status?format=xml
curl http://127.0.0.1/status?format=json
"""
{"servers": {
  "total": 3,
  "generation": 2,
  "server": [
    {"index": 0, "upstream": "cluster", "name": "192.168.0.1:80", "status": "down", "rise": 0, "fall": 432, "type": "tcp", "port": 0},
    {"index": 1, "upstream": "cluster", "name": "192.168.0.2:80", "status": "down", "rise": 0, "fall": 432, "type": "tcp", "port": 0},
    {"index": 2, "upstream": "cluster", "name": "127.0.0.1:80", "status": "up", "rise": 4, "fall": 0, "type": "tcp", "port": 0}
  ]
}}
"""

注意事项
如果后端是基于域名访问,可使用 check_http_send “GET /xxx HTTP/1.0rn HOST www.xxx.comrnrn”;方式在请求时添加请求头信息
参数详解

interval: 检测间隔 3 秒
fall: 连续检测失败次数 5 次时,认定 relaserver is down
rise: 连续检测成功 2 次时,认定 relaserver is up
timeout: 超时 1 秒
default_down: 初始状态为 down,只有检测通过后才为 up
type: 检测类型方式 tcp
tcp :tcp 套接字,不建议使用,后端业务未 100%启动完成,前端已经放开访问的情况
ssl_hello: 发送 hello 报文并接收 relaserver 返回的 hello 报文
http: 自定义发送一个请求,判断上游 relaserver 接收并处理
mysql: 连接到 mysql 服务器,判断上游 relaserver 是否还存在
ajp: 发送 AJP Cping 数据包,接收并解析 AJP Cpong 响应以诊断上游 relaserver 是否还存活(AJP tomcat 内置的一种协议)
fastcgi: php 程序是否存活

GIthub 地址
https://github.com/yaoweibin/nginx_upstream_check_module
添加模块–支持国家城市模块
安装依赖 libmaxmindd

因为需要读取在 GeoIP2 的 IP 数据库库,需要使用到 libmaxminddb 中的一个 C 库

pay源码

wget https://github.com/maxmind/libmaxminddb/releases/download/1.3.2/libmaxminddb-1.3.2.tar.gz
tar zxvf libmaxminddb-1.3.2.tar.gz
cd libmaxminddb-1.3.2
./configure
make
make  install

添加库路径并更新库

sh -c "echo /usr/local/lib  >> /etc/ld.so.conf.d/local.conf"
ldconfig
yum
yum install libmaxminddb-devel -y

下载 GeoIP 源码

wget https://github.com/leev/ngx_http_geoip2_module/archive/3.2.tar.gz
tar zxvf 3.2.tar.gz

Nginx 重新编译

 ./configure --prefix=/usr/local/nginx --add-module=../ngx_http_geoip2_module-3.2
make && make install

下载 GeoLite
这个库是为了将 IP 地址翻译成具体的地址信息,下载需要注册…
URL: https://www.maxmind.com/en/accounts/current/people/current 账号: xxxx@qq.com 密码: xxx..

gunzip GeoLite2-City.mmdb.gz
gunzip GeoLite2-Country.mmdb.gz
mkdir /data/geoip
mv GeoLite2-City.mmdb  /data/geoip/city.mmdb
mv GeoLite2-Country.mmdb /data/geoip/country.mmdb

启用 GeoIP

vim /usr/local/nginx/conf/nginx.conf
http {
    geoip2 /data/geoip/country.mmdb {
        $geoip2_data_country_code default=CN country iso_code;
        $geoip2_data_country_name country names en;
    }
    geoip2 /data/geoip/city.mmdb {
        $geoip2_data_city_name default=Shenzhen city names en;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            add_header geoip2_data_country_code $geoip2_data_country_code;
            add_header geoip2_data_city_name $geoip2_data_city_name;
            if ($geoip2_data_country_code = CN){
                root /data/webroot/cn;
            }
            if ($geoip2_data_country_code = US){
                root /data/webroot/us;
            }
        }
}

检查 GeoIP

mkdir /data/webroot/us
mkdir /data/webroot/cn
echo "US Site" > /data/webroot/us/index.html
echo "CN Site" > /data/webroot/cn/index.html
curl 试一试

内置变量

http://wiki.nginx.org/HttpCoreModule#Variables   官方文档
$arg_PARAMETER
$args
$binary_remote_addr
$body_bytes_sent
$content_length
$content_type
$cookie_COOKIE
$document_root
$document_uri
$host
$hostname
$http_HEADER
$sent_http_HEADER
$is_args
$limit_rate
$nginx_version
$query_string
$remote_addr
$remote_port
$remote_user
$request_filename
$request_body
0

打赏


评论 (0)

取消