Variables
5 methodshttp 上下文中可用的常用内置变量。
server { listen port; server_name name; ... }来自请求行的主机名(小写),或 Host 头,或匹配的服务器名。
Parameters
| Name | Type | Description |
|---|---|---|
| listen | string | Address:port or unix socket. |
| server_name | string | Hostname(s) matched against the Host header. |
Returns
void
Example
nginx
server {
listen 80;
server_name api.example.com;
}location [ = | ~ | ~* | ^~ ] uri { ... }完整的原始请求 URI,包括查询字符串,未修改。
Parameters
| Name | Type | Description |
|---|---|---|
| modifier | string | = exact, ~ regex case-sensitive, ~* regex case-insensitive, ^~ prefix priority. |
| uri | string | URI prefix or regular expression to match. |
Returns
void
Example
nginx
location ~* \.(png|jpg|css|js)$ {
expires 1h;
}proxy_pass URL;连接的客户端 IP 地址。
Parameters
| Name | Type | Description |
|---|---|---|
| URL | string | Upstream 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
| Name | Type | Description |
|---|---|---|
| code | integer | HTTP status code, e.g. 301, 404, 200. |
| text | string | Response body or redirect URL. |
Returns
void
Example
nginx
return 301 https://example.com$request_uri;try_files file ... uri | =code;请求协议:http 或 https。
Parameters
| Name | Type | Description |
|---|---|---|
| file | string | File path relative to root; $uri may be used. |
| uri | string | Fallback internal URI. |
Returns
void
Example
nginx
try_files $uri $uri/ /index.html;