Nginx 可以通过添加以下配置来禁用 OPTIONS HTTP 方法:

if ($request_method = OPTIONS) {
    return 405;
}

这将检查请求方法是否为 OPTIONS,如果是,将返回 405 状态码,表示该方法不被允许。

另外,可以使用以下配置来允许 OPTIONS 方法,但限制它只能由特定的来源访问:

if ($request_method = OPTIONS) {
    add_header 'Access-Control-Allow-Origin' 'http://example.com';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    add_header 'Content-Length' 0;
    return 204;
}

这将在响应头中添加一些允许的选项,例如 Access-Control-Allow-Origin,Access-Control-Allow-Methods,Access-Control-Allow-Headers 等。这将允许特定的来源访问 OPTIONS 方法。

Nginx 配置禁用或限制 OPTIONS HTTP 方法

原文地址: https://www.cveoy.top/t/topic/n6W0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录