Nginx运营相关Ngix

Nginx安全防护汇总(转)

2021-04-04  本文已影响0人  迦叶_金色的人生_荣耀而又辉煌

上一篇 <<<前后端分离场景通过Nginx调试本地后端接口
下一篇 >>>OSI七层模型与层上协议

Nginx配置

参考地址:http://tengine.taobao.org/nginx_docs/cn/docs/

涉及模块:

1、控制单IP并发连接数

编辑配置文件nginx.conf,在添加如下内容:

limit_conn_zone $binary_remote_addr zone=addr:10m;
server
{
     listen   80;
     server_name www.baidu.com;
     index index.html index.htm index.jsp;
     root  /usr/local/www;
     #Zone limit;
     location / {
         limit_conn addr 3;
         limit_rate 20k;
     }
}

2、禁止目录浏览

编辑配置文件nginx.conf,在添加如下内容:

autoindex off;

3、限制目录执行权限(PHP)

编辑配置文件nginx.conf,在添加如下内容:

location ~ /(attachments|upload)/.*\.(php|php5)?$ {
    deny all;
}

4、错误页面重定向

编辑配置文件nginx.conf,在添加如下内容:

    proxy_intercept_errors on;
    error_page  404  /404.html;
    location = /404.html {
        root   /usr/local/nginx/html;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/nginx/html;
    }

5、隐藏Nginx版本信息

编辑配置文件nginx.conf,在添加如下内容:

server_tokens off;

6、限制HTTP请求方法

编辑配置文件nginx.conf,在添加如下内容:

if ($request_method !~ ^(GET|HEAD|POST)$ ) {
    return 404;
}

7、Nginx用户降权

编辑配置文件nginx.conf,在添加如下内容:

user nginx www;

8、自定义Nginx缓存

编辑配置文件nginx.conf,在添加如下内容:

proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传

9、过滤非法的UA

编辑配置文件nginx.conf,在添加如下内容:

if ($http_user_agent ~* "java|python|perl|ruby|curl|bash|echo|uname|base64|decode|md5sum|select|concat|httprequest|httpclient|nmap|scan" ) {
    return 403;
}

10、过滤不支持的URL

编辑配置文件nginx.conf,在添加如下内容:

location ~* \.(bak|save|sh|sql|mdb|svn|git|old)$ {
    rewrite ^/(.*)$  $host  permanent;
}

11、强制域名访问

编辑配置文件nginx.conf,在添加如下内容:

if ( $host !~* 'taobao.com' ) {
    return 403;
}

12、去掉无用的Nginx模块

在编译安装时,执行./configure方法时加上以下配置指令,可以显式的删除不用的模块:

./configure --without-http_dav_module --withouthttp_spdy_module

13、SSL配置

需要购买证书,否则浏览器将有风险提示

ssl_certificate /etc/nginx/sites-enabled/certs/tecmintlovesnginx.crt;
ssl_certificate_key /etc/nginx/sites-enabled/certs/tecmintlovesnginx.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_protocols TLSv1.2;
# 只支持 proxy 模式下设置,SameSite 不需要可删除,如果想更安全可以把 SameSite 设置为 Strict
proxy_cookie_path / "/; httponly; secure; SameSite=Lax";
return 301 https://$server_name$request_uri;

防火墙配置

强烈建议在网络防火墙或WAF中统一配置规则

1、开启SELinux

2、拒绝 icmp协议

禁止ping

iptables -A OUTPUT -p icmp --icmp-type echo-request -j DROP
iptables -A INPUT  -p icmp --icmp-type echo-request -j DROP

3、限制SSH连接IP

iptables -A INPUT -p tcp --dport 22 -s 10.14.0.0/16 -j ACCEPT  /*允许22端口数据进入*/

4、关闭不需要的端口

默认关闭所有input端口,只保留必要服务的端口

iptables -P INPUT DROP     /*丢弃所有进入机器的数据包*/
iptables -A INPUT -p tcp --dport 22 -s 10.14.0.0/16 -j ACCEPT  /*允许22端口数据进入*/

Nginx配置

1、下载最新版Tenginx、zlib

wget http://tengine.taobao.org/download/tengine-2.3.2.tar.gz
wget http://www.zlib.net/zlib-1.2.11.tar.gz

2、添加用户和用户组

groupadd -g 666 www

useradd -u 801 -g 666 -Mn -s /sbin/nologin nginx

3、编译并安装zlib

tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11

./configure
make && make install

4、编译并安装luajit

下载nginx专用版,否则有坑

yum -y install gcc
yum -y install gcc-c++

wget https://github.com/openresty/luajit2/archive/v2.1-20200102.tar.gz
tar -zxvf v2.1-20200102.tar.gz && cd luajit2-2.1-20200102
make && make install PREFIX=/usr/local/luajit
export LUAJIT_LIB=/usr/local/luajit/lib LUAJIT_INC=/usr/local/luajit/include/luajit-2.1

echo /usr/local/luajit/lib > /etc/ld.so.conf.d/usr_local_lib.conf
ldconfig

5、下载ngx_devel_kit和lua-nginx-module

wget https://github.com/loveshell/ngx_lua_waf/archive/master.zip

