Web Server Performance Optimization
= Web Server Performance Optimization = This guide covers essential techniques for optimizing the performance of your web server, focusing on Gzip compression, caching headers, Keep-Alive settings, and tuning worker processes. Improving these aspects can significantly reduce page load times, decrease bandwidth usage, and enhance the overall user experience for your website visitors. These optimizations are particularly effective on dedicated servers where you have full control over the server environment, such as those offered by PowerVPS.
Prerequisites
Before you begin, ensure you have the following:- A running Linux server with root or sudo privileges.
- A web server installed (e.g., Apache or Nginx).
- Basic understanding of server configuration files.
- SSH access to your server.
- Access to your website's files.
Enabling Gzip Compression
Gzip compression reduces the size of your web pages (HTML, CSS, JavaScript) before sending them to the browser, leading to faster download times.Apache
1. **Install the `mod_deflate` module (if not already installed):**sudo apt update sudo apt install apache2-utils libapache2-mod-deflate(For RHEL/CentOS-based systems, it's usually enabled by default. If not, you might need to use `sudo yum install mod_deflate` or `sudo dnf install mod_deflate` and then `sudo a2enmod deflate`.)
2. **Enable `mod_deflate`:**
sudo a2enmod deflate
3. **Configure `mod_deflate`:**
Edit your Apache configuration file (e.g., `/etc/apache2/apache2.conf` or a virtual host file in `/etc/apache2/sites-available/`). Add the following lines within the `<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, XML, and text files.
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE image/svg+xml
# Netscape Navigator 4.x has some privacy issues with Gzip. BrowserMatch ^Mozilla/4\.0[678] nocompress # MSIE 5, 6, and Windows ME also have some privacy issues with Gzip. BrowserMatch ^MSIE [1-6] nocompress </IfModule>
4. **Restart Apache:**
sudo systemctl restart apache2
Nginx
1. **Edit your Nginx configuration file** (e.g., `/etc/nginx/nginx.conf` or a server block file in `/etc/nginx/sites-available/`). Add or ensure the following lines are present within the `http` or `server` block:gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml; gzip_disable "msie6"; # Disable for MSIE 6
2. **Test Nginx configuration and reload:**
sudo nginx -t sudo systemctl reload nginx
Configuring Caching Headers
Caching headers instruct the browser on how long to cache static assets (images, CSS, JS). This reduces the number of requests the browser needs to make to your server.Apache
Edit your Apache configuration file (e.g., virtual host file). Add the following lines within your `<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/json "access plus 1 month"
ExpiresByType text/html "access plus 1 day"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType image/webp "access plus 1 year"
</IfModule>
<IfModule mod_headers.c>
# Set Cache-Control for static assets
<FilesMatch "\.(cssjs|jpg|jpeg|png|gif|svg|ico|webp|woff|woff2|ttf|eot)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>
# Set Cache-Control for HTML
<FilesMatch "\.html$">
Header set Cache-Control "max-age=86400, public"
</FilesMatch>
</IfModule>Restart Apache if you made changes to `apache2.conf` or enabled modules:
sudo systemctl restart apache2
Nginx
Edit your Nginx configuration file (e.g., server block file). Add the following within your `server` block:location ~* \.(cssjs|jpg|jpeg|png|gif|svg|ico|webp|woff|woff2|ttf|eot)$ { expires 1y; add_header Cache-Control "public"; } location ~* \.html$ { expires 1d; add_header Cache-Control "public"; }
Test Nginx configuration and reload:
sudo nginx -t sudo systemctl reload nginx
Enabling HTTP Keep-Alive
HTTP Keep-Alive allows a single TCP connection to be reused for multiple HTTP requests, reducing the overhead of establishing new connections for each resource.Apache
Edit your Apache configuration file (e.g., `/etc/apache2/apache2.conf` or `/etc/httpd/conf/httpd.conf`). Ensure the following directives are set within the `<IfModule prefork.c>
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
</IfModule>
<IfModule worker.c>
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
</IfModule>
Restart Apache:
sudo systemctl restart apache2
Nginx
Nginx uses Keep-Alive by default. The relevant directive is `keepalive_timeout`. Edit your Nginx configuration file (e.g., `/etc/nginx/nginx.conf`). Ensure the `keepalive_timeout` directive is set within the `http` block:http {
# ... other http directives
keepalive_timeout 65;
keepalive_requests 1000;
# ...
}
Test Nginx configuration and reload:
sudo nginx -t sudo systemctl reload nginx
Tuning Worker Processes
Web servers use worker processes to handle incoming requests. Tuning the number of worker processes can significantly impact performance, especially under heavy load. This is where having a powerful dedicated server from providers like PowerVPS with ample CPU and RAM becomes crucial.Apache
Apache uses Multi-Processing Modules (MPMs) to manage worker processes. The most common are `prefork`, `worker`, and `event`. `event` is generally the most performant for high-traffic sites.sudo apache2ctl -m
<IfModule mpm_event_module>
ServerLimit 25
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestWorkers 150 # This is a crucial setting. Calculate based on RAM.
MaxConnectionsPerChild 0
</IfModule>
sudo a2dismod mpm_prefork # or mpm_worker sudo a2enmod mpm_event sudo systemctl restart apache2
Nginx
Nginx uses an event-driven, asynchronous architecture. The primary directive for tuning is `worker_processes`.Edit your Nginx configuration file (e.g., `/etc/nginx/nginx.conf`).
worker_processes auto; # or set to the number of CPU cores
You can also tune `worker_connections`:
events {
worker_connections 1024; # Default is 512. Increase if you expect many concurrent connections.
}
Test Nginx configuration and reload:
sudo nginx -t sudo systemctl reload nginx
Troubleshooting
Related Articles
Category:Web Server Setup Category:Performance Tuning Category:Linux Administration