实用 nginx.conf 用法大全

服务器拒绝非GET方式请求保障安全性,因为 DELETE、POST、PUT 是可以修改数据的。

Nginx 解决方案

在 nginx.conf 配置文件的网站配置区域中添加如下代码片段:

非 GET 类型请求,则返回 403 状态码。

if ($request_method !~* GET) {
    return 403;
}

非 GET 或 POST 类型请求,则返回 403 状态码。

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

或另一种写法:

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

Apache 解决方案

在站点配置文件 .htaccess 中添加如下代码片段:

非 GET 或 POST 类型请求,则返回 403 状态码。

<Location />
    <LimitExcept GET POST>
        Order Allow,Deny
        Deny from all
    </LimitExcept>
</Location>

IIS 解决方案

在站点 web.config 配置文件的 节点下添加如下代码:

只允许开启 GET、POST、HEAD 请求方法

<system.webServer>
    <security>
        <requestFiltering>
            <verbs allowUnlisted="false">
                <add verb="GET" allowed="true"/>
                <add verb="POST" allowed="true"/>
                <add verb="HEAD" allowed="true"/>
            </verbs>
        </requestFiltering>
    </security>
</system.webServer>
posted @ 2020-04-27 15:55  陈旭华  阅读(414)  评论(0编辑  收藏  举报