zoukankan      html  css  js  c++  java
  • nginx 配置虚拟主机( 基于域名 )

    一、创建网站目录及文件:

    [root@localhost data]# tree /data
    /data
    └── wwwroot
        ├── www.1.com
        │   └── index.html
        └── www.2.com
            └── index.html

    二、修改nginx.conf:

    [root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf
    
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        keepalive_timeout  65;
        include vhost/*.conf;   #vhost目录下会包含所有的虚拟主机的配置文件
    }

    三、创建虚拟主机的配置文件目录:

    [root@localhost conf]mkdir /usr/local/nginx/conf/vhost

    四、创建虚拟主机配置文件:

    [root@localhost nginx]# vim /usr/local/nginx/conf/vhost/www.1.com.conf 
    server{
        listen 80;
        server_name 1.com www.1.com;
        index index.html;
        root /data/wwwroot/www.1.com;
    }
    [root@localhost nginx]# vim /usr/local/nginx/conf/vhost/www.2.com.conf  
    server{
        listen 80;
        server_name 2.com www.2.com;
        index index.html;
        root /data/wwwroot/www.2.com;
    }
    [root@localhost nginx]# vim /usr/local/nginx/conf/vhost/default.conf          
    server{
        listen 80 default_server;  #使用default_server指定nginx的默认虚拟主机
        deny all;
    }

    若使用其他域名来访问虚拟主机时,会匹配到默认虚拟主机,该配置会拒绝未定义的域名的虚拟主机。若不配置该选项,默认排在最前边的server会成为默认虚拟主机。

    五、测试配置文件是否存在问题:

    [root@localhost root]# cd /usr/local/nginx/sbin
    [root@localhost sbin]# ./nginx -t 
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

    六、当配置文件修改时,可以使用一下命令重新加载配置文件

    [root@localhost sbin]# ./nginx -s reload
  • 相关阅读:
    Bzoj4873 [SXOI2017]寿司餐厅
    Bzoj4870 [SXOI2017]组合数问题
    Bzoj4820 [Sdoi2017]硬币游戏
    Bzoj4816 [Sdoi2017]数字表格
    HDU2089 不要62
    Python——lambda函数
    Django——在线教育项目总结
    Django项目——CRM
    数据库——MongoDB的安装
    母猪的产后护理——一些零碎的知识
  • 原文地址:https://www.cnblogs.com/yyxianren/p/10797257.html
Copyright © 2011-2022 走看看