2017-08-26 62 views
0

我已經使用Nginx 1.12.1設置了Ubuntu 16.04服務器並且安裝並配置了Phusion Passenger 5.1.8(開源)以正常工作。Nginx不能使用Sinatra JSON路由

我已經用passenger-ruby-sinatra-demo回購測試了這個。

我已經設置了root VAR到回購的/views文件夾中的文件/etc/nginx/sites-available/default

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

/views文件夾包含一個文件調用index.erb。參觀時/我看到html頁面

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 

所以現在:

這裏是app.rb文件的內容。但是當訪問/hello時,我得到一個404屏幕。

我在做什麼錯?

更新: 當我在views文件夾中添加名爲hello的空文件夾時,它確實有效。

回答

0

通過添加以下到我的/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的配置問題上面的代碼。