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

Reverse proxy not passing query string to application

我创建了一个 rails 应用程序,它被部署到我的 Vultr 服务器的一个子目录中。不知何故,后端不考虑任何 GET 参数。例如,来自生产环境中 ForestAdmin API don't read GET parameters (see issue here). Also, my search page is not receiving GET parameters, example for this search query 的调用,我得到以下日志:

如您所见,在标题中,« » 是空白的,因为它应该显示 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(.*)  break;
    rewrite ^/dictionnaire$ /dictionnaire/ permanent;
    rewrite ^/dictionnaire/definition-(.*) /dictionnaire/definition/ 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  @ruby-app;
}

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

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

问题

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