nginx学习笔记(2)

今天学了nginx配置虚拟主机,有3种方式,在windows下修改nginx.conf文件。

首先是基于端口号配置

 server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
	
        location / {
	    
            root   html;
            index  index.html index.htm;
        }
	error_page   403        /403.html;
       	error_page   404 =200   /403.html;
        #redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       
    }
	 server {
        listen       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
	
        location / {
	    
            root   html1;
            index  index1.html index1.htm;
        }
	error_page   403        /403.html;
       	error_page   404  /403.html;
        error_page   500 502 503 504  /50x.html;
        }

还有基于ip配置

 server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
	
        location / {
	    
            root   html;
            index  index.html index.htm;
        }
	error_page   403        /403.html;
       	error_page   404 =200   /403.html;
        #redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       
    }
	 server {
        listen       80;
        server_name  196.168.0.100;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
	
        location / {
	    
            root   html;
            index  index.html index.htm;
        }
	error_page   403        /403.html;
       	error_page   404 =200   /403.html;
        #redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

最后还有基于域名配置,首先在windows/System32/drivers/etc目录下修改hosts文件,添加域名及对应的网站

例如 196.168.0.100  www.xx.com,接下来在nginx.conf中配置

 server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
	
        location / {
	    
            root   html;
            index  index.html index.htm;
        }
	error_page   403        /403.html;
       	error_page   404 =200   /403.html;
        #redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       
    }
	 server {
        listen       80;
        server_name  www.xx.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
	
        location / {
	    
            root   html;
            index  index.html index.htm;
        }
	error_page   403        /403.html;
       	error_page   404 =200   /403.html;
        #redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       
    }

在url中输入www.xx.com,它就会显示对应的index.html文件。

如果在nginx.conf下配置多个虚拟主机,会造成nginx.conf文件可读性差,可以使用子配置文件引入的方式

在nginx.conf所在目录下新建一个vhost.conf文件,然后在文件中配置service模块,最后再nginx.conf中使用include引入。

include有两种方式

include vhost.conf//单个文件引入

include   *.conf//通配符引入

另外,可以在service下配置autoindex on,当用户访问该站点时,如果没有设置对应的index索引文件,就会显示该站点的目录,没有设置则报403错误。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值