0
0
Nginxdevops~5 mins

Web server vs application server in Nginx - CLI Comparison

Choose your learning style9 modes available
Introduction
A web server handles requests for web pages and static content like images or HTML files. An application server runs the code that creates dynamic content, like processing forms or accessing databases. Understanding the difference helps you set up your system correctly.
When you want to serve static files like images, CSS, or HTML quickly to users.
When you need to run backend code that creates web pages on the fly based on user input.
When you want to separate the part that handles web requests from the part that runs your application logic.
When you want to improve performance by letting the web server handle simple tasks and the application server handle complex ones.
When you want to secure your application by controlling access through the web server before requests reach the application server.
Config File - nginx.conf
nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    server {
        listen       8080;
        server_name  localhost;

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

        location /app/ {
            proxy_pass http://127.0.0.1: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;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}

This configuration sets up Nginx as a web server and a reverse proxy.

worker_processes and worker_connections control how many requests Nginx can handle.

The server block listens on port 8080.

The location / serves static files from /usr/share/nginx/html.

The location /app/ proxies requests to an application server running on localhost port 5000, forwarding headers for proper request handling.

Commands
Check the Nginx configuration file for syntax errors before starting the server.
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
Restart Nginx to apply the new configuration so it can serve static files and proxy requests to the application server.
Terminal
sudo systemctl restart nginx
Expected OutputExpected
No output (command runs silently)
Request a static HTML file from the web server to verify it serves static content correctly.
Terminal
curl http://localhost:8080/index.html
Expected OutputExpected
<!DOCTYPE html> <html> <head><title>Welcome</title></head> <body> <h1>Hello from Nginx web server</h1> </body> </html>
Send a request to the application server through Nginx to verify dynamic content is served via proxy.
Terminal
curl http://localhost:8080/app/api/data
Expected OutputExpected
{"data":"This is dynamic content from the application server"}
Key Concept

If you remember nothing else from this pattern, remember: a web server handles static content and forwards dynamic requests to an application server.

Common Mistakes
Configuring Nginx to serve dynamic content directly without proxying to an application server.
Nginx cannot run backend code like Python or Node.js, so dynamic content won't work.
Use Nginx to proxy dynamic requests to a separate application server that runs your backend code.
Not forwarding necessary headers in the proxy configuration.
The application server may not receive correct client information, causing errors or wrong behavior.
Include proxy_set_header directives to forward host and client IP headers.
Serving dynamic URLs under the same location as static files without proxying.
Nginx will try to find static files for dynamic URLs and return 404 errors.
Use separate location blocks for static content and proxy dynamic requests.
Summary
Nginx serves static files directly to users for fast delivery.
Nginx proxies dynamic requests to an application server that runs backend code.
Proper proxy configuration ensures smooth communication between web and application servers.