在Apache服务器上禁用OPTIONS HTTP

5
Request:
OPTIONS / HTTP/1.1
Host: webcat.staci.com
Connection: Keep-alive
Accept-Encoding: gzip,deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.21 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.21
Accept: */*

Response:
HTTP/1.1 200 OK
Date: Thu, 01 Oct 2015 12:24:59 GMT
Server: Apache
X-Frame-Options: SAMEORIGIN
Allow: GET,HEAD,POST,OPTIONS,TRACE
Vary: Accept-Encoding,User-Agent
Content-Length: 0
Keep-Alive: timeout=7, max=95
Connection: Keep-Alive
Content-Type: httpd/unix-directory
Set-Cookie: BIGipServerwebcat-ssl=192938503.47873.0000; path=/; httponly; secure

我希望禁用Apache服务器上的HTTP OPTIONS,但我想保留GET、POST并ping我的服务器。
我该如何做?
我的httpd.conf文件:
RewriteEngine On
RewriteCond %{REQUEST_METHOD} !^ (GET,POST,HEAD)
RewriteRule .* – [R=405,L]

你到目前为止尝试了什么?在谷歌上输入“在Apache服务器上禁用OPTIONS HTTP”会返回https://sureshk37.wordpress.com/2014/10/01/how-to-disable-apache-http-options-method/。 - SubOptimal
1
亲爱的先生们,如果您要给我点踩,请至少留下一条小评论,告诉我如何改进我的回答。 - Mercer
你有关注我发的链接吗?你会发现Stefano也发布了同样的解决方案。看来答案并没有藏在互联网里。;-) - SubOptimal
@SubOptimal 我跟着链接走了,但他没有谈论 ping - Mercer
也许需要添加更多信息,例如你尝试了什么,你期望得到什么以及实际得到了什么。你所说的“想要PING你的服务器”是什么意思?类似于ping your.server.ip这样的吗? - SubOptimal
3个回答

12

无法使用RewriteCond禁用OPTIONS方法。 必须使用LimitExcept指令禁用。

以下是可以添加到Apache配置的范例:

<Location />
    <LimitExcept GET POST>
        order deny,allow
        deny from all
    </LimitExcept>
</Location>

请别忘记重新启动web服务器 :)


你的解决方案也禁用了 ping 吗? - Mercer
1
不,只有在使用“禁用HTTP方法重写规则”时才会禁用ping。 - Stefano Lonati
这在WordPress网站上不起作用。我在我的Apache配置中有推荐的配置,但是WordPress的.htaccess文件覆盖了它。请参见此处:https://stackoverflow.com/questions/66570860/apache-mod-rewrite-to-block-html-options-requests-with-wordpress - Claud

0
如果你想将它应用到特定的项目中:
只需将以下几行代码添加到一个名为.htaccess的文件中即可:
RewriteCond %{REQUEST_METHOD} ^(OPTIONS)
RewriteRule .* - [F]

为了使这个工作正常,确保你已经启用了mod_rewrite并且在这些行之前使用RewriteEngine On


这个建议是有效的,但不能与WordPress自动添加的指令一起使用。请参考此主题:https://stackoverflow.com/questions/66570860/apache-mod-rewrite-to-block-html-options-requests-with-wordpress - Claud

0

要在仍允许GETPOSTPING请求的情况下禁用您的Apache服务器上的HTTP OPTIONS方法,您可以在您的httpd.conf文件中使用以下配置:

RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^OPTIONS$
RewriteRule .* - [R=405,L]

<LocationMatch "/your_ping_endpoint">
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
</LocationMatch>

/your_ping_endpoint替换为您想要用于请求的实际端点。此配置将对任何请求返回<405>()响应,并拒绝从除(<127.0.0.1>)以外的任何地方访问 。
此外,请确保通过运行以下命令启用Apache中的重写模块:
sudo a2enmod rewrite

进行这些更改后,重新启动您的Apache服务器以使配置生效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接