Core Concepts
Web Server
Web server concepts and configuration
Web Server
Web servers accept HTTP requests and return responses to clients.
What is a Web Server?
A web server is software that:
- Listens on a network port (usually 80 for HTTP, 443 for HTTPS)
- Receives HTTP requests from clients
- Processes requests or serves static files
- Sends HTTP responses back to clients
- Handles multiple concurrent connections
Popular Web Servers
Nginx
High-performance, lightweight web server
- Used as web server, reverse proxy, load balancer
- Event-driven architecture
- Efficient resource usage
- Configuration:
/etc/nginx/nginx.conf
# Install Nginx
sudo apt install nginx
# Start Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
# Check status
sudo systemctl status nginxApache
Traditional web server with modular design
- Long history and wide adoption
- Process-driven architecture
- Extensive module ecosystem
- Configuration:
/etc/apache2/apache2.conf
# Install Apache
sudo apt install apache2
# Enable modules
sudo a2enmod proxy
sudo a2enmod rewrite
# Restart Apache
sudo systemctl restart apache2HTTP Request/Response Cycle
Client Request:
GET /index.html HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0
Server Response:
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 1234
<html>...</html>Common HTTP Methods
- GET - Retrieve resource
- POST - Submit data
- PUT - Update resource
- DELETE - Remove resource
- PATCH - Partial update
- HEAD - Like GET but no body
- OPTIONS - Describe communication options
Serving Static Files
# Nginx configuration
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}Virtual Hosts
# Multiple sites on one server
server {
listen 80;
server_name site1.com;
root /var/www/site1;
}
server {
listen 80;
server_name site2.com;
root /var/www/site2;
}Common HTTP Status Codes
-
2xx Success
- 200: OK
- 201: Created
- 204: No Content
-
3xx Redirection
- 301: Moved Permanently
- 302: Found (temporary redirect)
- 304: Not Modified
-
4xx Client Error
- 400: Bad Request
- 401: Unauthorized
- 403: Forbidden
- 404: Not Found
-
5xx Server Error
- 500: Internal Server Error
- 502: Bad Gateway
- 503: Service Unavailable
Performance Optimization
# Gzip compression
gzip on;
gzip_types text/plain text/css text/javascript;
gzip_min_length 1000;
# Caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# Connection pooling
keepalive_timeout 65;Logging
# Log HTTP requests
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;Health Checks
# Check if web server is running
curl -I http://localhost
# Get response code
curl -s -o /dev/null -w "%{http_code}" http://localhost
# Monitor logs
tail -f /var/log/nginx/access.logBest Practices
- Use Nginx for high traffic scenarios
- Keep software updated
- Configure proper logging
- Use HTTPS/SSL
- Implement proper security headers
- Monitor performance metrics
- Use appropriate timeouts
- Implement rate limiting
- Keep configurations DRY (Don't Repeat Yourself)
- Document custom configurations