Directives
5 methodsCore, http, server and location directives.
server { listen port; server_name name; ... }Defines a virtual server block listening on a port for one or more hostnames.
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 { ... }Matches request URIs and applies directives within the block; modifiers control match priority.
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;Forwards the request to an upstream server, optionally inheriting the original URI.
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];Stops processing and returns the given HTTP status code, optionally with text or a redirect 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;Checks the existence of files in order and serves the first found, otherwise falls back to a URI or code.
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;