我正在尝试在我的 Apache 2.4 服务器(在 Ubuntu 上)的 VirtualHost 上创建一个 RESTful API。我有一个名为 dbManager.php
的 php 文件,我使用 RewriteRule 使其看起来像一个 api
目录。除了返回 403 错误的 PUT 和 DELETE 命令外,它工作得很好。这是我的 conf 文件的编辑版本:
<VirtualHost *>
ServerAdmin onigame@gmail.com
ServerName servername.com
ServerAlias *.servername.com
DirectoryIndex index.html index.php
DocumentRoot /path/to/local/dir/
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
<Limit PUT DELETE>
Require all granted
</Limit>
</Directory>
# RESTful services provided for the fake "api" directory
RewriteEngine on
RewriteRule ^/api/(.*)$ /dbManager.php/$1 [L]
ServerSignature On
AddDefaultCharset utf-8
</VirtualHost>
好吧,PUT 和 DELETE 仍然没有工作并返回 403。我也担心我真的不想在目录的任何地方允许 PUT 和 DELETE,但只能通过虚拟 api
目录。执行此操作的正确方法是什么?
最佳答案
我已经设法解决了我的问题,但我真的不明白为什么它有效:
<VirtualHost *>
ServerAdmin onigame@gmail.com
ServerName servername.com
ServerAlias *.servername.com
DirectoryIndex index.html index.php
DocumentRoot /path/to/local/dir/
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
<Directory /path/to/local/dir/>
Require all granted
Satisfy All
</Directory>
# RESTful services provided for the fake "api" directory
RewriteEngine on
RewriteRule ^/api/(.*)$ /dbManager.php/$1 [L]
ServerSignature On
AddDefaultCharset utf-8
</VirtualHost>
我能弄清楚的是,获得 403 意味着它的访问被阻止,而不是 HTTP 请求的类型(这将导致 405,而不是 403)。而访问问题是在本地目录上,因此需要一个特殊的部分。但我真的不明白为什么我放在那里的两行能让事情奏效。 Require
指令,这是有道理的。但是 Satisfy
指令,据我从 documentation 中可以看出, 应默认为 All
。
然而,当我删除任何一行时,它都不起作用。
关于php - 使用 PHP 在 Apache 2.4 上配置 RESTful VirtualHost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29331339/