轨道 - 没有匹配的路线 [GET] "/index.html"(适用于所有路线)



我第一次尝试将我的 Rails 应用程序部署到暂存环境。我很确定这是一个 Apache/乘客问题,但我不确定在哪里寻求修复它。我可能只需要在我的 conf 文件中添加一个规则,但我不知道该规则是什么。也许是某种重写规则,因为它似乎在寻找文件而不是解析路由?

问题是:对于每个路由,它似乎都在"幕后"将其转换为"/index.html" - 无论我尝试"/api/v1"还是"/api/v1/users"或"/api/v1/channels/authorize"(或任何其他(。

apache2/access.log 文件似乎显示了正确传递的路由:

[07/Dec/2019:18:23:15 +0000] "GET /api/v1/channels/authorize HTTP/1.1" 500 41024 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"

并且 apache2/error.log 文件没有显示任何错误。


这是我的 conf 文件。我使用别名在两个单独的应用程序之间进行解析(/api/* 转到 Rails 后端,其他所有内容都转到 VueJS 前端 - 我知道我可以将客户端嵌入到 Rails 公共目录中,但我这样做是出于我自己的原因(。

<VirtualHost *:443>
ServerName <my url>
DocumentRoot /path/to/client
# configure error logs
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# Passenger config stuff
PassengerEnabled off
PassengerRuby /path/to/ruby
PassengerAppEnv development
PassengerStartTimeout 400

# if the route matches https://<domain>/api/*
# then point to the Rails API app
# (and use Passenger to serve it)
Alias /api /path/to/railsapp/public
# configuration for the /api routes
# basically just enable Passenger and tell it where to point to
<Location /api>
PassengerEnabled on
PassengerBaseURI /
PassengerAppRoot /path/to/railsapp
</Location>
<Directory /path/to/railsapp/public>
Allow from all
Options -MultiViews
Require all granted
#       RailsEnv test
</Directory>
# for EVERYTHING ELSE, point to the VueJS client app
<Location />
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.html [L]
</Location>
# and of course enable SSL and point to the certs
SSLEngine on
SSLCertificateKeyFile /path/to/key
SSLCertificateFile /path/to/cert
SSLCertificateChainFile /path/to/chain-file
</VirtualHost>

这是我第一次和乘客做任何事情。我试图从网上找到的例子中拼凑出一些东西,但很可能我在此过程中错过了一些东西。

所以我已经想通了。以下块中的RewriteRule将应用于所有内容:

<Location / >
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.html [L]
</Location>

(Vue 中的每个路由都会被重写回 index.html 文件 - 但 Rails 不会这样做(。

因此,我尝试了一些不同工作的方法,最终确定我可以简单地向规则集添加一个RewriteCond以排除对api/*的所有调用

所以新块是:

<Location / >
RewriteEngine on
RewriteCond %{REQUEST_URI} !/api/*        ## THIS IS THE LINE TO ADD!
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.html [L]
</Location>

我必须添加到 apache conf 文件中才能使其完美运行。

尝试将<Location /api>下的PassengerBaseURI /更改为PassengerBaseURI /api

最新更新