Skip to content

Nginx Directives API

Nginx configuration directives used to define virtual servers, locations and proxying behavior.

1 class · 5 methods

Directives

5 methods

Core, 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

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 { ... }

Matches request URIs and applies directives within the block; modifiers control match priority.

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;

Forwards the request to an upstream server, optionally inheriting the original URI.

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];

Stops processing and returns the given HTTP status code, optionally with text or a redirect 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;

Checks the existence of files in order and serves the first found, otherwise falls back to a URI or code.

Parameters

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

Returns

void

Example

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