3. Embedded Variables & If

最後更新: 2022-12-26

目錄

  • Embedded Variables
  • Filter by $args
  • Secret Header

Embedded Variables

 

var list

URL

$host           # $host from the "Host" request header field

# in this order of precedence:
#  1. host name from the request line
     ("GET /index.php HTTP/1.1" -> "GET domain/index.php HTTP/1.1")
#  2. host name from the “Host” request header field
     (正常是這個)
#  3. the server name matching a request
    ("listen 80 default_server;" 的 server_name)

思考: $host, $http_host, and $server_name 分別

$hostname                 # server_name 的 directive 設定

$request_uri               # full original request URI with arguments

$uri                           # current URI in request ("/path/to/file")

$scheme                    # Request scheme, "http" or "https"

$https                       # “on” if connection operates in SSL mode, or an empty string otherwise

 

$http_name               # Arbitrary request header field;
                                    (converted to lower case with dashes replaced by underscores)

$cookie_name            # the name cookie

$content_type             # “Content-Type” request header field

$remote_addr             # Client IP

$status

$time_local

$server_X

  • $server_addr     # an address of the server which accepted a request
  • $server_name    # name of the server which accepted a request
                                (If a server contains multiple server_names, only the first one will be present in this variable.)
  • $server_port      # Server's port which accepted a request

ARG

$arg_name                # argument name in the request line

$args                         # arguments in the request line

$is_args                     # "?" if a request line has arguments, or an empty string otherwise

 


Filter by $args

 

if ($args ~ mobile=1){
  return 405;
}

Or

if ($arg_mobile) {
  return 302 http://m.example.com/;
}

 


Secret Header

 

snappy/cloudfront.conf

if ($http_secret != "1234") {
    return 401;
}

Test with curl

curl -H "MySecret: 1234" URL