0
0
Nginxdevops~5 mins

Why Nginx handles API routing - Why It Works

Choose your learning style9 modes available
Introduction
When you build web applications, you often need to send requests to different parts of your system. Nginx can help by directing these requests to the right place, especially for APIs. This makes your app faster and easier to manage.
When you want to send API requests to different backend servers based on the URL path.
When you need to serve both a website and API from the same domain without conflicts.
When you want to improve performance by letting Nginx handle routing before requests reach your app.
When you want to add security rules or caching specifically for API routes.
When you want to simplify your backend by letting Nginx manage which service handles which API call.
Config File - nginx.conf
nginx.conf
events {}
http {
    server {
        listen 80;
        server_name example.com;

        location /api/ {
            proxy_pass http://localhost:5000/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

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

This configuration tells Nginx to listen on port 80 for requests to example.com.

Requests starting with /api/ are sent to a backend server running on localhost port 5000.

Other requests serve static files from /var/www/html.

The proxy_set_header lines pass important client information to the backend.

Commands
Check if the Nginx configuration file syntax is correct before applying changes.
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
Reload Nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Send a test request to the API route to verify Nginx routes it to the backend server.
Terminal
curl -i http://example.com/api/test
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Wed, 01 Jan 2025 12:00:00 GMT Content-Type: application/json Content-Length: 15 {"message":"ok"}
-i - Show HTTP response headers along with the body
Send a request to the root URL to verify Nginx serves static files correctly.
Terminal
curl -i http://example.com/
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Wed, 01 Jan 2025 12:00:00 GMT Content-Type: text/html Content-Length: 1256 <!DOCTYPE html><html>...</html>
-i - Show HTTP response headers along with the body
Key Concept

If you remember nothing else from this pattern, remember: Nginx routes API requests by matching URL paths and forwarding them to the right backend server.

Common Mistakes
Not reloading Nginx after changing the configuration file.
Nginx keeps running the old configuration, so routing changes do not take effect.
Always run 'sudo nginx -t' to check syntax, then 'sudo systemctl reload nginx' to apply changes.
Forgetting to include proxy headers like Host and X-Real-IP.
Backend servers may not receive correct client information, causing issues with logging or security.
Add proxy_set_header lines to pass client info to the backend.
Using incorrect proxy_pass URL without trailing slash.
Nginx may append the original URI incorrectly, causing wrong backend paths.
Use a trailing slash in proxy_pass like 'http://localhost:5000/' to correctly map paths.
Summary
Nginx uses location blocks to match API routes and forward them to backend servers.
Always test Nginx configuration syntax before reloading to avoid downtime.
Use curl commands to verify that API routing and static file serving work as expected.