lanying56123 发表于 2017-12-23 14:01:07

nginx 配置多个主机

  我现在想配置 两个站点,通过域名来区分访问不同的网站目录
  比如有个域名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   ;

  sudoservice nginx restart    或者   sudo nginx -s>  打开浏览器输入google.com   显示的是 /opt/web/google/index.html 的内容,
  baidu.com   显示的是 /opt/web/baidu/index.html 的内容;
页: [1]
查看完整版本: nginx 配置多个主机