为了账号安全,请及时绑定邮箱和手机立即绑定

linux中nginx如何配置一个ip多个域名

linux中nginx如何配置一个ip多个域名

开心每一天1111 2019-02-15 11:14:14
linux中nginx如何配置一个ip多个域名
查看完整描述

1 回答

?
撒科打诨

TA贡献1934条经验 获得超2个赞

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

1

2

3

4

5

6

7

{

listen 80;

server_name www.rodine.org; #绑定域名

index index.htm index.html index.php; #默认文件

root /home/www/rodine.org; #网站根目录

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.rodine.org; #绑定域名

index index.htm index.html index.php; #默认文件

root /home/www/rodine.org; #网站根目录

include location.conf; #调用其他规则,也可去除

}server

{

listen 80;

server_name msn.rodine.org; #绑定域名

index index.htm index.html index.php; #默认文件

root /home/www/msn.rodine.org; #网站根目录

include location.conf; #调用其他规则,也可去除

}

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

1

2

3

4

5

6

server

{

listen 80;

server_namerodine.org;

rewrite ^/(.*) http://www.rodine.org/$1 permanent;

}

四、添加404网页

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

1

2

3

4

5

6

7

8

9

server

{

listen 80;

server_name www.rodine.org; #绑定域名

index index.htm index.html index.php; #默认文件

root /home/www/rodine.org; #网站根目录

include location.conf; #调用其他规则,也可去除

error_page 404 /404.html;

}

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


 


查看完整回答
反对 回复 2019-02-17
  • 1 回答
  • 0 关注
  • 693 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信