Nginx 不适用于 Sinatra JSON 路由

Nginx doesn't work with Sinatra JSON route

我已经使用 Nginx 1.12.1 设置了 Ubuntu 16.04 服务器,并安装和配置了 Phusion Passenger 5.1.8(开源)以正常工作。

我已经使用 passenger-ruby-sinatra-demo 存储库对此进行了测试。

我已将 root var 设置为 /etc/nginx/sites-available/default 文件中 repo 的 /views 文件夹:

root /var/www/passenger-ruby-sinatra-demo/views;

/views 文件夹包含一个名为 index.erb.

的文件

app.rb 文件的内容如下:

require 'sinatra/base'
require 'json'

class ExampleApp < Sinatra::Base
  get '/' do
    erb :index
  end

  get '/hello' do
    content_type 'application/json'

    {'message'=>'hello world!'}.to_json
  end
end

所以现在在访问 / 时我会看到 html 页面。但是在访问 /hello 时我得到一个 404 屏幕。

我做错了什么?

更新: 当我将一个名为 hello 的空文件夹添加到 views 文件夹时,它确实有效。

通过将以下内容添加到我的 /etc/nginx/sites-available/default 配置文件(在 server 块内)解决了这个问题:

...

location / {
    try_files $uri @app;
}

location @app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
}

...

我删除了:

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
}

这是一个与上面删除的代码相关的 Nginx 配置问题。