nginx 禁用不安全的http方法

从安全防护角度考虑,一般我们要禁用不安全的 HTTP 方法,仅保留 GET、POST 方法。

 

nginx 禁用不安全的http方法,既可以在nginx配置文件 server 下进行全局设置,也可以在某个location下进行设置。

全局设置方式一

        if ($request_method ~ ^(PUT|DELETE)$) {
            return 403;
        }

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

比如:

server {
        listen       80;
        server_name  www.iwen.com;
        #return 301 https://$server_name$request_uri;

        if ($request_method !~ ^(GET|POST)$) {
            return 403;
        }
        .......
        .......
}

局部设置方式一

location /knowlege_app {
        include /usr/local/nginx/allow_ip_list.conf;
        if ($request_method = PUT ) {
                return 403;
        }

        if ($request_method = DELETE ) {
                return 403;
        }
        if ($request_method = OPTIONS ) {
                return 403;
        }
        if ($request_method = TRACE ) {
                return 403;
        }
        proxy_pass http://serverKnowlege_app;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

局部设置方式二:

location /knowlege_app {
        include /usr/local/nginx/allow_ip_list.conf;
        if ($request_method !~ ^(GET|POST)$) {
                return 403;
        }

        proxy_pass http://serverKnowlege_app;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wudinaniya

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值