nginx.conf

Warning

This article assumes NGINX is installed, and the nginx.service is currently running. The nginx.service can be started by entering the following command:

systemctl start nginx

NGINX Configuration

NGINX is predominately driven by configuration files.

To configure NGINX to server static files, or to reverse proxy to a running web application framework NGINX requires a valid configuration file that instructs the NGINX web server on how to behave.

NGINX configuration files end with the suffix .conf.

Viewing Default Configuration File

By default a newly installed NGINX web server is configured to serve an example static HTML file.

Note

You can view the response made by making a web request to localhost in your browser, or with curl:

curl localhost

This was covered in the previous article.

The configuration file responsible for this NGINX web server behavior is located at: /etc/nginx/conf.d/default.conf. Take a look at the file with:

cat /etc/nginx/conf.d/default.conf

Contents of /etc/nginx/conf.d/default.conf:

server {
  listen      80;
  server_name localhost;

  #access_log /var/log/nginx/host.access.log  main;

  location / {
      root    /usr/share/nginx/html;
      index   index.html index.htm;
  }

  #error_page   404           /404.html;

  # redirect server error pages to the static page  /50x.html
  #
  error_page    500 502 503 504   /50x.html
  location = /50x.html {
      root    /usr/share/nginx/html;
  }

  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #    proxy_pass    http://127.0.0.1;
  #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  #location ~ \.php$ {
  #    root            html;
  #    fastcgi_pass    127.0.0.1:9000;
  #    fastcgi_index   index.php;
  #    fastcgi_param   SCRIPT_FILENAME   /scripts$fastcgi_script_name;
  #    include         fastcgi_params;
  #}

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #    deny    all;
  #}
}

There are many commented out notes in the default.conf file. NGINX provides this file with examples to guide the configuration in a way that serve the needs of the user.

Ignoring the commented out lines the file contents are:

server {
  listen      80;
  server_name localhost;

  location / {
      root    /usr/share/nginx/html;
      index   index.html index.htm;
  }

  error_page    500 502 503 504   /50x.html
  location = /50x.html {
      root    /usr/share/nginx/html;
  }

This configuration file:

  • is listening on port 80 (the default HTTP port)
  • is named localhost
  • any requests made to localhost:80:
    • will be served from the root location of /usr/share/nginx/html (on this computer’s file system)
    • if the HTTP request is missing a file extension from the path: index.html will be added and then index.html added before returning a 400 level HTTP status code.

To NGINX this means an HTTP request made to localhost:80/ would result in an HTTP response including the file located at /user/share/nginx/html/index.html.

Additional configuration instructs NGINX on handling HTTP Status Codes 500, 502, 503 and 504. In the case of a 5XX level error NGINX should respond with the corresponding 50x.html file found in /usr/share/nginx/html.

Note

In the following articles you will edit the existing configuration file and add new configuration files.

Configuration File Location

NGINX can be configured to load .conf files from many different locations. It is a best practice to store any custom .conf in the /etc/nginx/conf.d directory.

Top Level Configuration File

In addition to the user-defined configuration files there is a top level NGINX configuration file found at: /etc/nginx/nginx.conf.

This file contains high level configurations about NGINX itself, not for creating a new web server definition. This course will not cover the top level configuration file.