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
httpsfor TLS connections to the load balancer andstreamfor 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
listenkeyword:listen 80;To listen on IPv6 interfaces, prepend the
[::]:directive to the port number, for example:listen [::]:80Note that the
listenlines can be duplicated to specify more than one port for aserver{}block.Use the
server_namekeyword 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.confto avoid conflicting configuration definitions. - location
-
The
locationdirective 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
rootkeyword and specify the content's directory location.The
proxy_passdirective 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.comon port9090as follows:server { location / { proxy_pass http://websvr1.example.com:9090 } }You can also specify a server group by referencing its defined
upstreamname. - upstream
-
An
upstreamdirective is used to define a group of one or more servers where the content is stored and which can be used by theproxy_passdirective. For example, you can create an upstream group of servers calledbackendas follows:upstream backend { server server1.example.com; server server2.example.com; server server3.example.com; }To use this group, the
proxy_passdirective is specified:proxy_pass http://backendThe 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.