Website Performance
What Is a Web Server? Nginx, Apache and Caddy Explained
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيEvery website you visit is delivered by a piece of software called a web server. It listens for connections, reads what the browser asks for, and sends back a response: an HTML page, an image, a JSON document or an error. The term is used for both the machine and the program, but when you set up a VPS, the decision you actually make is about the program. This article explains what web server software does, walks through a single request, and compares the three popular choices on Linux: Nginx, Apache and Caddy.
What a Web Server Actually Does
At its core, a web server speaks HTTP. Its main jobs are:
- Listening on ports 80 (HTTP) and 443 (HTTPS).
- Terminating TLS, which means handling certificates and encryption so the rest of your stack can speak plain HTTP internally.
- Routing requests to the right site using the
Hostheader, so one server can host many domains (virtual hosts). - Serving static files such as CSS, JavaScript and images directly from disk, which it does very efficiently.
- Passing dynamic requests to an application: PHP through PHP-FPM, or Node.js, Python and Go apps through a reverse proxy.
- Applying rules: redirects, compression, cache headers, access control, rate limits and logging.
The Life of a Request
When someone opens https://example.com/blog/:
- DNS turns the domain into your server's IP address.
- The browser opens a TCP connection to port 443 and performs a TLS handshake. The web server presents the certificate for
example.com. - The browser sends
GET /blog/with headers such asHostandAccept-Encoding. - The web server matches the request to a site and a location rule. If a file exists, it sends it. If not, it forwards the request to the application.
- The application builds the page, possibly querying a database, and returns it.
- The web server compresses the response, adds headers, writes a line to the access log and sends it back.
Understanding these steps helps with troubleshooting. A certificate warning points to step 2, a 404 to step 4, and a 502 Bad Gateway usually means the application in step 5 is down or unreachable.
Nginx
Nginx uses an event-driven architecture: a small number of worker processes each handle thousands of connections without creating a thread per visitor. This keeps memory use low and predictable under load, which is why it is a common choice on small VPS plans.
A minimal site for a PHP application:
server {
listen 80;
server_name example.com;
root /var/www/example/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
}The socket path depends on your installed PHP version. Configuration is centralized; Nginx does not read per-directory files, so every change goes through the main config and a reload.
Apache
Apache HTTP Server has been around since the mid-1990s and has a huge ecosystem of modules. Its standout feature is .htaccess: per-directory configuration files that applications can ship themselves. That is convenient on shared hosting, but checking for these files on every request has a cost, so on a VPS you can disable them with AllowOverride None and move rules into the main config.
Apache handles connections through Multi-Processing Modules (MPMs). The modern event MPM combined with PHP-FPM performs well; the older prefork MPM with mod_php uses much more memory per connection.
Caddy
Caddy is a newer server written in Go. Its main attraction is automatic HTTPS: it obtains and renews certificates from Let's Encrypt or ZeroSSL without any extra tools. A complete site can be a few lines:
example.com {
root * /var/www/example
file_server
encode gzip zstd
}It is a great fit for small projects, internal tools and reverse-proxying apps. Its module ecosystem is smaller than that of Nginx or Apache, and fewer tutorials exist for unusual setups.
Comparison Table
| Nginx | Apache | Caddy | |
|---|---|---|---|
| Architecture | Event-driven workers | MPMs (prefork, worker, event) | Go, event-driven |
| Automatic HTTPS | No (use Certbot) | No (use Certbot) | Yes, built in |
| Per-directory config | No | Yes (.htaccess) | No |
| Config style | Blocks, very flexible | Directives, modules | Short Caddyfile or JSON |
| Typical use | High traffic, static files, reverse proxy | Legacy apps, shared hosting habits | Quick setups, small services |
Which One Should You Choose?
- Choose Nginx if you want broad documentation, strong static file performance and a reverse proxy in front of several apps.
- Choose Apache if your application depends on
.htaccessrules or Apache-specific modules. - Choose Caddy if you want HTTPS with the least effort and your setup is simple.
You can also combine them. Many setups run Nginx in front and pass requests to Apache or an app server behind it, a pattern covered in our article on reverse proxies. To try any of them, deploy a Linux VPS and install the server with your distribution's package manager, for example sudo apt install nginx.
FAQ
Is a web server the same as hosting?
No. Hosting is the machine, network and storage you rent. The web server is software running on that machine. On a VPS you install and configure the web server yourself, while shared hosting providers configure it for you.
Can I run two web servers on the same VPS?
Yes, but only one program can listen on a given port at a time. The usual approach is to put one server on ports 80 and 443 and have it forward requests to the other one on an internal port such as 8080.
Do I need a web server for a Node.js or Python app?
The app can listen on a port directly, but it is common to put Nginx or Caddy in front. The front server handles TLS, compression, static files and slow clients, which keeps the application process focused on its own work.