简体   繁体   English

NGINX 重写被忽略/不与 proxy_pass 一起工作

[英]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.在我们的节点应用程序中编写了一个路由来代理请求并避免错误,但无法让 NGINX 重写正常工作。 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;
}

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.您根本不需要重写请求,您可以将路径附加到proxy_pass指令,Nginx 会将原始请求 URI 中位置块的匹配部分替换为您的proxy_pass指令的 URI。

So this should work:所以这应该有效:

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

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 (/?)(.*)$神奇的是 -> location ~ ^ /admin (/?)(.*)$

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM