Why Binding Port and Domain is Necessary?¶
On a single server, you may run multiple websites or services (e.g., personal blog, company website, backend management system). A domain name is the “URL” users enter (e.g., www.example.com), while a port acts as a “house number” for different services on the server (e.g., port 80 is the default HTTP port, 8080 is a common custom port). Nginx uses the combination of “port + domain” to map different domains or ports to different websites/services, enabling the “one server, multiple websites” effect.
Prerequisites¶
- Server Environment: Ensure Nginx is installed on your server (e.g., Ubuntu/CentOS for Linux).
- Installation on Ubuntu:sudo apt update && sudo apt install nginx
- Installation on CentOS:sudo yum install nginx - Domain & IP: You need a registered domain (e.g.,
www.myblog.com) and point its A record to the server’s public IP via your domain provider’s console. - Test Access: Verify the server is reachable by visiting its IP directly (e.g.,
http://192.168.1.100), and you should see Nginx’s default welcome page.
Understanding Nginx Configuration Files¶
Nginx’s core configuration files are in /etc/nginx/. Key directives for virtual hosts (website configurations for different domains/ports) include:
- listen: Specifies the port Nginx listens on (default: 80).
- server_name: Binds the domain name (supports multiple domains separated by spaces).
- root: Path to website files (e.g., /var/www/html/myblog).
- index: Default homepage filename (e.g., index.html).
Practical Steps: Binding Domain and Port¶
Below are two scenarios to configure different domain/port bindings:
Scenario 1: Same Domain, Different Ports¶
Suppose you have www.myblog.com and want it to support both 80 (HTTP) and 443 (HTTPS) ports.
- Create/Modify Configuration File
Virtual host configurations are typically in/etc/nginx/conf.d/(Ubuntu/CentOS):
- Create a file:sudo nano /etc/nginx/conf.d/myblog.conf
- Add the following:
server {
listen 80; # Listen on HTTP port 80
server_name www.myblog.com; # Bind domain www.myblog.com
root /var/www/html/myblog; # Website file directory (create this first)
index index.html; # Default homepage
}
server {
listen 443 ssl; # Listen on HTTPS port 443 (ssl enables HTTPS)
server_name www.myblog.com; # Same domain
root /var/www/html/myblog; # Website file directory
index index.html;
# SSL certificate paths (place certs in /etc/nginx/ssl/ first)
ssl_certificate /etc/nginx/ssl/myblog.crt;
ssl_certificate_key /etc/nginx/ssl/myblog.key;
}
- Save and exit (
Ctrl+O→ Enter,Ctrl+X).
Scenario 2: Different Domains, Different Ports¶
Suppose you have two domains: www.myblog.com (port 80) and blog.myblog.com (port 8080).
- Create Configuration File
In/etc/nginx/conf.d/, createblog.conf:
server {
listen 80; # Listen on port 80
server_name www.myblog.com; # First domain
root /var/www/html/myblog; # Website directory
index index.html;
}
server {
listen 8080; # Listen on custom port 8080
server_name blog.myblog.com; # Second domain
root /var/www/html/blog; # Blog directory
index index.html;
}
Verification and Startup¶
- Check Configuration Syntax: Run
sudo nginx -t. If it returnssyntax is okandtest is successful, the config is valid. - Restart Nginx: Apply changes with
sudo systemctl restart nginx.
Testing Access¶
- Visit Domains/Ports: Use a browser to test:
- Scenario 1:www.myblog.com(HTTP) orhttps://www.myblog.com(HTTPS).
- Scenario 2:www.myblog.com(port 80) orblog.myblog.com:8080(port 8080).
- Troubleshooting: If access fails, check:- Domain resolution (use
ping www.myblog.comto verify IP). - Firewall settings (open ports in security groups for cloud servers like Aliyun/Tencent Cloud).
- Domain resolution (use
Common Issues & Solutions¶
- Nginx Startup Failure: Run
sudo nginx -tto check syntax errors (e.g., incomplete files). - Domain Not Resolving: Wait for DNS propagation (10 minutes to 24 hours) or test with
nslookup www.myblog.com. - Port Conflict: If 80 is occupied (e.g., by Apache), change
listenport or stop the conflicting service.
Summary¶
By using Nginx’s listen and server_name directives, you can easily bind domains and ports for multiple websites. The core is understanding virtual host configuration, ensuring domain resolution to the server IP, and validating configs with nginx -t. Practice to master multi-site management!
(Note: This guide uses Linux; Windows configurations are similar but adjust file paths and commands as needed.)