Learn how to optimize your website for better performance with our comprehensive guide covering key optimization techniques.
Images often account for most of the downloaded bytes on a webpage. Optimizing them is crucial for faster page loads and better user experience.
# Install ImageMagick
sudo apt-get install imagemagick
# Compress JPEG
convert input.jpg -quality 85 output.jpg
# Compress PNG
pngquant --quality=65-80 input.png
# Convert to WebP
cwebp -q 80 input.jpg -o output.webp
Proper caching configuration can dramatically improve load times for returning visitors and reduce server load.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "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-font-woff "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
</IfModule>
location ~* \.(jpg|jpeg|png|webp|gif|ico|css|js)$ {
expires 1y;
add_header Cache-Control "public, no-transform";
}
Minification reduces file sizes by removing unnecessary characters without changing functionality.
# Using npm tools
npm install -g uglify-js
npm install -g clean-css-cli
npm install -g html-minifier
# Minify JavaScript
uglifyjs input.js -o output.min.js
# Minify CSS
cleancss -o output.min.css input.css
# Minify HTML
html-minifier --collapse-whitespace input.html -o output.min.html
Enable compression to reduce the size of transmitted data between server and client.
gzip on;
gzip_vary on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml text/javascript;
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain text/html text/xml
AddOutputFilterByType DEFLATE text/css text/javascript
AddOutputFilterByType DEFLATE application/xml application/xhtml+xml
AddOutputFilterByType DEFLATE application/javascript application/x-javascript
AddOutputFilterByType DEFLATE application/json
</IfModule>
Content Delivery Networks improve loading times by serving content from locations closer to users.
<link rel="stylesheet" href="https://cdn.example.com/css/styles.min.css">
<script src="https://cdn.example.com/js/main.min.js"></script>
<img src="https://cdn.example.com/images/photo.jpg">
Regular monitoring helps identify and fix performance issues before they impact users.
# Install Lighthouse
npm install -g lighthouse
# Run audit
lighthouse https://example.com --output json --output html --output-path ./report
Mobile optimization is crucial with the majority of web traffic coming from mobile devices.
<meta name="viewport" content="width=device-width, initial-scale=1.0">
@media screen and (max-width: 768px) {
.container {
width: 100%;
padding: 0 15px;
}
}
Proper server configuration is fundamental for optimal website performance.
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
}