如何使httpd响应200的选项请求?

刮y的

我想在centos + apache上进行一些配置,当客户端提出选项请求时,让httpd服务器发送200响应。

这里有一个很老的帖子(2011年)。

在HTTP OPTIONS请求中在Apache中返回“ 200 OK”

该配置可能不适用于当前的os和apache。

如果配置处于良好状态,则curl -X OPTIONS -i http://remote_ip/remote.html可能会获得200个返回码。

这是我的尝试:

1.猫.htaccess

AuthName "login"  
AuthType Basic  
AuthUserFile /var/www/html/passwd  
require user usernam
Options -Indexes
<LimitExcept OPTIONS>
  Require valid-user
</LimitExcept>

使用systemctl restart httpd.Error info命令重新启动它curl -X OPTIONS -i http://remote_ip/remote.html

<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>

在.htacccess中删除以上配置。

2.cat /etc/httpd/conf/httpd.conf。

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride AuthConfig
    Require all granted
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"
    Header always set Access-Control-Allow-Credentials "true"
    Header always set Access-Control-Allow-Headers "Authorization,DNT,User-Agent,Keep-Alive,Content-Type,accept,origin,X-Requested-With"
    RewriteEngine On                  
    RewriteCond %{REQUEST_METHOD} OPTIONS 
    RewriteRule ^(.*)$ blank.html [QSA,L]
</Directory>

使用systemctl restart httpd.Error info命令重新启动它curl -X OPTIONS -i http://remote_ip/remote.html

HTTP/1.1 401 Unauthorized
Date: Sat, 08 Sep 2018 00:34:36 GMT
Server: Apache/2.4.6 (CentOS)
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Authorization,DNT,User-Agent,Keep-Alive,Content-Type,accept,origin,X-Requested-With
WWW-Authenticate: Basic realm="login"
Content-Length: 381
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>
杂音

首先,您的.htaccess文件存在问题

  • 在第6-8行;您要求用户通过身份验证,但前提是该OPTIONS请求不是请求。这可以。
  • 但是,在第4行中,您需要将用户认证为user usernam,这取决于请求方法(GET,POST,OPTIONS等)。

因此,如果您删除第4行或将其移到该LimitExcept部分中,则您的配置应该可以正常工作。

有关更多信息,请参见mod_authz_core文档。


其次,您发布的第一个解决方案的错误消息(“服务器遇到内部错误或配置错误...”)提示httpd.conf文件无效可能还有其他配置错误。检查您的配置和Apache文档


作为参考,可以在以下位置找到我用于测试的配置文件:https : //github.com/mhutter/stackexchange/tree/master/467654

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章