NGINX Virtual Host Configuration - Complete Guide
NGINX Virtual Host Configuration - Complete Guide
Table of Contents
1. Introduction to NGINX
2. Understanding Virtual Hosts
3. NGINX Installation and Basic Setup
4. Virtual Host Configuration Fundamentals
5. Server Blocks Configuration
6. Advanced Virtual Host Configurations
7. SSL/TLS Configuration
8. Troubleshooting and Common Issues
9. Best Practices and Security
10. Performance Optimization
Introduction to NGINX
NGINX (pronounced "engine-x") is a powerful, open-source web server that has gained
popularity for its high performance and efficiency. Originally developed by Igor Sysoev in
2004, it uses an event-driven, asynchronous architecture to handle a high volume of
connections simultaneously with minimal resource consumption. NGINX can also function as
a reverse proxy, load balancer, mail proxy, and HTTP cache.
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 1/15
2/6/25, 2:39 PM NGINX Virtual Host Configuration - Complete Guide
Virtual hosting is a method that allows multiple websites to be hosted on a single physical
server. Each site can have its own configuration, domain name, and resources.
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 2/15
2/6/25, 2:39 PM NGINX Virtual Host Configuration - Complete Guide
Before configuring virtual hosts, you need to install and set up NGINX on your system.
Installation on Ubuntu/Debian
Explanation: Ensures that your system has the latest package information.
2. Install NGINX:
Explanation: Downloads and installs the NGINX package along with its
dependencies.
3. Verify Installation:
nginx -v
Explanation: Confirms that NGINX is installed and shows the installed version.
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 3/15
2/6/25, 2:39 PM NGINX Virtual Host Configuration - Complete Guide
Installation on CentOS/RHEL
Explanation: Adds the Extra Packages for Enterprise Linux (EPEL) repository,
which contains additional software packages, including NGINX.
2. Install NGINX:
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 4/15
2/6/25, 2:39 PM NGINX Virtual Host Configuration - Complete Guide
/etc/nginx/
├── nginx.conf # Main configuration file
├── conf.d/ # Directory for general configurations
├── sites-available/ # Stores individual site configuration files
├── sites-enabled/ # Symlinks to active site configurations
├── modules-enabled/ # Enabled NGINX modules
└── modules-available/ # Available NGINX modules
Virtual hosts in NGINX are defined using server blocks. These blocks specify domain names,
document roots, and other settings.
http {
# Global settings
include mime.types; # Include file type mappings
default_type application/octet-stream;
sendfile on; # Optimize file transfers
keepalive_timeout 65; # Keep-alive timeout duration
Explanation: This is the core configuration structure where global settings and
virtual host files are included.
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 5/15
2/6/25, 2:39 PM NGINX Virtual Host Configuration - Complete Guide
server {
listen 80; # Listen on port 80 (HTTP)
server_name example.com www.example.com;# Define domain names
root /var/www/example.com; # Set the document root
index index.html index.htm; # Specify default index files
location / {
try_files $uri $uri/ =404; # Serve files or return 404 if n
}
# Logging
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
}
Explanation: Defines where the website files are located and how requests
should be handled.
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 6/15
2/6/25, 2:39 PM NGINX Virtual Host Configuration - Complete Guide
sudo nginx -t
5. Reload NGINX:
To host multiple websites on the same server, configure multiple server blocks.
location / {
try_files $uri $uri/ /index.php?$query_string;
}
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 7/15
2/6/25, 2:39 PM NGINX Virtual Host Configuration - Complete Guide
server_name blog.example.com;
root /var/www/blog.example.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
}
Configuring Subdomains
server {
listen 80;
server_name subdomain.example.com;
root /var/www/subdomain.example.com;
location / {
proxy_pass http://localhost:3000; # Forward requests to a backend s
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
URL Rewriting
URL rewriting can be used to redirect old URLs to new ones or create cleaner URLs.
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 8/15
2/6/25, 2:39 PM NGINX Virtual Host Configuration - Complete Guide
server {
listen 80;
server_name example.com;
location / {
rewrite ^/old-page$ /new-page permanent; # Permanent redirect
rewrite ^/blog/(\d{4})/(\d{2})/(.*)$ /blog/$3 last;
}
Load Balancing
upstream backend_servers {
least_conn; # Use least connections method
server backend1.example.com:8080;
server backend2.example.com:8080;
server backend3.example.com:8080 backup; # Backup server
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_servers; # Forward to backend servers
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 9/15
2/6/25, 2:39 PM NGINX Virtual Host Configuration - Complete Guide
Caching
http {
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=
server {
listen 80;
server_name example.com;
location / {
proxy_cache my_cache;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_5
proxy_cache_valid 200 60m;
proxy_pass http://backend;
}
}
}
SSL/TLS Configuration
server {
listen 443 ssl;
server_name example.com;
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 10/15
2/6/25, 2:39 PM NGINX Virtual Host Configuration - Complete Guide
add_header Strict-Transport-Security "max-age=31536000" always;
}
Explanation: Configures SSL/TLS with strong encryption and HSTS for security.
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
Analyzing Logs
Logs provide insights into server behavior and help diagnose issues.
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 11/15
2/6/25, 2:39 PM NGINX Virtual Host Configuration - Complete Guide
location ~ \.php$ {
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
Cause: The client is trying to upload a file that exceeds the default limit.
Solution:
client_max_body_size 100M;
server {
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src 'self'" always;
}
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 12/15
2/6/25, 2:39 PM NGINX Virtual Host Configuration - Complete Guide
Rate Limiting
Rate limiting prevents abuse by controlling the number of requests from a client.
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
location /login {
limit_req zone=one burst=5;
}
}
}
Explanation: Limits requests to one per second with a burst allowance of five.
Performance Optimization
http {
gzip on;
gzip_vary on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml application/json application/javascri
}
Browser Caching
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 13/15
2/6/25, 2:39 PM NGINX Virtual Host Configuration - Complete Guide
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
events {
worker_connections 1024; # Number of simultaneous connections per worker
multi_accept on; # Accept multiple connections at once
}
Conclusion
This guide has covered the essentials and advanced concepts of NGINX virtual host
configuration. You now understand how to:
Final Tips
1. Regularly Monitor Your Server: Keep an eye on logs and server performance.
2. Keep NGINX Updated: Install the latest security patches.
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 14/15
2/6/25, 2:39 PM NGINX Virtual Host Configuration - Complete Guide
NGINX is a highly flexible and robust tool that can be customized for various scenarios. For
advanced configurations and features, refer to the official NGINX documentation.
https://blog.geekinstitute.org/2024/11/nginx-virtual-host-configuration.html 15/15