码迷,mamicode.com
首页 > 其他好文 > 详细

nginx 配置多个主机

时间:2017-05-03 20:40:01      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:code   orm   pad   reload   name   try   span   sock   添加   

我现在想配置 两个站点,通过域名来区分访问不同的网站目录

比如有个域名  baidu.com   第二个域名  google.com,我有两个网站目录,

/opt/web/baidu;   /opt/web/google;

访问 baidu.com的时候访问  地一个目录的网站,google.com 访问第二个目录;

首先这两个域名都不是我的,为了达到讲解效果,先修改本地 hosts文件  ,让这两个域名暂时属于我;

1 sudo vim /etc/hosts
2 添加:
3 127.0.0.1   baidu.com   google.com

查看配置文件  /etc/nginx/nginx.conf (通过 apt安装的 nginx 配置文件位置),里面有一行

include /etc/nginx/sites-enabled/*;

如果前面有 #  注释了  就打开,有了这句话 ,所有放在  sites-enabled下面的文件都会被当作是配置文件来读取;

在下面新建两个文件  baidu.conf  ; google.conf ;

在 baidu.conf中 填充以下内容:

 1 server {
 2         listen 80  ;
 3 
 4         root /opt/web/baidu;
 5 
 6         # Add index.php to the list if you are using PHP
 7         index index.html index.php ;
 8 
 9         server_name baidu.com www.baidu.com;
10 
11         location / {
12                 try_files $uri $uri/ /index.php?$query_string;
13         }
14 
15         #
16         location ~ \.php$ {
17                 include snippets/fastcgi-php.conf;
18 
19                 fastcgi_pass unix:/run/php/php7.0-fpm.sock;
20         }
21 
22         location ~ /\.ht {
23                 allow all;
24         }
25 }

google.conf和上面一样,只需把  相应的  baidu  改为 google

通过 apt 安装的  nginx  在  sites-enabled下面会有一个默认的  default 文件  ,里面有一个默认的配置,会有影响,把里面内容全比注释了,或者删除;

好了 ,重启  nginx   ;

sudo  service nginx restart    或者   sudo nginx -s reload

打开浏览器  输入  google.com   显示的是 /opt/web/google/index.html 的内容,

baidu.com   显示的是 /opt/web/baidu/index.html 的内容;

 

nginx 配置多个主机

标签:code   orm   pad   reload   name   try   span   sock   添加   

原文地址:http://www.cnblogs.com/codeAB/p/6803511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!