反向代理未将查询字符串传递给应用程序

时间:2020-10-11 18:56:55

标签: ruby-on-rails nginx nginx-location

我创建了一个Rails应用程序,该应用程序已部署到Vultr服务器的子目录中。不知何故,后端不考虑GET参数。例如,来自ForestAdmin API的调用不会读取GET参数(see issue here)。另外,我的搜索页面没有收到GET参数,例如生产中的this search query示例,我得到以下日志:

logs

如您所见,标题中的«»为空白,因为它应显示q参数。

我的Rails应用程序配置似乎正确,因此我认为这是服务器配置问题。

这是我的路线:

Rails.application.routes.draw do
    scope 'dictionnaire' do
        mount ForestLiana::Engine => '/forest'
        root to: "home#index"
        resources :words, path: "definition", param: :slug

        post '/search', to: 'words#search'
        get '/recherche', to: 'words#search_page', as: 'recherche'
        get '/:letter', to: 'words#alphabet_page', param: :letter, as: "alphabetic_page"

        post '/api/get_synonyms', to: 'api#get_synonyms'
    end
    
end

这是我的Nginx配置:

location @ruby-app {
#    rewrite ^/dictionnaire-app(.*) $1 break;
    rewrite ^/dictionnaire$ /dictionnaire/ permanent;
    rewrite ^/dictionnaire/definition-(.*) /dictionnaire/definition/$1 permanent;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_redirect off;
    proxy_pass http://127.0.0.1:3000$uri;
    #proxy_set_header X-Forwarded-Proto https;
}

location ~ /dictionnaire(.*)$ {    
    alias /opt/dictionnaire-app/public;
    try_files $1 @ruby-app;
}

location /dictionnaire {
    try_files $uri $uri/ /dictionnaire/index.php?q=$uri&$args;
}

您知道阻止参数传递的问题是什么吗?

1 个答案:

答案 0 :(得分:2)

问题

proxy_pass没有将查询字符串转发到Rails应用程序

解决方案

$is_args添加到您的代理通过语句中。这包括空字符串或“?”取决于请求中的状态查询字符串。

在您的代理通过语句中添加$args$query_string。这会将查询字符串附加到您的代理请求中。

示例

代替:

proxy_pass http://127.0.0.1:3000$uri;

要做:

proxy_pass http://127.0.0.1:3000$uri$is_args$args;

参考

Nginx http核心模块(导航到嵌入式变量):http://nginx.org/en/docs/http/ngx_http_core_module.html

https://stackoverflow.com/a/8130872/806876