【问题标题】:NGINX Rewrite ignored / not working with proxy_passNGINX Rewrite 被忽略/不使用 proxy_pass
【发布时间】:2019-02-11 03:48:18
【问题描述】:

上游供应商应用程序中的错误。在我们的节点应用程序中编写了一个路由来代理请求并避免错误,但无法让 NGINX 重写正常工作。我已经尝试了许多重写的变体,现在我无能为力了。花在重写上的时间比实际代码要多... =(

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

【问题讨论】:

    标签: nginx url-rewriting proxypass


    【解决方案1】:

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

    所以这应该有效:

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

    【讨论】:

      【解决方案2】:

      例子:

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

      神奇的是 -> 位置 ~ ^ /admin (/?)(.*)$

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

      【讨论】:

        猜你喜欢
        • 2021-02-24
        • 1970-01-01
        • 2020-11-07
        • 2018-11-23
        • 2022-08-09
        • 2011-11-29
        • 1970-01-01
        • 2019-12-11
        • 1970-01-01
        相关资源
        最近更新 更多