๐ Configuring Nginx Server Blocks on Alpine Linux: Simple Guide
Letโs set up multiple websites with Nginx server blocks on Alpine Linux! ๐ This tutorial shows you how to host different domains and websites on one server easily. Perfect for managing multiple sites! ๐
๐ค What are Nginx Server Blocks?
Nginx server blocks are like separate rooms in a building! Each room (server block) can host a different website, all running on the same server.
Nginx server blocks are like:
- ๐ Virtual hosting that lets one server run multiple websites
- ๐ฏ Smart routing that sends visitors to the right website based on domain name
- ๐ก Efficient resource sharing where multiple sites use the same server
๐ฏ What You Need
Before we start, you need:
- โ Alpine Linux with Nginx already installed
- โ Domain names pointing to your server (or local testing setup)
- โ Basic knowledge of Nginx configuration
- โ Root access or sudo privileges
๐ Step 1: Install and Prepare Nginx
Install Nginx Web Server
Letโs make sure Nginx is installed and running! ๐
What weโre doing: Installing Nginx and checking its status on Alpine Linux.
# Update package list
apk update
# Install Nginx
apk add nginx
# Start Nginx service
rc-service nginx start
# Enable Nginx to start at boot
rc-update add nginx default
# Check Nginx status
rc-service nginx status
What this does: ๐ Installs and starts Nginx web server on your system.
Example output:
nginx [started]
What this means: Nginx is running and ready to serve websites! โ
Check Nginx Configuration
Letโs understand the Nginx configuration structure! ๐ฏ
What weโre doing: Exploring Nginx configuration files and directories.
# Check main Nginx configuration
cat /etc/nginx/nginx.conf
# List Nginx configuration directories
ls -la /etc/nginx/
# Check if sites directories exist (create if needed)
mkdir -p /etc/nginx/sites-available
mkdir -p /etc/nginx/sites-enabled
# Check default web directory
ls -la /var/www/
You should see directories like:
drwxr-xr-x 2 root root 4096 Jun 17 10:00 sites-available
drwxr-xr-x 2 root root 4096 Jun 17 10:00 sites-enabled
๐ก Important Tips
Tip: Server blocks are similar to Apacheโs virtual hosts! ๐ก
Warning: Always test Nginx configuration before restarting the service! โ ๏ธ
๐ ๏ธ Step 2: Create Website Directories
Set Up Directory Structure
Letโs create directories for our websites! This keeps everything organized! ๐
What weโre doing: Creating separate directories for each website weโll host.
# Create web directories for different sites
mkdir -p /var/www/site1.example.com/html
mkdir -p /var/www/site2.example.com/html
mkdir -p /var/www/blog.example.com/html
# Set proper ownership
chown -R nginx:nginx /var/www/
# Set proper permissions
chmod -R 755 /var/www/
# Create sample index files
echo "<h1>Welcome to Site 1! ๐</h1>" > /var/www/site1.example.com/html/index.html
echo "<h1>Welcome to Site 2! ๐</h1>" > /var/www/site2.example.com/html/index.html
echo "<h1>Welcome to Blog! ๐</h1>" > /var/www/blog.example.com/html/index.html
Code explanation:
mkdir -p
: Creates directories and parent directories if neededchown -R nginx:nginx
: Sets Nginx user as owner of web fileschmod -R 755
: Sets proper read/execute permissions for web serving- Sample HTML files help us test each site individually
What this means: Each website has its own dedicated directory! ๐
Create Log Directories
Letโs set up separate log files for each website! ๐ฎ
What weโre doing: Creating log directories to track each website separately.
# Create log directories
mkdir -p /var/log/nginx/site1.example.com
mkdir -p /var/log/nginx/site2.example.com
mkdir -p /var/log/nginx/blog.example.com
# Set proper permissions for logs
chown -R nginx:nginx /var/log/nginx/
chmod -R 755 /var/log/nginx/
# Check directory structure
tree /var/www/ /var/log/nginx/ 2>/dev/null || ls -la /var/www/ /var/log/nginx/
What this does: Each website will have separate access and error logs! โ
๐ง Step 3: Create Server Block Configurations
Configure First Server Block
Letโs create our first server block configuration! This is exciting! ๐
What weโre doing: Creating a server block configuration for site1.example.com.
# Create server block configuration for site1
cat > /etc/nginx/sites-available/site1.example.com << 'EOF'
server {
listen 80;
listen [::]:80;
server_name site1.example.com www.site1.example.com;
root /var/www/site1.example.com/html;
index index.html index.htm;
# Logging
access_log /var/log/nginx/site1.example.com/access.log;
error_log /var/log/nginx/site1.example.com/error.log;
# Basic location block
location / {
try_files $uri $uri/ =404;
}
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
# Disable server tokens
server_tokens off;
# Favicon handling
location = /favicon.ico {
log_not_found off;
access_log off;
}
# Robots.txt handling
location = /robots.txt {
log_not_found off;
access_log off;
allow all;
}
# Static file caching
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
}
EOF
echo "First server block created! ๐"
What this does: Creates a complete server block with security and performance features! โ
Configure Second Server Block
Letโs create another server block for our second website! ๐
What weโre doing: Creating a server block configuration for site2.example.com.
# Create server block configuration for site2
cat > /etc/nginx/sites-available/site2.example.com << 'EOF'
server {
listen 80;
listen [::]:80;
server_name site2.example.com www.site2.example.com;
root /var/www/site2.example.com/html;
index index.html index.htm index.php;
# Logging
access_log /var/log/nginx/site2.example.com/access.log;
error_log /var/log/nginx/site2.example.com/error.log;
# Basic location block
location / {
try_files $uri $uri/ =404;
}
# PHP processing (if needed)
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
# Disable server tokens
server_tokens off;
# Deny access to hidden files
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
}
EOF
echo "Second server block created! ๐"
Create Blog Server Block
Letโs add a blog server block with special configuration! ๐ฏ
What weโre doing: Creating a server block for blog.example.com with blog-specific settings.
# Create server block configuration for blog
cat > /etc/nginx/sites-available/blog.example.com << 'EOF'
server {
listen 80;
listen [::]:80;
server_name blog.example.com www.blog.example.com;
root /var/www/blog.example.com/html;
index index.html index.htm;
# Logging
access_log /var/log/nginx/blog.example.com/access.log;
error_log /var/log/nginx/blog.example.com/error.log;
# Basic location block
location / {
try_files $uri $uri/ =404;
}
# API endpoint example
location /api/ {
proxy_pass http://localhost:3000/;
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;
}
# Custom error pages
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/blog.example.com/html;
}
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
# GZIP compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
}
EOF
echo "Blog server block created! ๐"
What this means: You now have three different server blocks configured! ๐
โก Step 4: Enable Server Blocks
Enable Sites
Letโs enable our server blocks by creating symbolic links! ๐
What weโre doing: Activating server blocks by linking them to sites-enabled directory.
# Enable server blocks by creating symbolic links
ln -s /etc/nginx/sites-available/site1.example.com /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/site2.example.com /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/blog.example.com /etc/nginx/sites-enabled/
# Check enabled sites
ls -la /etc/nginx/sites-enabled/
# Verify symbolic links are correct
echo "Enabled sites:"
ls -la /etc/nginx/sites-enabled/ | grep "^l"
Expected output:
lrwxrwxrwx 1 root root 39 Jun 17 10:00 site1.example.com -> /etc/nginx/sites-available/site1.example.com
lrwxrwxrwx 1 root root 39 Jun 17 10:00 site2.example.com -> /etc/nginx/sites-available/site2.example.com
lrwxrwxrwx 1 root root 38 Jun 17 10:00 blog.example.com -> /etc/nginx/sites-available/blog.example.com
What this does: Tells Nginx to load and use these server blocks! โ
Update Main Nginx Configuration
Letโs make sure Nginx loads our server blocks! ๐ฎ
What weโre doing: Modifying main Nginx configuration to include our server blocks.
# Backup original nginx.conf
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
# Add include directive for sites-enabled
cat >> /etc/nginx/nginx.conf << 'EOF'
# Include server blocks
include /etc/nginx/sites-enabled/*;
EOF
# Alternative: Edit nginx.conf to include sites-enabled in http block
# Make sure this line is inside the http { } block
grep -q "include /etc/nginx/sites-enabled/\*;" /etc/nginx/nginx.conf || \
sed -i '/http {/a\ include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf
echo "Nginx configuration updated! โ๏ธ"
What this means: Nginx will now load all enabled server blocks! ๐
๐ Step 5: Test Configuration
Test Nginx Configuration
Letโs make sure our configuration is correct before restarting! ๐
What weโre doing: Testing Nginx configuration for syntax errors and issues.
# Test Nginx configuration syntax
nginx -t
# Check configuration details
nginx -T | grep server_name
# Verify server blocks are loaded
nginx -T | grep -A 5 -B 5 "server_name site1.example.com"
# Check for duplicate server_name directives
nginx -T | grep server_name | sort
Expected output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
What this means: Your configuration is valid and ready to use! โ
Reload Nginx
Letโs apply our new configuration! ๐
What weโre doing: Reloading Nginx to apply the new server block configurations.
# Reload Nginx to apply new configuration
nginx -s reload
# Alternative: restart Nginx service
rc-service nginx restart
# Check Nginx status
rc-service nginx status
# Verify Nginx is listening on port 80
netstat -tuln | grep :80
You should see:
nginx [started]
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
Awesome! Nginx is running with your new server blocks! ๐
๐ Step 6: Test Your Websites
Test with Local Hosts File
Letโs test our server blocks using the hosts file! ๐ฏ
What weโre doing: Adding entries to /etc/hosts for local testing of our domains.
# Backup hosts file
cp /etc/hosts /etc/hosts.backup
# Add test domains to hosts file
cat >> /etc/hosts << 'EOF'
127.0.0.1 site1.example.com
127.0.0.1 site2.example.com
127.0.0.1 blog.example.com
EOF
# Test each website with curl
echo "Testing Site 1:"
curl -H "Host: site1.example.com" http://localhost/
echo "Testing Site 2:"
curl -H "Host: site2.example.com" http://localhost/
echo "Testing Blog:"
curl -H "Host: blog.example.com" http://localhost/
Expected output:
Testing Site 1:
<h1>Welcome to Site 1! ๐</h1>
Testing Site 2:
<h1>Welcome to Site 2! ๐</h1>
Testing Blog:
<h1>Welcome to Blog! ๐</h1>
What this means: All your server blocks are working perfectly! ๐
Test with Different Methods
Letโs test in multiple ways to make sure everything works! ๐
What weโre doing: Testing server blocks with various methods and checking logs.
# Test with wget
wget -O - --header="Host: site1.example.com" http://localhost/
# Check access logs
echo "Site 1 access log:"
tail -5 /var/log/nginx/site1.example.com/access.log
echo "Site 2 access log:"
tail -5 /var/log/nginx/site2.example.com/access.log
# Test error handling
curl -H "Host: site1.example.com" http://localhost/nonexistent-page
# Check error logs
echo "Error log check:"
tail -5 /var/log/nginx/site1.example.com/error.log
What this does: Verifies that routing and logging work correctly for each site! โ
๐ Quick Server Block Commands Table
Command | Purpose | Result |
---|---|---|
๐ง nginx -t | Test configuration | โ Check for errors |
๐ nginx -s reload | Reload config | โ Apply changes |
๐ ln -s available enabled | Enable site | โ Activate server block |
๐ `nginx -T | grep server_name` | List server names |
๐ฎ Practice Time!
Letโs practice what you learned! Try these simple examples:
Example 1: Add New Server Block ๐ข
What weโre doing: Creating and enabling a new server block for a fourth website.
# Create directory for new site
mkdir -p /var/www/shop.example.com/html
echo "<h1>Welcome to Shop! ๐</h1>" > /var/www/shop.example.com/html/index.html
# Create server block configuration
cat > /etc/nginx/sites-available/shop.example.com << 'EOF'
server {
listen 80;
server_name shop.example.com;
root /var/www/shop.example.com/html;
index index.html;
access_log /var/log/nginx/shop.example.com/access.log;
error_log /var/log/nginx/shop.example.com/error.log;
location / {
try_files $uri $uri/ =404;
}
}
EOF
# Enable the new site
ln -s /etc/nginx/sites-available/shop.example.com /etc/nginx/sites-enabled/
# Test and reload
nginx -t && nginx -s reload
echo "New server block added! ๐"
What this does: Shows you how to quickly add new websites! ๐
Example 2: Server Block Management Script ๐ก
What weโre doing: Creating a script to manage server blocks easily.
# Create server block management script
cat > manage-sites.sh << 'EOF'
#!/bin/sh
case "$1" in
list)
echo "๐ Available sites:"
ls /etc/nginx/sites-available/
echo "โ
Enabled sites:"
ls /etc/nginx/sites-enabled/
;;
enable)
if [ -f "/etc/nginx/sites-available/$2" ]; then
ln -s "/etc/nginx/sites-available/$2" "/etc/nginx/sites-enabled/"
echo "โ
Enabled $2"
nginx -t && nginx -s reload
else
echo "โ Site $2 not found"
fi
;;
disable)
if [ -L "/etc/nginx/sites-enabled/$2" ]; then
rm "/etc/nginx/sites-enabled/$2"
echo "โ Disabled $2"
nginx -s reload
else
echo "โ Site $2 not enabled"
fi
;;
*)
echo "Usage: $0 {list|enable|disable} [site-name]"
;;
esac
EOF
chmod +x manage-sites.sh
./manage-sites.sh list
What this does: Gives you a handy tool for managing server blocks! ๐
๐จ Fix Common Problems
Problem 1: Server blocks not working โ
What happened: All domains show the same website (default server). How to fix it: Check server_name directives and DNS/hosts configuration.
# Check for default server conflicts
nginx -T | grep -A 3 -B 3 "default_server"
# Verify server_name directives
nginx -T | grep server_name
# Test with explicit Host header
curl -H "Host: site1.example.com" http://localhost/
# Check hosts file entries
grep example.com /etc/hosts
Problem 2: Permission denied errors โ
What happened: Nginx canโt access website files. How to fix it: Check file ownership and permissions.
# Fix ownership
chown -R nginx:nginx /var/www/
# Fix permissions
chmod -R 755 /var/www/
# Check directory permissions
ls -la /var/www/
# Check error logs
tail -f /var/log/nginx/error.log
Donโt worry! Server block issues are usually simple configuration problems! ๐ช
๐ก Simple Tips
- Test configuration always ๐
- Use
nginx -t
before reloading - Keep organized structure ๐ฑ - Use sites-available and sites-enabled pattern
- Monitor logs separately ๐ค - Each site should have its own log files
- Use descriptive names ๐ช - Server block files should match domain names
โ Check Everything Works
Letโs make sure all server blocks are working perfectly:
# Complete server blocks system check
echo "=== Nginx Server Blocks System Check ==="
echo "1. Nginx configuration test:"
nginx -t
echo "2. Available server blocks:"
ls /etc/nginx/sites-available/
echo "3. Enabled server blocks:"
ls /etc/nginx/sites-enabled/
echo "4. Server names configured:"
nginx -T | grep server_name | sort -u
echo "5. Test each website:"
for site in site1.example.com site2.example.com blog.example.com; do
echo "Testing $site:"
curl -s -H "Host: $site" http://localhost/ | head -1
done
echo "6. Log files created:"
find /var/log/nginx/ -name "*.log" | grep -E "(site1|site2|blog)" | head -5
echo "All server blocks are operational! โ
"
Good output shows:
=== Nginx Server Blocks System Check ===
1. Nginx configuration test:
nginx: configuration file /etc/nginx/nginx.conf test is successful
2. Available server blocks:
site1.example.com site2.example.com blog.example.com
3. Enabled server blocks:
site1.example.com site2.example.com blog.example.com
5. Test each website:
Testing site1.example.com:
<h1>Welcome to Site 1! ๐</h1>
Testing site2.example.com:
<h1>Welcome to Site 2! ๐</h1>
Testing blog.example.com:
<h1>Welcome to Blog! ๐</h1>
All server blocks are operational! โ
๐ What You Learned
Great job! Now you can:
- โ Install and configure Nginx on Alpine Linux
- โ Create and organize multiple server block configurations
- โ Set up separate directories and logs for each website
- โ Enable and disable server blocks easily
- โ Test server block configurations thoroughly
- โ Handle multiple domains on a single server
- โ Implement security headers and performance optimizations
- โ Troubleshoot common server block issues
๐ฏ Whatโs Next?
Now you can try:
- ๐ Learning about SSL/TLS configuration for server blocks
- ๐ ๏ธ Setting up load balancing between multiple backend servers
- ๐ค Implementing advanced caching and compression strategies
- ๐ Exploring Nginx modules for enhanced functionality!
Remember: Server blocks are the foundation of hosting multiple websites efficiently! Youโre doing amazing! ๐
Keep experimenting and youโll become an Nginx configuration expert! ๐ซ