Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于if-else条件的NGINX proxy_pass_Nginx_Lua - Fatal编程技术网

基于if-else条件的NGINX proxy_pass

基于if-else条件的NGINX proxy_pass,nginx,lua,Nginx,Lua,问题陈述 我想根据请求头中的某个值代理传递到另一个url。所有请求详细信息,包括查询参数(如果有)、头和所有内容都应传递到代理地址 我尝试过的 我已经遵循并根据上述要求尝试了以下方法 Test.lua文件 nginx.conf 如您所见,我正在proxy\u pass语句中手动传递查询参数 如何通过在代理传递过程中不传递实际的请求来解决此问题?您的问题可以通过根据标头重写原始请求来轻松解决。 下面是一个例子: #sample backend set $backend_host "http://h

问题陈述

我想根据请求头中的某个值代理传递到另一个url。所有请求详细信息,包括查询参数(如果有)、头和所有内容都应传递到代理地址

我尝试过的

我已经遵循并根据上述要求尝试了以下方法

Test.lua文件

nginx.conf

如您所见,我正在
proxy\u pass
语句中手动传递查询参数


如何通过在代理传递过程中不传递实际的
请求
来解决此问题?

您的问题可以通过根据标头重写原始请求来轻松解决。 下面是一个例子:

#sample backend
set $backend_host "http://httpbin.org";

location ~*/api/employees {
        rewrite_by_lua '
             --reading request headers
             local req_headers = ngx.req.get_headers()
             local target_uri  = ""
             -- checking an a header value to determine target uri
             if req_headers["x-special-header"] then
                target_uri = req_headers["x-special-header"]
             else 
                -- default path if not header found
                target_uri = "/get"
             end 
            ngx.log(ngx.NOTICE, string.format("resolved target_uri: %s", target_uri))
            --rewriting uri according to header (all original req args and headers are preserved)
            ngx.req.set_uri(target_uri)
        ';
        proxy_pass $backend_host;
}
发送“特殊”标头作为目标后端路径的请求示例:

curl 'http://localhost/api/employees?arg1=val1&arg2=val2' -H 'x-special-header: /headers'
答复:

    {
  "headers": {
    "Accept": "*/*",
    "Connect-Time": "0",
    "Connection": "close",
    "Host": "httpbin.org",
    "Total-Route-Time": "0",
    "User-Agent": "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2",
    "Via": "1.1 vegur",
    "X-Request-Id": "6e515e0a-0061-4576-b1aa-5da3e3308c81",
    "X-Special-Header": "/headers"
  }
{
  "args": {
    "arg1": "val1",
    "arg2": "val2"
  },
  "headers": {
    "Accept": "*/*",
    "Connect-Time": "4",
    "Connection": "close",
    "Host": "httpbin.org",
    "Total-Route-Time": "0",
    "User-Agent": "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2",
    "Via": "1.1 vegur",
    "X-Request-Id": "520c0e12-1361-4c78-8bdf-1bff3f9d924c"
  },
  "url": "http://httpbin.org/get?arg1=val1&arg2=val2"
}
不带“特殊”标题的样本请求

curl 'http://localhost/api/employees?arg1=val1&arg2=val2'
答复:

    {
  "headers": {
    "Accept": "*/*",
    "Connect-Time": "0",
    "Connection": "close",
    "Host": "httpbin.org",
    "Total-Route-Time": "0",
    "User-Agent": "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2",
    "Via": "1.1 vegur",
    "X-Request-Id": "6e515e0a-0061-4576-b1aa-5da3e3308c81",
    "X-Special-Header": "/headers"
  }
{
  "args": {
    "arg1": "val1",
    "arg2": "val2"
  },
  "headers": {
    "Accept": "*/*",
    "Connect-Time": "4",
    "Connection": "close",
    "Host": "httpbin.org",
    "Total-Route-Time": "0",
    "User-Agent": "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2",
    "Via": "1.1 vegur",
    "X-Request-Id": "520c0e12-1361-4c78-8bdf-1bff3f9d924c"
  },
  "url": "http://httpbin.org/get?arg1=val1&arg2=val2"
}