首页 > 解决方案 > 反向代理未将查询字符串传递给应用程序

问题描述

我创建了一个 Rails 应用程序,该应用程序部署到我的 Vultr 服务器的子目录中。不知何故,后端不考虑 GET 参数。例如,来自ForestAdmin API的调用不会读取 GET 参数(请参阅此处的问题)。此外,我的搜索页面没有接收到 GET 参数,例如在生产中的这个搜索查询,我得到以下日志:

日志

如您所见,在标题中,« » 是空白的,因为它应该显示 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;
}

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

标签: ruby-on-railsnginxnginx-location

解决方案


问题

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


推荐阅读