使用Nginx-安全防護(hù)匯總

Nginx配置
涉及模塊:
ngx_http_core_module
ngx_http_limit_conn_module
ngx_http_proxy_module
ngx_http_ssl_module
ngx_http_upstream_module
ngx_http_rewrite_module
ngx_http_referer_module
ngx_http_headers_module
ngx_http_log_module
nginx_cookie_flag_module

1锅铅、控制單IP并發(fā)連接數(shù)
編輯配置文件nginx.conf,在添加如下內(nèi)容:

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摇幻,在添加如下內(nèi)容:

autoindex off;

3、限制目錄執(zhí)行權(quán)限(PHP)
編輯配置文件nginx.conf顷锰,在添加如下內(nèi)容:

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

4柬赐、錯(cuò)誤頁面重定向
編輯配置文件nginx.conf,在添加如下內(nèi)容:

    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肛宋,在添加如下內(nèi)容:

server_tokens off;

6、限制HTTP請(qǐng)求方法
編輯配置文件nginx.conf束世,在添加如下內(nèi)容:

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

7酝陈、Nginx用戶降權(quán)
編輯配置文件nginx.conf,在添加如下內(nèi)容:

user nginx www;

8毁涉、自定義Nginx緩存
編輯配置文件nginx.conf沉帮,在添加如下內(nèi)容:

proxy_buffer_size 4k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小
proxy_buffers 4 32k; #proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的設(shè)置
proxy_busy_buffers_size 64k; #高負(fù)荷下緩沖大衅堆摺(proxy_buffers*2)
proxy_temp_file_write_size 64k; #設(shè)定緩存文件夾大小穆壕,大于這個(gè)值,將從upstream服務(wù)器傳

9其屏、過濾非法的UA
編輯配置文件nginx.conf喇勋,在添加如下內(nèi)容:

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偎行,在添加如下內(nèi)容:

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

11川背、強(qiáng)制域名訪問
編輯配置文件nginx.conf,在添加如下內(nèi)容:

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

12蛤袒、去掉無用的Nginx模塊
在編譯安裝時(shí)熄云,執(zhí)行./configure方法時(shí)加上以下配置指令,可以顯式的刪除不用的模塊:

./configure --without-http_dav_module --withouthttp_spdy_module

13妙真、SSL配置
需要購(gòu)買證書皱碘,否則瀏覽器將有風(fēng)險(xiǎn)提示
開啟HTTPS服務(wù)

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 打開TLS
需要購(gòu)買證書,否則瀏覽器將有風(fēng)險(xiǎn)提示

ssl_protocols TLSv1.2;

Cookie 設(shè)置 Secure
在location中設(shè)置

# 只支持 proxy 模式下設(shè)置隐孽,SameSite 不需要可刪除癌椿,如果想更安全可以把 SameSite 設(shè)置為 Strict
proxy_cookie_path / "/; httponly; secure; SameSite=Lax";

重定向到HTTPS
需要購(gòu)買證書健蕊,否則瀏覽器將有風(fēng)險(xiǎn)提示

return 301 https://$server_name$request_uri;

防火墻配置
強(qiáng)烈建議在網(wǎng)絡(luò)防火墻或WAF中統(tǒng)一配置規(guī)則

1、開啟SELinux
2踢俄、拒絕 icmp協(xié)議
禁止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端口數(shù)據(jù)進(jìn)入*/

4、關(guān)閉不需要的端口
默認(rèn)關(guān)閉所有input端口都办,只保留必要服務(wù)的端口

iptables -P INPUT DROP     /*丟棄所有進(jìn)入機(jī)器的數(shù)據(jù)包*/
iptables -A INPUT -p tcp --dport 22 -s 10.14.0.0/16 -j ACCEPT  /*允許22端口數(shù)據(jù)進(jìn)入*/

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及皂、修改配置文件
/usr/local/nginx/conf/nginx.conf

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;
    # 設(shè)置壓縮所需要的緩沖區(qū)大小
    gzip_buffers     4 16k;
    # 設(shè)置gzip壓縮針對(duì)的HTTP協(xié)議版本
    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;
}

/data/www/nginx/conf/icasc_cloud_server_8080.conf
/data/www/nginx/conf/icasc_cloud_vue_80.conf
配置iptables
禁止ping

iptables -A OUTPUT -p icmp --icmp-type echo-request -j DROP
iptables -A INPUT  -p icmp --icmp-type echo-request -j DROP
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末甫男,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子验烧,更是在濱河造成了極大的恐慌板驳,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件碍拆,死亡現(xiàn)場(chǎng)離奇詭異若治,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)感混,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門直砂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人浩习,你說我怎么就攤上這事〖们穑” “怎么了谱秽?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)摹迷。 經(jīng)常有香客問我疟赊,道長(zhǎng),這世上最難降的妖魔是什么峡碉? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任近哟,我火速辦了婚禮,結(jié)果婚禮上鲫寄,老公的妹妹穿的比我還像新娘吉执。我一直安慰自己疯淫,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布戳玫。 她就那樣靜靜地躺著熙掺,像睡著了一般。 火紅的嫁衣襯著肌膚如雪咕宿。 梳的紋絲不亂的頭發(fā)上币绩,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天,我揣著相機(jī)與錄音府阀,去河邊找鬼缆镣。 笑死,一個(gè)胖子當(dāng)著我的面吹牛试浙,可吹牛的內(nèi)容都是我干的董瞻。 我是一名探鬼主播,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼川队,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼力细!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起固额,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤眠蚂,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后斗躏,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體逝慧,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年啄糙,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了笛臣。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡隧饼,死狀恐怖沈堡,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情燕雁,我是刑警寧澤诞丽,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站拐格,受9級(jí)特大地震影響僧免,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜捏浊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一懂衩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦浊洞、人聲如沸牵敷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽劣领。三九已至,卻和暖如春铁材,著一層夾襖步出監(jiān)牢的瞬間尖淘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工著觉, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留村生,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓饼丘,卻偏偏與公主長(zhǎng)得像趁桃,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子肄鸽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容