KB: 41467
·
更新时间:2018-11-16 20:26:51
Nginx 是一款广泛应用的 Web 服务器,常用于反向代理、负载均衡器以及 HTTP 缓存等。本文以 CentOS 6.8 为例,提供使用 Nginx 服务为 Linux 实例绑定多个域名的方法。您可以:
-
为每一个域名建立一个单独的配置文件,建议使用域名为配置文件命名。
-
远程连接并登录到 Linux 实例。
-
执行命令
cd /etc/nginx/conf.d打开 Nginx 服务配置文件目录。 -
执行命令
vi 您要创建的域名.conf创建域名规则配置文件,如示例中的vi www.server110.com.conf。 -
输入
i编辑新建的配置文件:-
为每一个域名建立一个单独的配置文件时输入以下内容:
server{listen 80; #监听端口设为 80。server_name www.server110.com; #绑定您的域名。index index.htm index.html index.php; #指定默认文件。root /home/www/server110.com; #指定网站根目录。include location.conf; #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。}
-
将多个域名规则写进一个共同的配置文件时输入以下内容:
server{listen 80; #监听端口设为 80。server_name www.server110.com; #绑定您的域名。index index.htm index.html index.php; #指定默认文件。root /home/www/server110.com; #指定网站根目录。include location.conf; #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。}server{listen 80; #监听端口设为 80。server_name msn.server111.com; #绑定您的域名。index index.htm index.html index.php; #指定默认文件。root /home/www/msn.server110.com; #指定网站根目录。include location.conf; #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。}
-
为无 WWW 前缀的域名配置规则并加 301 跳转时输入以下内容:
server{listen 80;server_name server110.com;rewrite ^/(.*) http://www.server110.com/$1 permanent;}
-
需要为域名添加 404 提示时输入以下内容:
server{listen 80; #监听端口设为 80。server_name www.server110.com; #绑定您的域名。index index.htm index.html index.php; #指定默认文件。root /home/www/server110.com; #指定网站根目录。include location.conf; #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。error_page 404 /404.html;}
-
-
按 Esc 退出编辑并输入
:wq保存退出。 -
执行命令
nginx -t检查配置是否有误,并按照报错提示修复错误。 -
执行命令
service nginx restart重启 Nginx 服务。 -
执行命令
service nginx reload重新载入 Nginx 服务。
