本文针对独立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 多域名配置问题了
版权属于: 三三世界-百宝箱
本文链接: http://www.33f.net/setup/muti_domain_on_niginx_with_same_IP_redirecting_same_directory.html
本文最后更新于2024年04月01日 ,已超过300天没有更新,若内容或图片失效,请留言反馈。
本文允许转载,但请在转载时请以超链接或其它形式标明文章出处
@Doug Shume it's ok for me , you can post here.
Saved as a favorite, I like your website!
If some one wishes to be updated with hottest technologies after that he must be visit this site and be up to date daily.
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!
Thanks to my father who shared with me regarding this webpage, this website is genuinely amazing.
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.
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 怎么办啊