随笔 - 347  文章 - 1 评论 - 44 阅读 - 201万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

 nginx绑定多个域名可又把多个域名规则写一个配置文件里,也可又分别建立多个域名配置文件,我一般为了管理方便,每个域名建一个文件,有些同类域名也可又写在一个总的配置文件里。
一、每个域名一个文件的写法
       首先打开nginx域名配置文件存放目录:/usr/local/nginx/conf/servers ,如要绑定域名www.itblood.com 则在此目录建一个文件:www.itblood.com.conf然后在此文件中写规则,如:

1
2
3
4
5
6
7
server{
    listen 80;
    server_name www.itblood.com; #绑定域名
    index index.htm index.html index.php; #默认文件
    root /home/www/itblood.com; #网站根目录
    include location.conf; #调用其他规则,也可去除
}

  

然后重起nginx服务器,域名就绑定成功了nginx服务器重起命令:/etc/init.d/nginx restart
二、一个文件多个域名的写法
一个文件添加多个域名的规则也是一样,只要把上面单个域名重复写下来就ok了,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server{
    listen 80;
    server_name www.itblood.com; #绑定域名
    index index.htm index.html index.php; #默认文件
    root /home/www/itblood.com; #网站根目录
    include location.conf; #调用其他规则,也可去除
}
 
server{
    listen 80;
    server_name msn.itblood.com; #绑定域名
    index index.htm index.html index.php; #默认文件
    root /home/www/msn.itblood.com; #网站根目录
    include location.conf; #调用其他规则,也可去除
}

  

三、不带www的域名加301跳转
如果不带www的域名要加301跳转,那也是和绑定域名一样,先绑定不带www的域名,只是不用写网站目录,而是进行301跳转,如:

1
2
3
4
5
6
server
{
    listen 80;
    server_name itblood.com;
    rewrite ^/(.*) http://www.itblood.com/$1 permanent;
}

  

四、添加404网页

       添加404网页,都可又直接在里面添加,如:

1
2
3
4
5
6
7
8
server{
    listen 80;
    server_name www.itblood.com; #绑定域名
    index index.htm index.html index.php; #默认文件
    root /home/www/itblood.com; #网站根目录
    include location.conf; #调用其他规则,也可去除
    error_page 404 /404.html;
}

  

学会上面四种规则方法,基本就可以自己独立解决nginx 多域名配置问题了

posted on   FreeSpider  阅读(62269)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示