Skip to content

Nginx Directives API

内置 Nginx 变量,暴露可在指令中使用的请求和连接属性。

1 class · 5 methods

Variables

5 methods

http 上下文中可用的常用内置变量。

server { listen port; server_name name; ... }

来自请求行的主机名(小写),或 Host 头,或匹配的服务器名。

Parameters

NameTypeDescription
listenstringAddress:port or unix socket.
server_namestringHostname(s) matched against the Host header.

Returns

void

Example

nginx
server {
  listen 80;
  server_name api.example.com;
}
location [ = | ~ | ~* | ^~ ] uri { ... }

完整的原始请求 URI,包括查询字符串,未修改。

Parameters

NameTypeDescription
modifierstring= exact, ~ regex case-sensitive, ~* regex case-insensitive, ^~ prefix priority.
uristringURI prefix or regular expression to match.

Returns

void

Example

nginx
location ~* \.(png|jpg|css|js)$ {
  expires 1h;
}
proxy_pass URL;

连接的客户端 IP 地址。

Parameters

NameTypeDescription
URLstringUpstream URL such as http://127.0.0.1:3000 or a named upstream.

Returns

void

Example

nginx
location /api/ {
  proxy_pass http://127.0.0.1:3000/;
}
return code [text | URL];

来自请求行的完整查询字符串;值相同。

Parameters

NameTypeDescription
codeintegerHTTP status code, e.g. 301, 404, 200.
textstringResponse body or redirect URL.

Returns

void

Example

nginx
return 301 https://example.com$request_uri;
try_files file ... uri | =code;

请求协议:http 或 https。

Parameters

NameTypeDescription
filestringFile path relative to root; $uri may be used.
uristringFallback internal URI.

Returns

void

Example

nginx
try_files $uri $uri/ /index.html;