オプションリクエストに対してhttpdが200で応答するようにする方法は?

オプションリクエストに対してhttpdが200で応答するようにする方法は?

クライアントがオプション要求を行うと、httpdサーバーが200応答を送信するようにcentos + apacheでいくつかの構成を実行したいと思います。

ここに非常に古い投稿があります(2011)。

Apache では、HTTP OPTIONS リクエストに対して「200 OK」を返します。

この設定は、現在のオペレーティングシステムやApacheには適していない可能性があります。

構成状態が良好であれば、curl -X OPTIONS -i http://remote_ip/remote.html200 の戻りコードを受け取ることができます。

私の試みは次のとおりです。

1.cat.htaccess

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

systemctl restart httpd次のコマンドのエラーメッセージで再起動してください。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>

.htaccessから上記の設定を削除します。

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次のコマンドのエラーメッセージで再起動してください。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>

ベストアンサー1

まず、ファイルに問題があります.htaccess

  • 行6-8:ユーザー認証を要求しますが、これは要求ではない場合にのみ適用されますOPTIONS。大丈夫です。
  • usernamただし、4行目では、要求方法(GET、POST、OPTIONSなど)に関係なく、ユーザーをユーザーとして認証する必要があります。

LimitExceptしたがって、4行を削除するか、構成セクションに移動すると正しく機能します。

詳細については、次を参照してください。mod_authz_core ドキュメント


次に、最初に公開したソリューションのエラーメッセージ(「サーバーで内部エラーまたは設定エラーが発生しました...」)は、ファイルが無効であることを意味しますhttpd.conf。他の誤った設定がある可能性があります。構成を確認してApacheドキュメント


ちなみに、テストに使用した設定ファイルは以下にあります。https://github.com/mhutter/stackexchange/tree/master/467654

おすすめ記事