+
objc
+
gentoo
+
express
โˆ‘
nvim
prettier
+
โˆ‘
::
prettier
+
$
hack
+
vault
+
+
saml
tf
+
+
+
+
remix
notepad++
+
+
rs
puppet
cosmos
ฯ€
0x
+
xgboost
elixir
html
+
+
+
kotlin
+
&&
node
+
dask
pnpm
puppet
+
+
*
phpstorm
+
cypress
s3
+
+
soap
+
0x
wasm
torch
+
aurelia
go
+
riot
+
graphql
choo
+
+
clj
+
โˆช
+
+
+
tcl
+
+
+
deno
gh
+
+
solid
โ‰ 
Back to Blog
๐ŸŒ Configuring Nginx Server Blocks on Alpine Linux: Simple Guide
Alpine Linux Nginx Beginner

๐ŸŒ Configuring Nginx Server Blocks on Alpine Linux: Simple Guide

Published Jun 17, 2025

Easy tutorial for beginners to set up multiple websites with Nginx server blocks on Alpine Linux. Perfect for web admins with step-by-step instructions and examples.

10 min read
0 views
Table of Contents

๐ŸŒ 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 needed
  • chown -R nginx:nginx: Sets Nginx user as owner of web files
  • chmod -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

CommandPurposeResult
๐Ÿ”ง nginx -tTest configurationโœ… Check for errors
๐Ÿ”„ nginx -s reloadReload configโœ… Apply changes
๐Ÿ“ ln -s available enabledEnable siteโœ… Activate server block
๐Ÿ“‹ `nginx -Tgrep 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

  1. Test configuration always ๐Ÿ“… - Use nginx -t before reloading
  2. Keep organized structure ๐ŸŒฑ - Use sites-available and sites-enabled pattern
  3. Monitor logs separately ๐Ÿค - Each site should have its own log files
  4. 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! ๐Ÿ’ซ