简体   繁体   English

Apache DirectoryIndex指令无法与Ruby on Rails一起正常使用

[英]Apache DirectoryIndex directive not working correctly with Ruby on Rails

I have a site where Ruby on Rails is running at the root directory via Phusion Passenger. 我有一个站点,其中Ruby on Rails通过Phusion Passenger在根目录中运行。 Some Apache Alias directives serve static HTML directories. 一些Apache Alias指令可提供静态HTML目录。

If you request an exact URL of a file from one of the static directories, Apache returns it as expected. 如果您从一个静态目录中请求文件的确切URL,则Apache将按预期方式将其返回。 However, there are a few cases where I'm not getting the expected behavior. 但是,在某些情况下我没有得到预期的行为。 Consider the alias Alias /functional /path/to/functional , where the functional directory contains index.php and hello.jpg . 考虑别名Alias /functional /path/to/functional ,其中功能目录包含index.phphello.jpg

  1. A request for https://example.com/functional/hello.jpg returns the JPEG file as expected. https://example.com/functional/hello.jpg的请求将按预期返回JPEG文件。
  2. A request for https://example.com/functional/index.php runs the PHP script and prints the resulting HTML document as expected. https://example.com/functional/index.php的请求将运行PHP脚本并按预期打印结果HTML文档。
  3. A request for https://example.com/functional/ results in a 404 in the Rails stack . 请求https://example.com/functional/会在Rails堆栈中产生404。
  4. A request for https://example.com/functional (without the trailing slash) also results in a 404 in the Rails stack. https://example.com/functional的请求(不带斜杠)也会在Rails堆栈中产生404。 Ideally this URL should return a 301 redirecting the user to http://example.com/functional/ 理想情况下,此URL应该返回301,将用户重定向到http://example.com/functional/

If I add an index.html file into the functional directory (in addition to the index.php that is already there), paths #3 and #4 return that file as expected. 如果我将index.html文件添加到功能目录(除了已经存在的index.php之外),路径3和4将按预期返回该文件。

Here's the relevant parts from my Apache configuration file for Passenger: 这是我的用于乘客的Apache配置文件中的相关部分:

LoadModule passenger_module /path/to/mod_passenger.so
LoadModule ssl_module modules/mod_ssl.so

<IfModule mod_passenger.c>
    # ... passenger setup stuff omitted ...

    Listen 443
    <VirtualHost *:443>
        ServerName ...
        DocumentRoot /path/to/rails/public
        PassengerRuby /path/to/ruby
        <Directory /path/to/rails/public>
            Allow from all
            Options -MultiViews
        </Directory>

        #  ... ssl setup omitted ...

    </VirtualHost>
</IfModule>

And for the static directories: 对于静态目录:

Alias /functional /path/to/functional/
Alias /phpMyAdmin /path/to/phpMyAdmin/
# ... other aliases omitted ...

<Directory /path/to/functional>
    DirectoryIndex index.php
    AllowOverride all
</Directory>

I've tried most of the answers in index.php not loading by default to make index.php load, to no prevail. 我已经尝试了index.php中的大多数答案, 默认情况下它们不会加载以使index.php加载,但没有成功。 I shouldn't even need to specify DirectoryIndex index.php since that line is already in conf.d/php.conf . 我什至不需要指定DirectoryIndex index.php因为该行已经在conf.d / php.conf中

Any ideas on how to solve this issue where Rails is giving 404s on URLs which are supposed to be going elsewhere in the Apache stack? 关于如何解决这个问题的任何想法,其中Rails在URL上提供了404,这些URL应该在Apache堆栈中的其他地方去了?

Have you tried replacing 您是否尝试过更换

<Directory /path/to/functional>

With

<Directory /functional>

I had the same problem. 我有同样的问题。 The working solution was just a RedirectMatch statement in the virtualhost serving passenger as follows: 可行的解决方案只是在为乘客提供服务的virtualhost中的RedirectMatch语句,如下所示:

RedirectMatch  ^/path/to/functional(/)?$  /path/to/functional/index.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM