设为首页 收藏本站
查看: 1304|回复: 0

[经验分享] nginx学习笔记之基于端口的虚拟主机基于主机名的虚拟主机root、alias、index配置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-12-29 08:51:32 | 显示全部楼层 |阅读模式
nginx学习笔记之基于端口的虚拟主机基于主机名的虚拟主机root、alias、index配置

实验环境:
    centos 测试节点IP:172.16.3.101

基于端口的虚拟主机:
    vim /etc/nginx/nginx.conf
    # 向里面的http {}里面加入如下内容
      server {                                   # server定义一个虚拟主机
            listen 8080;                       # 监听本机所有IP端口8080
            server_name www.test.com;          # 虚拟主机名为:www.test.com
            location / {                      # 根据用户请求的URI决定是否匹配该location,如若匹配到,则将被该location中的配置所处理
               root "/web/htdocs";              # web资源路径映射
             }
       }
     # 保存退出
     # 创建目录/web/htdocs
       mkdir -pv /web/htdocs
     vim /web/htdocs/index.html
     # 向里面加入如下内容
       hello,my serser_name is www.test.com,port is 8080
     # 保存退出
     # 测试,在远端浏览器分别输入:http://172.16.3.101http://172.16.3.101:8080
     # 如果显示对应的结果,则表明基于端口的虚拟主机配置成功

配置基于主机名的虚拟主机
    vim /etc/nginx/nginx.conf
    # 向里面的http{}里面加入如下内容
          server {                           # server定义一个虚拟主机
                listen 80;                 # 监听本机所有IP端口:80
                server_name www.test.com;  # 虚拟主机名为:www.test.com
                location / {              # 根据用户请求的RUL决定是否匹配该location,如果匹配到,则将被该location中的配置所处理
                   root "/web/htdocs";      # web资源路径映射
                     }
           }

          server {                           # server定义一个虚拟主机
                listen 80;                 # 监听本机所有IP端口:80
                server_name mail.test.com; # 虚拟主机名为:mail.test.com
                location / {              # 上面已经说了,此处不再重复
                   root "/web/mail";        # web资源路径映射
               }
           }
    mkdir -pv /web/mail
    vim /web/mail/index.html
    # 向里面加入如下内容
        hello,my server_name is mail.test.com
    # 保存退出
    vim /web/htdocs/index.html
    # 向里面加入如下内容
        hello,my server_name is www.test.com
    # 保存退出
    # 检查其语法
        [iyunv@localhost conf]# nginx -t
        nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
        nginx: configuration file /etc/nginx/nginx.conf test is successful   
    # 重启nginx【由于此处修改了端口号,所以需要重启nginx】
        [iyunv@localhost conf]# service nginx restart
        nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
        nginx: configuration file /etc/nginx/nginx.conf test is successful
        Stopping nginx:                                            [  OK  ]
        Starting nginx:                                            [  OK  ]
    # 配置解析文件/etc/hosts【这里访问测试还是在本机进行的,所以我就修改本机的hosts文件】
        vim /etc/hosts
        # 向里面加入如下内容
            172.16.3.101    www.test.com        # 把主机名www.test.com解析为172.16.3.101
            172.16.3.101    mail.test.com      # 把主机名mail.test.com解析为172.16.3.101
        # 保存退出
    # 测试【在本机直接测试就行,前面已说,hosts文件已经修改好】
    # 测试www.test.com虚拟主机
        [iyunv@localhost conf]# elinks -dump http://www.test.com
              hello,my server_name is www.test.com
      # 可知,www.test.com虚拟主机正常
      # 测试mail.test.com虚拟主机
        [iyunv@localhost conf]# elinks -dump http://mail.test.com
           hello,my server_name is mail.test.com   
    # 可知,mail.test.com虚拟主机正常

root和alias的区别:
    我这里先不说,且看下面的例子
    vim /etc/nginx/nginx.conf
    # 把如下语句加入http{}里面
          server {
                listen 80;
                server_name www.test.com;
                location /root {
                   root "/web/htdocs";
                 }
          }

          server {
                listen 80;
                server_name mail.test.com;
                location /alias {
                   alias "/web/mail";
                 }
          }
    # 对于虚拟主机www.test.com
        mkdir -pv /web/htdocs/root/
        vim /web/htdocs/root/index.html
        # 向里面加入如下语句
            hello,this is root type.
        # 保存,退出
    # 对于虚拟主机mail.test.com
        mkdir -pv /web/mail
        vim /web/mail/index.html
        # 向里面加入如下语句
            hello,this is alias type.
        # 保存退出
    # 语法检查
        nginx -t
    # nginx服务重启
        service nginx restart
    # 配置解析文件/etc/hosts,和上面那个实验做得一样,这里就不重复啦
    # 测试【直接在本机测试】
    # 测试www.test.com虚拟主机  
        [iyunv@localhost root]# elinks -dump http://www.test.com/root/
           hello,this is root type.  
    # 可知root类型是这样访问的,是这样起作用的,其对应访问路径为:/web/htdocs/root/index.html,不用我多说了吧。
    # 测试mail.test.com虚拟主机
        [iyunv@localhost root]# elinks -dump http://mail.test.com/alias
           hello,this is alias type.
    # 可知alias类型是这样访问的,是这样起作用的,其对应访问路径为:/web/mail/index.html,不用我解释了吧。

index配置:设置默认主页面
    看下面的操作吧
    vim /etc/nginx/nginx.conf
    # 把如下内容放入里面
      server {
            listen 80;
            server_name www.test.com;
            location /root {
               root "/web/htdocs";
           }
        }  
    # nginx 语法检查
        nginx -t
    # nginx 服务重启
        service nginx restart
    # 创建其web资源路径和文件
        mkdir -pv /web/htdocs/root/
        vim /web/htdocs/root/index.html
        # 向里面加入如下语句
            hello,this is root type.
        # 保存,退出
    # 配置解析文件/etc/hosts
        vim /etc/hosts
        # 把如下语句加入其中
            172.16.3.101    www.test.com
        # 保存退出
    # 访问【在本机测试访问就行】
    # 输入如下语句,这里没有输入主页文件,但是也能正常访问,可知nginx默认主页文件为index.html
        [iyunv@localhost root]# elinks -dump http://www.test.com/root/
           hello,this is root type.  
    vim /etc/nginx/nginx.conf
    # 把刚才加入的语句换成如下语句  
      server {
            listen 80;
            server_name www.test.com;
            location /root {
               root "/web/htdocs";
               index test.html
           }
        }  
    # nginx语法检查
        nginx -t
    # nginx 服务重启
        service nginx restart
    # 访问【在本机测试访问就行】
    # 输入如下语句,这里没有输入主页文件,但是不能正常访问了
        [iyunv@localhost root]# elinks -dump http://www.test.com/root
                                         403 Forbidden

           --------------------------------------------------------------------------

                                          nginx/1.6.2
    # 现在我把主页文件名更改一下
        mv /web/htdocs/root/index.html /web/htdocs/root/test.html
    # 再访问一下
    # 输入如下语句,这里没有输入主页文件,可以正常访问了   
        [iyunv@localhost root]# elinks -dump http://www.test.com/root
           hello,this is root type.
    现在应该明白index的作用了吧

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-39475-1-1.html 上篇帖子: nginx的自我介绍以及如何编译安装 下篇帖子: 编译安装配置nginx1.6以及其一些基本配置等 虚拟主机 alias
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表