Search code examples
nginxurl-rewritingproxypass

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

Solution

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