ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

nginx配置虚拟主机

2021-05-28 16:34:53  阅读:134  来源: 互联网

标签:index www 虚拟主机 配置 server nginx conf data


  一、为了不影响主配置的文件,我先加个include 

    1、进入nginx所在目录

cd /etc/nginx/

    2、修改配置

vim nginx.conf 

    3、在http这段添加一行

http {
    include     /etc/nginx/conf.d/*.conf
}

这行表示,配置文件也可也放在/etc/nginx/conf.d/下以conf结尾(默认自带,不带在添加)

  二、在data下创建多个站点

    1、新建目录

mkdir /data/site1
mkdir /data/site2

    2、生成自己的页面

echo /data/site1/index.html > /data/site1/index.html
echo /data/site2/index.html > /data/site2/index.html

  三、配置虚拟主机

vim /etc/nginx/conf.d/test.conf
server {
        server_name     www.a.net;
        root /data/site1;
}
server {
        server_name     www.a.tech  www.a.org;
        root /data/site2;
}

这个语句已经包含在http 中了所以可以直接写server

listen 80 默认省略不写

server_name 网站名

root 指定家目录

也可以多个域名,支持通配符,支持正则表达式

  四、修改hosts文件

vim /etc/hosts
192.168.1.5 www.a.net www.a.tech

直接访问需要域名解析,我这里就不解析了,直接修改需要访问的hosts文件

  五、测试访问

[15:04:17 root@localhost nginx]#curl www.a.net
/data/site1/index.html
[15:04:59 root@localhost nginx]#curl www.a.tech
/data/site2/index.html

  六、如何通过IP访问的话,可以设置你需要的页面为默认主机

vim /etc/nginx/conf.d/test.conf

  server {
    server_name www.a.net ;
     root /data/site1;
  }

  server {
    listen 80 default_server;
    server_name www.a.tech;
    root /data/site2;
  }

注意要修改默认的/etc/nginx/nginx.conf,去掉原本里面的server默认server ,我这里用的是1.20版,没在nginx.conf

中,而是在etc/nginx/conf.d下面有个默认的.conf 文件

  七、测试访问

curl 192.168.1.5/index.html
/data/site2/index.html

 支持*通配任意长度的任意字符

  server_name *.magedu.com www.magedu.*
支持~起始的字符做正则表达式模式匹配,性能原因慎用
  server_name ~^www\d+\.magedu\.com$
说明: \d 表示 [0-9]
匹配优先级机制从高到低
  (1) 首先是字符串精确匹配 如:www.magedu.com
  (2) 左侧*通配符 如:*.magedu.com
  (3) 右侧*通配符 如:www.magedu.*
  (4) 正则表达式 如: ~^.*\.magedu\.com$
  (5) default_server

 

标签:index,www,虚拟主机,配置,server,nginx,conf,data
来源: https://www.cnblogs.com/alexlv/p/14822294.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有