在Windows环境下配置Nginx支持多域名(虚拟主机)是一个常见的需求。以下是详细的配置步骤:
编辑nginx.conf文件
C:\nginx\conf\nginx.conf
http
块内添加 include
指令引入虚拟主机配置http {
# 其他配置...
include vhosts/*.conf;
}
创建虚拟主机配置文件
conf
目录下创建 vhosts
文件夹example1.conf
和 example2.conf
example1.conf
server {
listen 80;
server_name example1.com www.example1.com;
root C:/websites/example1;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
example2.conf
server {
listen 80;
server_name example2.com www.example2.com;
root C:/websites/example2;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 443 ssl;
server_name secure.example.com;
ssl_certificate C:/ssl/secure.example.com.crt;
ssl_certificate_key C:/ssl/secure.example.com.key;
root C:/websites/secure;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
如果用于本地开发,可以修改hosts文件(C:\Windows\System32\drivers\etc\hosts
)添加:
127.0.0.1 example1.com
127.0.0.1 example2.com
127.0.0.1 secure.example.com
测试配置是否正确
nginx -t
重新加载配置(不中断服务)
nginx -s reload
重启Nginx
nginx -s stop
nginx
/
而不是反斜杠 \
server
块的 server_name
唯一server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
upstream backend {
server 127.0.0.1:8001;
server 127.0.0.1:8002;
}
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://backend;
}
}
通过以上配置,您可以在Windows环境下轻松实现Nginx的多域名支持。根据实际需求调整配置即可。