nginx location 内部的 location和if中的add_header是如何配合工作的?

653 阅读3分钟

2个if -- 案例一 ,if按先后顺序执行

server {
  listen       9900;
  access_log  /usr/local/var/log/nginx/nested_access.log  main;
  error_log   /usr/local/var/log/nginx/nested_access.log;
  location /dav/ {
    set $a 0000;
    if ( $request_uri ~* ".*/(product_version)$" ) {
        set $a "${a}111"; 
    }
    if ($request_method = 'OPTIONS' ) {
        set $a "${a}222";
    }
    return 200 "a: $a"; 
  }
}

curl -XOPTIONS http://localhost:9900/dav/product_version
a: 0000111222
----------------------------
server {
  listen       9900;
  access_log  /usr/local/var/log/nginx/nested_access.log  main;
  error_log   /usr/local/var/log/nginx/nested_access.log;
  location /dav/ {
    set $a 0000;
    if ($request_method = 'OPTIONS' ) {
        set $a "${a}222";
    }
    if ( $request_uri ~* ".*/(product_version)$" ) {
        set $a "${a}111";
    }
    return 200 "a: $a";
  }
}
curl -XOPTIONS http://localhost:9900/dav/product_version
a: 0000222111

一个location和一个if

    server {
      listen       9900;
      access_log  /usr/local/var/log/nginx/nested_access.log  main;
      error_log   /usr/local/var/log/nginx/nested_access.log;
      location /dav/ {
        location ~* .*/(product_version)$ {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
        }
        if ($request_method = 'OPTIONS' ) {
            add_header Access-Control-Allow-Origin '*';
            add_header Access-Control-Allow-Methods "GET, OPTIONS";
            add_header 'Access-Control-Allow-Headers' 'ORIGIN,X-Auth-Token,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            add_header 'Access-Control-Max-Age' 1728000;

            add_header Content-Length 0;
            add_header Content-Type text/plain;
            return 204;
          }

         }
    }
curl -vv -XOPTIONS http://localhost:9900/dav/product_version
*   Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 9900 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9900 (#0)
> OPTIONS /dav/product_version HTTP/1.1
> Host: localhost:9900
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 405 Not Allowed
< Server: nginx/1.19.5
< Date: Thu, 24 Jun 2021 13:46:25 GMT
< Content-Type: text/html
< Content-Length: 157
< Connection: keep-alive
<
<html>
<head><title>405 Not Allowed</title></head>
<body>
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.19.5</center>
</body>
</html>
* Connection #0 to host localhost left intact

使用2个if,请求成功

  server {
    listen       9900;
    access_log  /usr/local/var/log/nginx/nested_access.log  main;
    error_log   /usr/local/var/log/nginx/nested_access.log;
    location /dav/ {
      if ( $request_uri ~* ".*/(product_version)$" ) {
          add_header 'Access-Control-Allow-Origin' '*';
          add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
      }
      if ($request_method = 'OPTIONS' ) {
          add_header Access-Control-Allow-Origin '*';
          add_header Access-Control-Allow-Methods "GET, OPTIONS";
          add_header 'Access-Control-Allow-Headers' 'ORIGIN,X-Auth-Token,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
          add_header 'Access-Control-Max-Age' 1728000;

          add_header Content-Length 0;
          add_header Content-Type text/plain;
          return 204;
        }

       }
  }
*   Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 9900 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9900 (#0)
> OPTIONS /dav/product_version HTTP/1.1
> Host: localhost:9900
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 204 No Content
< Server: nginx/1.19.5
< Date: Thu, 24 Jun 2021 13:55:27 GMT
< Connection: keep-alive
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, OPTIONS
< Access-Control-Allow-Headers: ORIGIN,X-Auth-Token,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type
< Access-Control-Max-Age: 1728000
< Content-Length: 0
< Content-Type: text/plain
<
* Connection #0 to host localhost left intact

request_method在前,request_uri在后 --案例3

  server {
    listen       9900;
    access_log  /usr/local/var/log/nginx/nested_access.log  main;
    error_log   /usr/local/var/log/nginx/nested_access.log;
    location /dav/ {
      if ($request_method = 'OPTIONS' ) {
          add_header Access-Control-Allow-Origin '*';
          add_header Access-Control-Allow-Methods "GET, OPTIONS";
          add_header 'Access-Control-Allow-Headers' 'ORIGIN,X-Auth-Token,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
          add_header 'Access-Control-Max-Age' 1728000;

          add_header Content-Length 0;
          add_header Content-Type text/plain;
      }
      if ( $request_uri ~* ".*/(product_version)$" ) {
          add_header 'Access-Control-Allow-Origin' '*';
          add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
          add_header "My-Header" 'My';
      }
      return 200; 
    }
  }

curl -vv -XOPTIONS http://localhost:9900/dav/product_version
< HTTP/1.1 200 OK
< Server: nginx/1.19.5
< Date: Thu, 24 Jun 2021 14:08:31 GMT
< Content-Type: application/octet-stream
< Content-Length: 0
< Connection: keep-alive
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, OPTIONS
< My-Header: My
<
* Connection #0 to host localhost left intact

curl -vv -XGET http://localhost:9900/dav/product_version
< HTTP/1.1 200 OK
< Server: nginx/1.19.5
< Date: Thu, 24 Jun 2021 14:09:37 GMT
< Content-Type: application/octet-stream
< Content-Length: 0
< Connection: keep-alive
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, OPTIONS
< My-Header: My

request_method在后,request_uri在前 --案例4

  server {
    listen       9900;
    access_log  /usr/local/var/log/nginx/nested_access.log  main;
    error_log   /usr/local/var/log/nginx/nested_access.log;
    location /dav/ {
      if ( $request_uri ~* ".*/(product_version)$" ) {
          add_header 'Access-Control-Allow-Origin' '*';
          add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
          add_header "My-Header" 'My';
      }
      if ($request_method = 'OPTIONS' ) {
          add_header Access-Control-Allow-Origin '*';
          add_header Access-Control-Allow-Methods "GET, OPTIONS";
          add_header 'Access-Control-Allow-Headers' 'ORIGIN,X-Auth-Token,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
          add_header 'Access-Control-Max-Age' 1728000;

          add_header Content-Length 0;
          add_header Content-Type text/plain;
      }
      return 200;
    }
  }
  
curl -vv -XOPTIONS http://localhost:9900/dav/product_version
< HTTP/1.1 200 OK
< Server: nginx/1.19.5
< Date: Thu, 24 Jun 2021 14:12:53 GMT
< Content-Type: application/octet-stream
< Content-Length: 0
< Connection: keep-alive
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, OPTIONS
< Access-Control-Allow-Headers: ORIGIN,X-Auth-Token,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type
< Access-Control-Max-Age: 1728000
< Content-Length: 0
< Content-Type: text/plain

curl -vv -XGET http://localhost:9900/dav/product_version
< HTTP/1.1 200 OK
< Server: nginx/1.19.5
< Date: Thu, 24 Jun 2021 14:13:22 GMT
< Content-Type: application/octet-stream
< Content-Length: 0
< Connection: keep-alive
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, OPTIONS
< My-Header: My

结论

  • location 下面2个if 都含有 add_header ,但是不会相互继承,以最后面一个匹配的if下的规则为为准
  • location 下面2个if 都不含有 add_header ,if规则先后执行

参考链接