NGINX Server Profile, The Basics.

A server profile in Nginx is a configuration block that defines how the server should handle requests for a specific domain or subdomain. It specifies the root directory for the website’s files, the default file to be served when the URL does not specify a filename, and can also include other configuration options such as SSL encryption, error pages, and more.

In this post, we’ll be breaking down the basic contents of an Nginx server profile and explaining what each section does. We’ll cover topics such as server name and port, location blocks, root and index files, error pages, SSL configuration, and more.

Here is an example of a generic Nginx server profile with SSL configuration, located in the /etc/nginx/sites-available directory on a Linux-based system:

server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.com;

    # SSL certificate configuration
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;

    # SSL options
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:10m;

    # Basic HTTP authentication
    auth_basic "Restricted Content";
    auth_basic_user_file /path/to/.htpasswd;

    # Root directory and index file
    root /var/www/example.com;
    index index.html;

    # Location blocks for handling requests
    location / {
        try_files $uri $uri/ =404;
    }

    location /api {
        proxy_pass http://localhost:3000;
    }

    # Custom error pages
    error_page 404 /404.html;
    location = /404.html {
        internal;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        internal;
    }
}

In this example, the server profile defines two server blocks: one for HTTP traffic on port 80, which redirects all traffic to HTTPS, and one for HTTPS traffic on port 443, which handles the actual requests.

The SSL certificate configuration specifies the path to the SSL certificate and key files, and the SSL options specify which SSL protocols to use, how to handle SSL sessions, and more.

The server profile also includes basic HTTP authentication for restricting access to certain content, a root directory and index file for serving static files, and location blocks for handling different types of requests.

Finally, the server profile includes custom error pages for 404 Not Found and 50x Server Error responses.

Server name and port:

The first line of an Nginx server block specifies the server name and the port number that the server will listen to. For example:

server {
  listen 80;
  server_name example.com;
}

This block will configure Nginx to listen on port 80 for requests to the domain example.com.

Location blocks:

Nginx uses location blocks to define different actions to be taken for different URLs. For example:

location / {
  # configuration for the root URL
}

location /blog {
  # configuration for URLs that start with /blog
}

Root and index files:

The root directive specifies the root directory for the website’s files, and the index directive specifies the default file to be served when the URL does not specify a filename. For example:

server {
  listen 80;
  server_name example.com;

  root /var/www/example.com;
  index index.html;
}

This block sets the root directory to /var/www/example.com and the default file to index.html.

Error pages:

The error_page directive can be used to customize error pages for specific HTTP status codes. For example:

error_page 404 /404.html;

location = /404.html {
  internal;
  root /var/www/error_pages;
}

This block sets the error page for a 404 status code to /404.html, and specifies that the file is located in the /var/www/error_pages directory.

SSL configuration:

Nginx can be configured to serve HTTPS requests with SSL encryption. SSL certificates can be obtained from certificate authorities or self-signed for testing purposes. For example:

server {
  listen 443 ssl;
  server_name example.com;

  ssl_certificate /path/to/certificate.crt;
  ssl_certificate_key /path/to/private_key.key;
}

These are just a few examples of the many configuration options available in an Nginx server block. The specific contents of a server block will depend on the needs of the website or application being hosted.

Leave a Reply

Scroll to top