I want to write some more complex conditions in my Nginx configuration files but I'm not sure of the syntax and can't find docs describing what you can do beyond the basics in the examples and I can't seem to find this on the Nginx forums or on the mailing list.
For example, is it possible for me to have an unless
condition?
Configuration files have largely adopted a serialization format, such as XML, YAML and JSON, to represent complex data structures in a way that is easily stored and parsed. The resulting filenames can reflect the format. For example, a configuration file in YAML format may appear such as myapplication_configuration.
NGINX location directive syntax The NGINX location block can be placed inside a server block or inside another location block with some restrictions. The syntax for constructing a location block is: location [modifier] [URI] { ... ... } The modifier in the location block is optional.
It currently supports seven languages – Go, Node. js, Perl, PHP, Python, Ruby, and Java Servlet Containers (the last as an experimental module). You can run applications written in different languages on the same server.
So I'm a newbie to nginx, and had this same question. Turns out the syntax of the language as mentioned above is both custom and actually quite simple. The syntax is captured in a section in the NGINX docs, and repeated here for convenience:
nginx consists of modules which are controlled by directives specified in the configuration file. Directives are divided into simple directives and block directives. A simple directive consists of the name and parameters separated by spaces and ends with a semicolon (;). A block directive has the same structure as a simple directive, but instead of the semicolon it ends with a set of additional instructions surrounded by braces ({ and }). If a block directive can have other directives inside braces, it is called a context (examples: events, http, server, and location).
Directives placed in the configuration file outside of any contexts are considered to be in the main context. The events and http directives reside in the main context, server in http, and location in server.
The rest of a line after the # sign is considered a comment.
In summary: Everything in an NGINX config file is a directive which may reference a variable. All directives are listed alphabetically here, and all variables are listed alphabetically here. NGINX configuration is driven by modules that each implement a certain piece of functionality, and each module contributes directives and variables that become available for use within the config. That's it.
That is why even if
-- which looks like a keyword like in a traditional programming language -- is actually just a directive contributed by the ngx_http_rewrite_module
module.
Hope this helps!
PS - Also check out https://devdocs.io/, and specifically https://devdocs.io/nginx, for a much improved way to search/use the NGINX documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With