0
0
Nginxdevops~5 mins

First Nginx configuration - Commands & Configuration

Choose your learning style9 modes available
Introduction
Nginx is a web server that helps deliver websites to users. This configuration sets up a simple website so your server can respond to web requests.
When you want to serve a basic website from your server.
When you need to test if Nginx is installed and working correctly.
When you want to learn how to configure a web server step-by-step.
When you want to serve static files like HTML or images.
When you want to listen on a specific port for web traffic.
Config File - nginx.conf
nginx.conf
events {}
http {
    server {
        listen 8080;
        server_name localhost;

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

This file tells Nginx to listen on port 8080 for web requests.

The server block defines how to handle requests.

The location / block serves files from /usr/share/nginx/html, starting with index.html.

Commands
Start Nginx using the custom configuration file in the current directory.
Terminal
sudo nginx -c $(pwd)/nginx.conf
Expected OutputExpected
No output (command runs silently)
-c - Specify the path to the configuration file.
Check if Nginx is serving the default page on port 8080.
Terminal
curl http://localhost:8080
Expected OutputExpected
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> </head> <body> <h1>Welcome to nginx!</h1> </body> </html>
Stop the Nginx server after testing to free the port.
Terminal
sudo nginx -s quit
Expected OutputExpected
No output (command runs silently)
-s quit - Send the quit signal to the Nginx process.
Key Concept

If you remember nothing else from this pattern, remember: the nginx.conf file controls how Nginx listens and serves files.

Common Mistakes
Not specifying the full path to the custom nginx.conf file when starting Nginx.
Nginx will use the default config and ignore your custom settings.
Use the -c flag with the full or relative path to your nginx.conf file.
Trying to access Nginx on port 80 when it is configured to listen on port 8080.
The browser or curl will not connect because Nginx is not listening on port 80.
Use the correct port number in the URL, like http://localhost:8080.
Not stopping Nginx after testing, causing port conflicts later.
The port remains in use and starting Nginx again will fail.
Run 'sudo nginx -s quit' to cleanly stop the server.
Summary
Create an nginx.conf file to tell Nginx which port to listen on and where to find website files.
Start Nginx with the custom config using 'sudo nginx -c nginx.conf'.
Test the server response with 'curl http://localhost:8080'.
Stop Nginx after testing with 'sudo nginx -s quit'.