在 Nginx 中指向域名的配置主要有以下几种方式:

1. 单域名配置:
nginx
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html index.htm;
}
}
这种配置将 `example.com` 指向 `/var/www/html` 目录下的文件。
2. 多域名配置:
nginx
server {
listen 80;
server_name example.com www.example.com;
location / {
root /var/www/example;
index index.html index.htm;
}
}
server {
listen 80;
server_name example.net www.example.net;
location / {
root /var/www/example-net;
index index.html index.htm;
}
}
这种配置将 `example.com` 和 `www.example.com` 指向 `/var/www/example` 目录,`example.net` 和 `www.example.net` 指向 `/var/www/example-net` 目录。
3. 使用域名别名:
nginx
server {
listen 80;
server_name example.com www.example.com example.net www.example.net;
location / {
root /var/www/html;
index index.html index.htm;
}
}
这种配置将 `example.com`、`www.example.com`、`example.net` 和 `www.example.net` 都指向 `/var/www/html` 目录。
4. 重定向:
nginx
server {
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html index.htm;
}
}
这种配置将 `www.example.com` 重定向到 `example.com`,然后 `example.com` 指向 `/var/www/html` 目录。
以上是 Nginx 中指向域名的几种常见配置方式,可以根据具体需求选择合适的方式。

查看详情

查看详情