nico_lrx
nico_lrx

Reputation: 280

Reverse proxy not passing query string to application

I created a rails app that is deployed into a subdirectory of my Vultr server. Somehow, no GET parameters are considered by the backend. For example, calls from the ForestAdmin API don't read GET parameters (see issue here). Also, my search page is not receiving GET parameters, example for this search query in production, I get the following logs:

logs

As you can see, in the title, the « » is blank as it should display the q parameter.

My Rails app config seems right, so I guess it's a server configuration issue.

Here are my routes:

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

And here is my nginx config:

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;
}

and

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

Any idea what could be the issue preventing the parameters from being passed?

Upvotes: 3

Views: 4888

Answers (1)

pygeek
pygeek

Reputation: 7404

Problem

proxy_pass is not forwarding query strings to rails application

Solution

Add $is_args to your proxy pass statement. This includes either empty string or “?” depending on presence query string in request.

Add $args or $query_string to your proxy pass statement. This appends the query string to your proxied request.

Example

Instead of:

proxy_pass http://127.0.0.1:3000$uri;

Do:

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

References

Nginx http core module (navigate to embedded variables): http://nginx.org/en/docs/http/ngx_http_core_module.html

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

Upvotes: 3

Related Questions