Nginx Fundmentals

Part - 2

Variables

events {
    # Event settings can go here if needed
}

http {
    # Include mime types for content type determination
    include mime.types;
    # Define the server block
    server {
        listen 80;  # Listen on port 80
        server_name 54.226.209.32;  # Replace with your server IP or domain
        # Serve static files directly from /var/www/demo/
        location /inspect {
          return 200 "$host\n$uri\n$args"; 
        }
    }
}

$host gives the ip adress

$uri is path or uniform resource identifier

here $args is not present as we have not queried

so now after queried even $args is displayed

events {
    # Event settings can go here if needed
}

http {
    # Include mime types for content type determination
    include mime.types;
    # Define the server block
    server {
        listen 80;  # Listen on port 80
        server_name 54.226.209.32;  # Replace with your server IP or domain
        # Serve static files directly from /var/www/demo/
        location /inspect {
          return 200 "City : $arg_city"; 
        }
    }
}

here,in 54.226.209.32/inspect?city\=Bengaluru should match with $arg_city

events {
    # Event settings can go here if needed
}

http {
    # Include mime types for content type determination
    include mime.types;
    # Define the server block
    server {
        listen 80;  # Listen on port 80
        server_name 54.226.209.32;  # Replace with your server IP or domain

        if ( $arg_server != "nginx" ) {
          return 401 "incorrect API key";
          }
    }
 }

Here, the variable is not case sensitive, but the variable value is.

events {
    # Event settings can go here if needed
}

http {
    # Include mime types for content type determination
    include mime.types;
    # Define the server block
    server {
        listen 80;  # Listen on port 80
        server_name 54.226.209.32;  # Replace with your server IP or domain

        set $weekend "No";

        if ( $date_local ~* "Saturday|Sunday" ) {
                set $weekend "Yes";
                }
        location /weekend {
          return 200 $weekend;
        }
    }
}

Rewrites and Redirects

events {
    # Event settings can go here if needed
}

http {
    # Include mime types for content type determination
    include mime.types;
    # Define the server block
    server {
        listen 80;  # Listen on port 80
        server_name 54.226.209.32;  # Replace with your server IP or domain
        root /var/www/demo;    
    }
}

Nginx is directly serving the requested file based on the root directive specified in the server block.

54.226.209.32/UbuntuCoF.svg this gave the above output but if in case I need same output for 54.226.209.32/logo

events {
    # Event settings can go here if needed
}

http {
    # Include mime types for content type determination
    include mime.types;
    # Define the server block
    server {
        listen 80;  # Listen on port 80
        server_name 54.226.209.32;  # Replace with your server IP or domain
        root /var/www/demo;
        location /logo {
          return 307 /UbuntuCoF.svg;
        }
    }
}

here 54.226.209.32/logo is redirected to 54.226.209.32/UbuntuCoF.svg\

here we are redirecting but rewrites mutate the URI internally.

Replace the 307 status code with 200 to address any questions on your mind and enhance your learning process.

events {
    # Event settings can go here if needed
}
http {
    # Include mime types for content type determination
    include mime.types;

    # Define the server block
    server {
        listen 80;  # Listen on port 80
        server_name 54.226.209.32;  # Replace with your server IP or domain
        root /var/www/demo;
        rewrite ^/user/\w+ /greet;
        # Handle requests to /greet
        location /greet {
            return 200 "hello welcome";
        }
    }
}

here we are rewriting, means nginx will internally rewrite 54.226.209.32/user/ram this to 54.226.209.32/greet

events {
}
http {
    # Include mime types for content type determination
    include mime.types;

    # Define the server block
    server {
        listen 80;  # Listen on port 80
        server_name 54.226.209.32;  # Replace with your server IP or domain

        # Root directory for static HTML files
        root /var/www/demo;

        rewrite ^/user/(\w+) /greet/$1;

        # Handle requests to /greet
        location /greet {
            # Return a response including the captured value from the rewrite
            return 200 "hello welcome";
        }
        location = /greet/ram {
                return 200 "hello ram";
        }
    }
}

events {
}
http {
    # Include mime types for content type determination
    include mime.types;

    # Define the server block
    server {
        listen 80;  # Listen on port 80
        server_name 54.226.209.32;  # Replace with your server IP or domain

        # Root directory for static HTML files
        root /var/www/demo;

        rewrite ^/user/(\w+) /greet/$1;   
        rewrite ^/greet/ram /UbuntuCoF.svg;

#here, both rewrite coveys same if url is http://54.226.209.32/greet/ram/ but what they serve is diffrent in this case /UbuntuCoF.svg will be served

        # Handle requests to /greet
        location /greet {
            return 200 "hello welcome";
        }
        location = /greet/ram {
                return 200 "hello ram";
        }
    }
}

So, to serve the first one, which is /greet/$1, you need to use a flag called last so that Nginx won't rewrite a second time and it will be skipped.

events {
}
http {
    # Include mime types for content type determination
    include mime.types;

    # Define the server block
    server {
        listen 80;  # Listen on port 80
        server_name 54.226.209.32;  # Replace with your server IP or domain

        # Root directory for static HTML files
        root /var/www/demo;

        rewrite ^/user/(\w+) /greet/$1 last;   
        rewrite ^/greet/ram /UbuntuCoF.svg;

#here, both rewrite coveys same if url is http://54.226.209.32/greet/ram/ but what they serve is diffrent in this case /UbuntuCoF.svg will be served

        # Handle requests to /greet
        location /greet {
            return 200 "hello welcome";
        }
        location = /greet/ram {
                return 200 "hello ram";
        }
    }
}

You can comment on why the above output for 54.226.209.32/user/Ram is different in this case.

Try Files and Named Locations

events {
}
http {
    # Include mime types for content type determination
    include mime.types;

    # Define the server block
    server {
        listen 80;  # Listen on port 80
        server_name 54.226.209.32;  # Replace with your server IP or domain

        # Root directory for static HTML files
        root /var/www/demo;
      try_files /cat.png /greet;
        location /greet {
            return 200 "hello try files!";
        }

    }
}

Here, it will look for cat.png under /var/www/demo. If not found, it will proceed to the next, which is /greet. This will be rewritten and served. Additionally, the URL path is not compulsory; it will serve regardless of the URL path.

here it is fetching /greet even if it is not present under root directory because it is present as location and this happens if it is only the last argument.

http {
    # Include mime types for content type determination
    include mime.types;

    # Define the server block
    server {
        listen 80;  # Listen on port 80
        server_name 54.226.209.32;  # Replace with your server IP or domain

        # Root directory for static HTML files
        root /var/www/demo;

        # Handle requests for any URI
        location / {
            # Use try_files to check for the requested URI, then fall back to other URIs
            try_files $uri /cat.png /greet @sorry;
        }

        # Handle requests to @sorry
#@sorry is called as named location
        location @sorry {
            # Return a custom 404 response
            return 404 "sorry could not found";
        }

        # Handle requests to /greet
        location /greet {
            # Return a custom response
            return 200 "hello try files!";
        }
    }
}

Even though /greet is listed as a location, it is not being served. The location block is only served if it is the last argument.

Thank you