Understand Directives And Contexts
type
status
date
slug
summary
tags
category
icon
password
The first configuration file
How to Understand Directives and Contexts in NGINX
The few lines of code you've written here, although seemingly simple, introduce two of the most important terminologies of NGINX configuration files. They are directives and contexts.
Technically, everything inside a NGINX configuration file is a directive.
Directives are of two types:
- Simple Directives
- Block Directives
A simple directive consists of the directive name and the space delimited parameters, like
listen
, return
and others. Simple directives are terminated by semicolons.
Block directives are similar to simple directives, except that instead of ending with semicolons, they end with a pair of curly braces { }
enclosing additional instructions.
A block directive capable of containing other directives inside it is called a context, that is events
, http
and so on. There are four core contexts in NGINX:events { }
– Theevents
context is used for setting global configuration regarding how NGINX is going to handle requests on a general level. There can be only oneevents
context in a valid configuration file.
http { }
– Evident by the name,http
context is used for defining configuration regarding how the server is going to handle HTTP and HTTPS requests, specifically. There can be only onehttp
context in a valid configuration file.
server { }
– Theserver
context is nested inside thehttp
context and used for configuring specific virtual servers within a single host. There can be multipleserver
contexts in a valid configuration file nested inside thehttp
context. Eachserver
context is considered a virtual host.
main
– Themain
context is the configuration file itself. Anything written outside of the three previously mentioned contexts is on themain
context.
Official website command index document
Configure multiple servers
When requesting
http://nginx-handbook.test:80
in the browser, the screen will display hello from port 80!
When requesting
http://nginx-handbook.test:8080
in the browser, the screen will display hello from port 8080!
Reference article
Loading...
Last update: 2024-08-22