三三世界-百宝箱

文章 分类 标签
130 9 247

公告

暂无站点公告

[基础]nginx同一iP多网站配置方法(换域名同目录转向)

三三 2024-04-01 106 0条评论 服务器配置Linux使用网站建设 Nginx配置多网站多个域名换域名同目录转向

首页 / 正文

本文针对独立nginx配置用户,lnmp或者面板用户请忽略。

nginx绑定多个域名可以把多个域名规则写一个配置文件里,也可又分别建立多个域名配置文件,我一般为了管理方便,每个域名建一个文件,有些同类域名也可以写在一个总的配置文件里。

一、每个域名一个文件的写法

首先打开nginx域名配置文件存放目录:/usr/local/nginx/conf/servers(部分为vhost目录) ,如要绑定域名www.33f.net 则在此目录建一个文件:www.33f.net.conf然后在此文件中写规则,如:

server{
    listen 80;
    server_name www.33f.net; #绑定域名
    index index.htm index.html index.php; #默认文件
    root /home/www/33f.net; #网站根目录
    include location.conf; #调用其他规则,也可去除
}
  

然后重启nginx服务器,域名就绑定成功了nginx服务器重启命令:

/etc/init.d/nginx restart   #centos6.x及以前版本系统
systemctl restart nginx     #centos7及以后

这样可以每个域名做一些单独的设置。

二、一个文件多个域名的写法

如果几个域名展示的内容相同,那么一个文件添加多个域名的规则也是一样,只要把上面单个域名重复写下来就ok了,如:

server{
    listen 80;
    server_name www.33f.net example.com www.example.com; #绑定多个域名,空格分离
    index index.htm index.html index.php; #默认文件
    root /home/www/33f.net; #网站根目录
    include location.conf; #调用其他规则,也可去除
}
  

三、重新定向33f.net的所有网页到www.33f.net(域名可以变化)之下

作用是将访问 33f.net 的所有请求重定向到 www.33f.net 的相同路径下,并使用永久重定向。
这个可以用作你的网站变更了一个域名,但是你还不想损失之前某个页面的访问用户,这个时候可以将之前的网页转到另外一个域名的对应页面。

server {
    listen 80;
    server_name 33f.net www.33f.net;

    # 匹配所有路径并重定向到新域名的相同路径下
    location / {
        rewrite ^/(.*)$ http://33f.net/$1 permanent;
    }
}

四、添加404网页

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

server{
    listen 80;
    server_name www.33f.net; #绑定域名
    index index.htm index.html index.php; #默认文件
    root /home/www/33f.net; #网站根目录
    include location.conf; #调用其他规则,也可去除
    error_page 404 /404.html;
}

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

评论(0)

当前没有评论,还不快来留下第一个脚印吧


Copyright 2021 三三世界-百宝箱. All Rights Reserved.

最新评论

  • 三三

    @Doug Shume it's ok for me , you can post here.

  • refugiaguenther

    Saved as a favorite, I like your website!

  • josefa

    If some one wishes to be updated with hottest technologies after that he must be visit this site and be up to date daily.

  • Penzu

    Heello would you mind sharing which blog platform you're using? I'm planning to start my own blog in the near future but I'm having a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then moost blogs and I'm looking for something completely unique. P.S Apologies forr being off-topic butt I had to ask!

  • Yupoo Fendi

    Thanks to my father who shared with me regarding this webpage, this website is genuinely amazing.

  • Doug Shume

    Hi, I have an overflow of customers that I'd like to send to you but I want to make sure you can handle more leads, let me know if you'd like me to send you more info.

  • SuperWind

    zh.us.to 有效
    kms.03k.org 有效
    kms.chinancce.com
    kms.shuax.com 有效
    kms.dwhd.org 有效
    kms.luody.info 有效
    kms.digiboy.ir 有效
    kms.lotro.cc 有效
    www.zgbs.cc 有效
    cy2617.jios.org 有效

  • 三三

    @         权限问题,试试sudo 再加命令。

  •         

    你好提示Permission denied 怎么办啊

日历

2024年05月

   1234
567891011
12131415161718
19202122232425
262728293031 

文章目录

上一张 下一张