Location directive, gzip compression in Nginx

Learn types of location block in Nginx and compression technique in Nginx

Location directive

Syntax

location [modifier] [URI] {

}

Modifier plays a vital role in telling Nginx to react differently on different modifiers.

Types of location blocks in an Nginx configuration

  1. Exact Match (=)

  2. Prefix Match (^~)

  3. Case-Insensitive Regular Expression Match (~*)

  4. Case-Sensitive Regular Expression Match (~)

  5. Longest Prefix Match (No Modifier)

Example configurations for each type of location block

Exact Match (=)

requests to /exact will be served from the /var/www/html directory, and the index.html file will be the default document

location = /exact {
    root /var/www/html;
    index index.html;
}

Prefix Match (^~)

Requests starting with /images/ will be served from the /var/www/images/ directory using the alias directive

location ^~ /images/ {
    alias /var/www/images/;
}

Case-Insensitive Regular Expression Match (~*)

location ~* \.(jpg|jpeg|png)$ {
    root /var/www/media;
}

This configuration matches requests for image files with .jpg, .jpeg, or .png extensions (case-insensitive) and serves them from the /var/www/media directory.

Case-Sensitive Regular Expression Match (~)

Requests ending with .php (case-sensitive) will be passed to a PHP-FPM backend for processing

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

Longest Prefix Match (No Modifier)

This generic location block handles requests that don't match any other specific location block. It serves content from the /var/www/default directory with index.html as the default document.

location / {
    root /var/www/default;
    index index.html;
}

How Nginx processes requests

Nginx processes requests by evaluating location blocks in a specific order. When a request comes in, Nginx checks location blocks in the following order

  1. Exact Match (=)

  2. Prefix Match (^~)

  3. Case-Insensitive Regular Expression Match (~*)

  4. Case-Sensitive Regular Expression Match (~)

  5. Longest Prefix Match (No Modifier)

  6. Default Location Block (if defined)

Nginx has gzip compression enabled by default, but Nginx only applies gzip compression to HTML files, leaving all other file types to be served without compression.

I have an example site running which conf looks like,

to add support for other types of files let's modify this conf to the below

server {
        listen 80;
        listen [::]:80;
        root /var/www/sitename.com/spering-html;
        index index.html index.htm;

        # Gzip compression
        gzip on
        gzip_vary on;
        gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
        gzip_min_length 1000;
        gzip_proxied no-cache no-store private expired auth;

       server_name sitename.com www.sitename.com;

        location / {
                try_files $uri $uri/ =404;
        }
}
  1. gzip on

    • This directive enables gzip compression for responses served by Nginx. When gzip on; is set, Nginx will attempt to compress responses before sending them to clients that support gzip compression.
  2. gzip_vary on;

    • The gzip_vary directive adds the "Vary: Accept-Encoding" HTTP header to responses that are compressed using gzip. This header informs caching mechanisms that the content's representation may vary depending on the client's Accept-Encoding header, which helps prevent caching of different compressed versions of the same resource.
  3. gzip_types

    • The gzip_types directive specifies the MIME types of files that should be compressed using gzip. In the configuration you provided, the following MIME types are specified:

      • text/plain: Plain text files.

      • text/css: Cascading Style Sheets (CSS) files.

      • text/xml: XML files.

      • text/javascript: JavaScript files in text format.

      • application/x-javascript: JavaScript files.

      • application/xml: XML files in application format.

  4. gzip_min_length 1000

    • The gzip_min_length directive sets a minimum file size (in bytes) for files to be considered for gzip compression. In your configuration, only files with a size greater than or equal to 1000 bytes will be compressed. Smaller files are not compressed to avoid potential overhead.
  5. gzip_proxied

    • The gzip_proxied directive determines under which conditions Nginx should apply to gzip compression. In your configuration, the following conditions are specified:

      • no-cache: Responses that include the "no-cache" Cache-Control header.

      • no-store: Responses that include the "no-store" Cache-Control header.

      • private: Responses marked as "private."

      • expired: Responses that have expired.

      • auth: Responses that require authentication.

Did you find this article valuable?

Support Muhammad Usama by becoming a sponsor. Any amount is appreciated!