wget https://github.com/vision5/ngx_devel_kit/archive/v0.3.1.tar.gz
tar -xzvf v0.3.1.tar.gz

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.14.tar.gz
tar -xzf v0.10.14.tar.gz

6、编译Tenginx并安装

tar -zxvf tengine-2.3.2.tar.gz
cd tengine-2.3.2

./configure --prefix=/usr/local/nginx --pid-path=/usr/local/nginx/nginx.pid --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_ssl_module --user=nginx --group=www --add-module=/usr/local/nginx/modules/ngx_devel_kit-0.3.1 --add-module=/usr/local/nginx/modules/lua-nginx-module-0.10.14
make && make install

5、修改配置文件

user  nginx www;
worker_processes  auto;

error_log  /data/www/nginx/logs/error.log;
#error_log  logs/error.log  notice;
# error_log  logs/error.log  info;
#error_log  "pipe:rollback logs/error_log interval=1d baknum=7 maxsize=2G";

pid        /usr/local/nginx/nginx.pid;

events{
    use epoll;
    worker_connections 51200;
    multi_accept on;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    server_tokens off;

    server_names_hash_bucket_size 512;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;

    #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;
    # access_log  "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G"  main;
    
    add_header X-Content-Type-Options nosniff;
    #防止跨站脚本 Cross-site scripting (XSS)
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Frame-Options "SAMEORIGIN";
    #add_header Content-Security-Policy: "default-src self";
    add_header Content-Security-Policy "script-src * 'unsafe-inline' 'unsafe-eval'";
    #add_header Content-Security-Policy "content *";

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  60;

    tcp_nodelay on;
    autoindex off;

    #fastcgi_connect_timeout 300;
    #fastcgi_send_timeout 300;
    #fastcgi_read_timeout 300;
    #fastcgi_buffer_size 64k;
    #fastcgi_buffers 4 64k;
    #fastcgi_busy_buffers_size 128k;
    #fastcgi_temp_file_write_size 256k;
    #fastcgi_intercept_errors on;

    gzip  on;
    gzip_min_length  1k;
    # 设置压缩所需要的缓冲区大小
    gzip_buffers     4 16k;
    # 设置gzip压缩针对的HTTP协议版本
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
    gzip_vary on;
    gzip_proxied   expired no-cache no-store private auth;
    # IE6以下浏览器不压缩
    gzip_disable   "MSIE [1-6]\.";


    error_page   403  /403.html;
    error_page   404  /404.html;
    error_page   500 502 503 504  /50x.html;
    
     server {
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        server_name  b2cfl.ispacechina.com;
        root         /usr/share/nginx/html;

        client_max_body_size 20m;

        ssl_certificate "/etc/nginx/cert/htsl_jks.pem";
        ssl_certificate_key "/etc/nginx/cert/htsl_jks.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        #ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256::!MD5;
        #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_protocols TLSv1.2;

        #ssl_stapling on;
        #ssl_stapling_verify on; # Requires nginx => 1.3.7
        #ssl_prefer_server_ciphers on;
        #ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
        #ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0
        #ssl_session_cache shared:SSL:1m;
        #ssl_session_tickets off; # Requires nginx >= 1.5.9
        #ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
        add_header Strict-Transport-Security "max-age=80720000; preload";
        add_header X-Content-Type-Options nosniff;
        #防止跨站脚本 Cross-site scripting (XSS)
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Frame-Options "SAMEORIGIN";
        #add_header Content-Security-Policy: "default-src self";
        #add_header Content-Security-Policy "content *";
        add_header Content-Security-Policy "script-src * 'unsafe-inline' 'unsafe-eval'";

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

#        location / {
#               proxy_pass http://127.0.0.1:9001;
#               proxy_http_version 1.1;
#                       proxy_redirect off;
#               proxy_set_header Upgrade $http_upgrade;
#               proxy_set_header Connection keep-alive;
#               proxy_set_header Host $host;
#               proxy_cache_bypass $http_upgrade;
#               proxy_set_header X-Real-IP $remote_addr;
#               proxy_set_header X-Forwarded-Proto  $scheme;
#       }
    } 
    include /data/www/nginx/conf/*.conf;
}



配置iptables

iptables -A OUTPUT -p icmp --icmp-type echo-request -j DROP
iptables -A INPUT  -p icmp --icmp-type echo-request -j DROP

推荐阅读:
<<<DNS域名解析过程
<<<初识Nginx与负载均衡
<<<Nginx使用场景之静态服务器
<<<Nginx使用场景之虚拟主机(动静分离)
<<<Nginx使用场景之反向代理
<<<Nginx使用场景之负载均衡和故障转移(主从)
<<<Nginx使用场景之跨域解决
<<<Nginx使用场景之资源压缩
<<<Nginx使用场景之数据缓存
<<<前后端分离场景通过Nginx调试本地后端接口
<<<Nginx Location指令详解
<<<Nginx全局变量
<<<Nginx导致图片下载失败的原因分析
<<<Nginx+Consul+upSync实现动态负载均衡
<<<Nginx+Lvs+keepAlived实现高可用
<<<Linux环境安装Nginx
<<<Linux环境安装keepAlived
<<<Keepalived虚拟vip功能

上一篇下一篇

猜你喜欢

热点阅读