zoukankan      html  css  js  c++  java
  • 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   ;

    sudo  service nginx restart    或者   sudo nginx -s reload

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

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

  • 相关阅读:
    关于WEB页面的强制分页打印问题
    iOS地图多个自定义大头针绘制核心代码
    PHP生成随机码
    安卓动画小结
    WKWebView遇到的问题汇总
    实现图片预加载的几种方式
    苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文教程
    markdown语法博客园测试
    Agreement has been updated--Edit Phone Number最便捷解决办法(不需要安全提示问题和双重认证)
    Python黑帽子:自动化内存取证
  • 原文地址:https://www.cnblogs.com/codeAB/p/6803511.html
Copyright © 2011-2022 走看看