0
0
Nginxdevops~5 mins

Main configuration file (nginx.conf) - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to serve websites or apps, you need a server that listens to requests and sends back responses. Nginx is a popular server software that uses a main configuration file called nginx.conf to control how it works and handles traffic.
When you want to host a simple website on your own server or computer.
When you need to reverse proxy requests to another app running on a different port.
When you want to set up caching or compression to speed up your website.
When you want to control access to your site with security rules.
When you want to serve multiple websites from the same server using different domain names.
Config File - nginx.conf
nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile on;
    keepalive_timeout 65;

    server {
        listen 80;
        server_name localhost;

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

        error_page 404 /404.html;
        location = /404.html {
            root /usr/share/nginx/html;
        }
    }
}

user nginx; sets the user running the worker processes.
worker_processes auto; lets nginx decide how many worker processes to run based on CPU cores.
events block controls connection handling.
http block contains settings for web traffic, including MIME types, logging, and server blocks.
server block defines how to handle requests on port 80 for the localhost domain.
location / serves files from the default web folder.
error_page defines a custom 404 error page.

Commands
This command tests the nginx configuration file for syntax errors before applying it. It helps catch mistakes early.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command restarts the nginx service to apply any changes made in the configuration file.
Terminal
sudo systemctl restart nginx
Expected OutputExpected
No output (command runs silently)
This command checks if the nginx service is running properly after restart.
Terminal
sudo systemctl status nginx
Expected OutputExpected
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2024-06-07 10:00:00 UTC; 5s ago Main PID: 1234 (nginx) Tasks: 2 (limit: 1152) Memory: 3.5M CGroup: /system.slice/nginx.service ├─1234 nginx: master process /usr/sbin/nginx └─1235 nginx: worker process
This command sends a simple HTTP request to the local nginx server to check if it responds correctly.
Terminal
curl -I http://localhost
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx/1.24.0 Date: Fri, 07 Jun 2024 10:00:05 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Wed, 01 May 2024 12:00:00 GMT Connection: keep-alive ETag: "607d-5a1c9f4e4a000" Accept-Ranges: bytes
Key Concept

If you remember nothing else from this pattern, remember: nginx.conf controls how nginx listens, serves files, and handles web requests.

Common Mistakes
Editing nginx.conf without testing syntax using 'nginx -t' before restarting.
This can cause nginx to fail restarting due to syntax errors, making your website unavailable.
Always run 'sudo nginx -t' to check for errors before restarting nginx.
Not restarting nginx after changing the configuration file.
Changes won't take effect until nginx reloads or restarts, so your updates won't work.
Run 'sudo systemctl restart nginx' or 'sudo nginx -s reload' after changes.
Setting incorrect file paths in the root directive inside the server block.
Nginx won't find your website files and will return errors or blank pages.
Use the correct absolute path to your website files, like '/usr/share/nginx/html'.
Summary
The nginx.conf file sets global and server-specific settings for nginx web server.
Use 'nginx -t' to test configuration syntax before restarting nginx.
Restart nginx with 'systemctl restart nginx' to apply changes and verify with 'systemctl status nginx'.
Test the server response using 'curl -I http://localhost' to confirm nginx serves requests.