Nginx配置分析 (nginx 二)

虚拟主机配置

  1. 基于域名配置
    在/conf/nginx.conf中添加两个虚拟主机配置
    server {
        listen       80;
        server_name  www.host1.com;

        location / {
            root   html/test;
            index  host.htm;
        }
    }

    server {
        listen       80;
        server_name  www.host2.com;

        location / {
            root   html/test;
            index  host.htm;
        }
    }

配置host文件,dns本地化

192.168.1.103 www.host1.com
192.168.1.103 www.host2.com

测试成功
在这里插入图片描述

在这里插入图片描述

  1. 端口配置
    在/conf/nginx.conf中添加两个虚拟主机配置
         server {
        listen       80;
        server_name  www.dengjl.com;

        location / {
            root   html/test;
            index  index.html index.htm;
        }
    }
    server {
        listen       8080;
        server_name  www.dengjl.com;

        location / {
            root   html/test8080;
            index  index.html index.htm;
        }
    }

配置host文件,dns本地化

192.168.1.103 www.dengjl.com

测试成功

在这里插入图片描述

在这里插入图片描述

Nginx的日志配置

使用nginx中默认的格式

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
日志分割思路

基于crontab定时任务,执行命令

### 备份
mv access.log access.log.bak
### 释放原文件句柄
./nginx -s reload

location的语法和匹配规则

location语法

location [=|~|~*|^~] patt {
}

参数解释:
= 开头表示精确匹配
^~ 开头表示uri以某个常规字符串开头,理解为匹配url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
~ 开头表示区分大小写的正则匹配
~* 开头表示不区分大小写的正则匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配的正则
/ 通用匹配,任何请求都会匹配到。
location配置优先级

先判断精准匹配,如果匹配,立即返回结果并结束解析过程
然后,判断普通命中没如果有多个命中,记录最长的匹配结果
再然后判断正则表达式的解析过程,按配置里的正则表达式顺序为准,由上到下开始匹配,一旦匹配成功立即返回结果并结束解析过程。
通过上面的分析我们可以知道:

普通匹配与顺序无关,因为按照匹配的长短来取匹配结果。
正则匹配与顺序有关,因为是从上往下匹配。(首先匹配,取其之。结束解析过程)

这是我测试过的配置文件
server {
        listen       80;
        server_name  www.test.com;

        # 一般匹配
        location / {
            root   html/test;
            index  test.htm;
        }   
            
        # 精准匹配
        location = /jz/ {     
            root   html/test;
            index  index.htm;
        }     
                
        # 开头表示uri以某个常规字符串开头
        location ^~ /static/ {
            root   html/other;
            index  index.htm;  
        }
    
        # 开头表示区分大小写的正则匹配
        location ~ \.(gif|jpg|js|png|css)$ {
            root   html/other;
        }

        # 开头表示不区分大小写的正则匹配
        location ~* \.jpg$ {
            root   html/other;
        }
 }

一般匹配
http://www.test.com/

精准匹配
http://www.test.com/jz/
http://www.test.com/jz/jz.htm

开头表示uri以某个常规字符串开头
http://www.test.com/static/aaa/3.html

开头表示区分大小写的正则匹配
http://www.test.com/static/s1/a.jpg

开头表示不区分大小写的正则匹配
http://www.test.com/static/s2/b.JPG

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值