【问题标题】:Configuring a RESTful VirtualHost on Apache 2.4 with PHP使用 PHP 在 Apache 2.4 上配置 RESTful VirtualHost
【发布时间】:2015-06-02 14:10:37
【问题描述】:

我正在尝试在我的 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 目录。这样做的正确方法是什么?

【问题讨论】:

    标签: php apache rest http mod-rewrite


    【解决方案1】:

    我已经设法解决了我的问题,但我真的不明白为什么它有效:

    <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

    然而,当我删除任何一行时,它都不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-11
      • 2017-11-26
      • 2018-07-24
      • 2012-03-30
      • 2016-10-02
      • 2016-05-18
      • 1970-01-01
      • 2020-06-19
      相关资源
      最近更新 更多