使用 Nginx 为 Linux 实例绑定多个域名

 

KB: 41467

 · 

更新时间:2018-11-16 20:26:51

   

Nginx 是一款广泛应用的 Web 服务器,常用于反向代理、负载均衡器以及 HTTP 缓存等。本文以 CentOS 6.8 为例,提供使用 Nginx 服务为 Linux 实例绑定多个域名的方法。您可以:


  1. 远程连接并登录到 Linux 实例。

  2. 执行命令 cd /etc/nginx/conf.d 打开 Nginx 服务配置文件目录。

  3. 执行命令 vi 您要创建的域名.conf 创建域名规则配置文件,如示例中的 vi www.server110.com.conf

  4. 输入 i 编辑新建的配置文件:

    • 为每一个域名建立一个单独的配置文件时输入以下内容:

       
      1. server
      2. {
      3. listen 80; #监听端口设为 80。
      4. server_name www.server110.com; #绑定您的域名。
      5. index index.htm index.html index.php; #指定默认文件。
      6. root /home/www/server110.com; #指定网站根目录。
      7. include location.conf; #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。
      8. }
    • 将多个域名规则写进一个共同的配置文件时输入以下内容:

       
      1. server
      2. {
      3. listen 80; #监听端口设为 80。
      4. server_name www.server110.com; #绑定您的域名。
      5. index index.htm index.html index.php; #指定默认文件。
      6. root /home/www/server110.com; #指定网站根目录。
      7. include location.conf; #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。
      8. }
      9. server
      10. {
      11. listen 80; #监听端口设为 80。
      12. server_name msn.server111.com; #绑定您的域名。
      13. index index.htm index.html index.php; #指定默认文件。
      14. root /home/www/msn.server110.com; #指定网站根目录。
      15. include location.conf; #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。
      16. }
    • 为无 WWW 前缀的域名配置规则并加 301 跳转时输入以下内容:

       
      1. server
      2. {
      3. listen 80;
      4. server_name server110.com;
      5. rewrite ^/(.*) http://www.server110.com/$1 permanent;
      6. }
    • 需要为域名添加 404 提示时输入以下内容:

       
      1. server
      2. {
      3. listen 80; #监听端口设为 80。
      4. server_name www.server110.com; #绑定您的域名。
      5. index index.htm index.html index.php; #指定默认文件。
      6. root /home/www/server110.com; #指定网站根目录。
      7. include location.conf; #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。
      8. error_page 404 /404.html;
      9. }
  5. 按 Esc 退出编辑并输入 :wq 保存退出。

  6. 执行命令 nginx -t 检查配置是否有误,并按照报错提示修复错误。

  7. 执行命令 service nginx restart 重启 Nginx 服务。

  8. 执行命令 service nginx reload 重新载入 Nginx 服务。

猜你喜欢

转载自www.cnblogs.com/onetwo/p/10392979.html