安全

通过IP地址限制访问
http设置IP地址限制访问

既可放在server下也可放在location下,如下:

server {
    allow 192.168.0.0/16;
    deny all;
}
---------------------------
location /api {
    deny  192.168.1.2;
    allow 192.168.1.1/24;
    allow 127.0.0.1;
    deny  all;
}
与LDAP集合认证

因为nginx自带的基本认证太简单了,不方便集成外部的认证源,例如ldap认证。所以我们需要用到auth_request模块去集成外部认证,注意yum安装的nginx默认是没有编译 auth_request 模块,这里我们需要重新编译一下。

  • 下载nginx-auth-ldap,执行命令cd ~ && git clone https://github.com/kvspb/nginx-auth-ldap.git

  • 查看现有nginx版本与编译的参数

    [root@test11 nginx-1.17.2]# nginx -V
    nginx version: nginx/1.17.2
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
    built with OpenSSL 1.0.2k-fips  26 Jan 2017
    TLS SNI support enabled
    configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
  • 下载相同的版本nginx

    wget http://nginx.org/download/nginx-1.17.2.tar.gz
    tar -zxvf nginx-1.17.2.tar.gz
  • 备份原有nginx和配置

    cp /usr/sbin/nginx /tmp/nginx.back
    cp -r /etc/nginx/ /tmp/nginx.conf.back
  • 安装编译的依赖包

    yum install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data gperftools redhat-rpm-config
    yum install openldap-devel -y          #因为这里我示例添加第三方模块nginx-auth-ldap,需要安装openldap-devel
  • 编译nginx,添加模块,不管是第三方模块,还是自身模块,都直接在最后添加参数即可(填写模块所在路径)!

    ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/root/nginx-auth-ldap
  • 编译,注意:这里很多说不能make install,只make就好了,其实不然!在执行./configure命令时则已经查出了原有nginx的各个文件的路径,如果原来没有nginx,则会默认安装到/usr/local/nginx目录下。直接执行make install 则会自动替换原来的nginx,无须手动,也不会中断nginx进程。make && make install

  • 查看nginx参数nginx -V,并重启nginx:systemctl restart nginx

  • 配置ldap server,这里跟公司现有的AD认证集合

    [root@test11 vhost]# cat /etc/nginx/conf.d/vhost/00_ldap.conf 
    ldap_server ad {
    url "ldap://192.168.22.22:389/OU=test,DC=test,DC=ad?sAMAccountName?sub?(objectClass=person)";
    binddn "CN=admin,OU=test,DC=test,DC=ad";
    binddn_passwd "admin-password";
    group_attribute member;
    group_attribute_is_dn on;
    satisfy any;
    require_group "CN=wifi,OU=Others,OU=Groups,OU=test,DC=test,DC=ad";
    require_user valid_user;
    #require_user "CN=admin,OU=test,DC=test,DC=ad";
    
    auth_ldap_cache_enabled on;
    auth_ldap_cache_expiration_time 10000;
    auth_ldap_cache_size 1000;
    }
  • 在需要配置ldap认证的server或location上配置认证,如下例样:

    server {
      listen 80;
      server_name backend.example.com;
      location / {
          auth_ldap "LDAP Authentication";
          auth_ldap_servers ad;
          autoindex on;
          #proxy_max_temp_file_size 0;
          fastcgi_buffer_size 4k;
          fastcgi_buffers 2048 512k;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header REMOTE-HOST $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_pass http://backend;
      }

监控

nginx的监控可以从下面几方面考虑:

监控nginx是否存活

思路:可以从nginx进程和curl nginx返回的状态码上判断

首先:在nginx主配置文件上开启status模块:

server
    {
        location /status {
            stub_status on;
        }
    }

获取nginx的状态码:curl -I -m 5 -s -w "%{http_code}\n" -o /dev/null 127.0.0.1,nginx正常的一般状态码为200;

获取nginx的进程数:/sbin/pidof nginx|wc -w,正常来说会有一个守护进程和至少一个工作进程,这里可以判断进程数大于或等于2是正常的;

所以可以写一个脚本从上面两方面去判断nginx是否存活,最好是两个条件都考虑上!

服务损耗

获取nginx的7种状态,如下:

[root@test11 backend]# /bin/curl -s 127.0.0.1/status
Active connections: 176 
server accepts handled requests
 11839270 11839270 9522848 
Reading: 0 Writing: 20 Waiting: 155

分别解释一下这7种状态:

  • Active connections:当前与http建立的连接数
  • accepts:总共处理了多少个连接
  • handled:成功创建多少次握手
  • requests:总共处理了多少个请求
  • Reading:正在读取的客户连接数
  • Writing:处理响应数据到客户端的数量
  • Waiting:Nginx等待下次请求的驻留的客户连接数

可以awk和sed把这7种状态截取出来,再放到zabbix或其它监控工具上监控,下面是脚本的一部分,仅供参考:

# 检测nginx性能
function active {
     /bin/curl -s 127.0.0.1/status 2>/dev/null | grep 'Active' | awk '{print $NF}'
}
function reading {
     /bin/curl -s 127.0.0.1/status 2>/dev/null | grep 'Reading' | awk '{print $2}'
}
function writing {
     /bin/curl -s http://$HOST:$PORT/status 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
     /bin/curl -s http://$HOST:$PORT/status 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
     /bin/curl -s http://$HOST:$PORT/status 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
     /bin/curl -s http://$HOST:$PORT/status 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
     /bin/curl -s http://$HOST:$PORT/status 2>/dev/null| awk NR==3 | awk '{print $3}'
}
日志监控

从nginx的access.log和error.log下还可以监控到nginx的以下指标:

  • 请求时长:从发出请求到结束需要的时间$request_time 和 $upstream_response_time
  • 请求返回错误:从$status上获取,服务器日志方式错误码4xx和5xx等
  • 流量:pv可以从$remote_addr来统计

设置nginx的日志格式,如下:

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" $upstream_addr $request_
time $upstream_response_time $scheme';

nginx的日志监控可以集合到elk上监控,更加实时,明确!

文档更新时间: 2019-07-31 11:51   作者:子木