Centos
如何為選項請求製作 httpd 響應 200?
我想在我的 centos+apache 上進行一些配置,讓 httpd 伺服器在客戶端發出選項請求時發送 200 響應。
這裡有一篇很老的文章(2011)。
在 Apache 中對 HTTP OPTIONS 請求返回“200 OK”
該配置可能不適合目前的作業系統和 apache。
如果配置狀態良好,
curl -X OPTIONS -i http://remote_ip/remote.html
可能會得到 200 返回碼。這是我的嘗試:
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
使用命令的 .Error 資訊重新啟動它: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 資訊重新啟動它: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