Michael Hobbs
Michael Hobbs

Reputation: 1693

NGINX Rewrite ignored / not working with proxy_pass

Bug in the upstream vendor app. Wrote a route in our node app to proxy the request and avoid the bug but can't get the NGINX rewrite to work correctly. I've tried many variations of rewrite and now at my wit's end. Spent more time on the rewrite than the actual code... =(

IN: /Txtranscription/transcription/TranscriptionHandler.ashx?q=c3R1ZHlfaWQ...
OUT: /Txtranscription/transcription/TranscriptionHandler.ashx?q=c3R1ZHlfaWQ...
EXPECTED: /transcription?encoded=c3R1ZHlfaWQ... 

### override handling of /Txtranscription/transcription/TranscriptionHandler.ashx
location /Txtranscription/transcription/TranscriptionHandler.ashx {
    add_header Access-Control-Allow-Origin $cors_header;
    access_log  logs/vapi.proxy.log lfupstream;
    error_log  logs/vapi.error.log error;
    rewrite ^/Txtranscription/transcription/TranscriptionHandler\.ashx\?q=(.*)$ /transcription?encoded=$1 break;
    proxy_pass http://vapi;
}

Upvotes: 0

Views: 591

Answers (2)

psicopante
psicopante

Reputation: 381

Example:

 location ~ ^/connector(/?)(.*)$ {
    proxy_buffer_size 64k;
    proxy_buffers 16 32k;
    proxy_http_version 1.1;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Connection "Keep-Alive";
    proxy_set_header Proxy-Connection "Keep-Alive";
    proxy_set_header Authorization "";
    set $upstream_endpoint http://YOUR-END-POINT/$2$is_args$args; 
    proxy_pass $upstream_endpoint;
  }

The magic is -> location ~ ^ /admin (/?)(.*)$

And then -> /$2$is_args$args;

Upvotes: 0

miknik
miknik

Reputation: 5941

You shouldn't need to rewrite the request at all, you can append a path to a proxy_pass directive and Nginx will replace the matching part of the location block from the original request URI with the URI of your proxy_pass directive.

So this should work:

location /Txtranscription/transcription/TranscriptionHandler.ashx {
    set $args encoded=$arg_q;
    ....
    proxy_pass http://vapi/transcription$is_args$args;

Upvotes: 1

Related Questions