NGINX Configuration Directives

NGINX configuration can be spread across several files to specify different configuration directives and set the values for configuration variables. Configuration is stored in /etc/nginx. The base configuration is stored in /etc/nginx/nginx.conf, while site specific configuration tends to be created within distinct files in /etc/nginx/conf.d/. By convention, site configurations tend to use the full domain name for the file name and would have a .conf suffix.

In these examples, a configuration has the following general format:

http {
  server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    location / {
       root /usr/share/nginx/html/example.com;
       index index.html;
    }
  }
}

The previous example shows an HTTP server configuration for a web server that serves content from the web root directory at /usr/share/nginx/html/example.com.

The following configuration directives are useful for configuring load balancing:

http, https, stream

Defines the protocol for which the settings apply. Use https for TLS connections to the load balancer and stream for generic TCP/UDP traffic.

server

Defines how to handle incoming traffic from the specified ports for the chosen protocol.

To configure at least one listening port for IPv4, use the listen keyword:

listen 80;

To listen on IPv6 interfaces, prepend the [::]: directive to the port number, for example:

listen [::]:80

Note that the listen lines can be duplicated to specify more than one port for a server{} block.

Use the server_name keyword to define the hostname or domain name that the server responds to. If you don't specify this value, the configuration applies to any incoming connection, however you might need to comment out the default server configuration within /etc/nginx/nginx.conf to avoid conflicting configuration definitions.

location

The location directive defines path mappings and behavior, depending on incoming requests on the server. At minimum, you must have a value for the web root that's indicated with the value /. The behavior is defined by setting values within a location block.

For example, to configure a simple web server that serves content from a directory on the server, use the root keyword and specify the content's directory location.

The proxy_pass directive can be used to implement a reverse proxy service. Traffic is proxied onto the specified server or group of servers, as defined in an upstream directive.

For example, you would proxy inbound HTTP traffic to a website that's hosted on websrv1.example.com on port 9090 as follows:

server {
  location / {
    proxy_pass http://websvr1.example.com:9090
  }
}              

You can also specify a server group by referencing its defined upstream name.

upstream

An upstream directive is used to define a group of one or more servers where the content is stored and which can be used by the proxy_pass directive. For example, you can create an upstream group of servers called backend as follows:

upstream backend {
    server server1.example.com;
    server server2.example.com;
    server server3.example.com;
  }

To use this group, the proxy_pass directive is specified:

proxy_pass http://backend

The upstream directive is the key configuration component that's used to control load-balancing methods and algorithms. For more information, see http://nginx.org/en/docs/http/ngx_http_upstream_module.html.