Nginx 是一个高性能的 HTTP 和反向代理服务器,广泛用于网站和应用的部署。设置多个域名可以通过配置虚拟主机(Virtual Host)来实现。以下是详细的基础概念、优势、类型、应用场景以及配置示例。
blog.example.com
和 shop.example.com
。假设我们要为 example.com
和 anotherdomain.com
设置虚拟主机,以下是Nginx配置文件的示例:
# 定义example.com的虚拟主机
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_log /var/log/nginx/example.com-error.log;
access_log /var/log/nginx/example.com-access.log;
}
# 定义anotherdomain.com的虚拟主机
server {
listen 80;
server_name anotherdomain.com www.anotherdomain.com;
root /var/www/anotherdomain.com;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_log /var/log/nginx/anotherdomain.com-error.log;
access_log /var/log/nginx/anotherdomain.com-access.log;
}
root
指令指向的目录存在且包含正确的默认文件(如index.html
)。sudo nginx -s reload
命令重新加载配置。通过以上步骤,你可以成功地在Nginx中设置多个域名,并解决常见的配置问题。
领取专属 10元无门槛券
手把手带您无忧上云