nginx介绍(http)

一、include   引入外部文件

mime.types :主要存放请求的类型 

include       mime.types;

二、default_type  默认的类型

三、log_format 日志格式化

四、access_log 日志的存放路径

主要存放请求的日志

文件太大解决方法:

(1)、清空,进入到nginx 的logs文件夹下,执行命令

truncate -s 0 access.log

(2)关闭日志(如果不需要日志文件就直接关闭,但是不建议不建议)

access_log off;

(3)进行分割

通过shell脚本+linux的定时任务进行的一个平滑切分

#!/bin/bash
log_path=/usr/local/nginx/logs/access.log
save_path=/usr/local/nginx/logs/bak/access_$(date +%Y%m%d -d 'yesterday').log
cp $log_path $save_path && echo > $log_path

设置定时任务

crontab -e

#输入
0 0  * * * /usr/bin/sh cut_logs.sh #每天的00:00执行日志切分

crontab -l #查看定时任务是否添加成功

注:在安装时已经指定了路径:--http-log-path

 五、sendfile   用于文件高效传输

on :打开

六、tcp_nopush 

当数据包达到一定的大小时,再发送,必须和sendfile 联合使用

七、keepalive_timeout (单位:s)

客户端和服务器的连接时长

八、gzip 压缩(动态压缩)

on :开启  off:关闭

目的:提高传输效率,节约带宽,但是会消耗CPU

1)、限制最小压缩

gizp压缩起点,文件大于1k才进行压缩
gzip_min_length  1k;

 2)、定义压缩比例,压缩级别,1-9

gzip_comp_level 2;

注:数字越大压缩的越好,越占用CPU时间。

3)、定义压缩类型

gzip_types application/atom+xml application/geo+json application/javascript application/x-javascript application/json application/ld+json application/manifest+json application/rdf+xml application/rss+xml application/xhtml+xml application/xml font/eot font/otf font/ttf image/svg+xml text/css text/javascript text/plain text/xml;

 4)、设置压缩所需要的缓冲区大小

# 设置压缩所需要的缓冲区大小,以4k为单位,如果文件为7k则申请2*4k的缓冲区 
gzip_buffers 4 16k;

首先尝试使用静态压缩,如果有则返回 .gz 的预压缩文件,否则尝试动态压缩。

九、server 

server {
        listen       80;
        server_name  localhost;

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

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

     
    }

1)、listen 监听的端口号

2)、server_name 指定域名或ip

3)location /

注:/ 代表根目录

html的根目录

如果server比较多,可以通过include来引入

include ***.conf;

 4)、location = /50x.html

异常页面的匹配

5)、location规则

root

location中root指定的只是相对路径,需要和路径结合起来映射地址,比如

location ^~/static/ {	## 这里的root需要和路径结合使用,即是映射的文件位置为 /home/static
    root /home/; 
    index index.html
}

 此时我们访问 IP/static/a.css ,那么就会找到 /home/static/a.css

alias

alias指定的是绝对路径,不会和location中的路径结合使用,而是直接使用地址映射到文件,比如

location ^~/static/ {	## 不会路径结合映射地址,那么这里就会直接映射到/home/文件夹下的文件
    alias /home/; 
    index index.html
}
location的匹配种类
格式:location [ 空格 | = | ~ | ~* | !~ | !~* | @ ] /uri/ {}

=表示精确匹配,如果找到,立即停止搜索并立即处理此请求。
~表示执行一个正则匹配,区分大小写匹配
~*表示执行一个正则匹配,不区分大小写匹配
!~区分大小写不匹配
!~*不区分大小写不匹配
^~即表示只匹配普通字符(空格)。使用前缀匹配,^表示“非”,即不查询正则表达式。如果匹配成功,则不再匹配其他location。
@指定一个命名的location,一般只用于内部重定向请求。例如 error_page, try_files
/表示通用匹配,任何请求都会匹配到

对应示例说明:
1)=

server {
  server_name www.baidu.com;
  location = /abcd {
  […]
  }
}


匹配情况:
    # 正好完全匹配
    http://www.baidu.com/abcd

    # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配      
    http://www.baidu.com/ABCD   

    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1?m2     
    http://www.baidu.com/abcd?param1?m2 

    # 不匹配,因为末尾存在反斜杠(trailing slash),Nginx 不认为这种情况是完全匹配   
    http://www.baidu.com/abcd/

    # 不匹配,因为不是完全匹配   
    http://www.baidu.com/abcde    
 
2)(None)
可以不写 location modifier ,Nginx 仍然能去匹配 pattern 。这种情况下,
匹配那些以指定的 patern 开头的 URI,注意这里的 URI 只能是普通字符串,
不能使用正则表达式。

server {
  server_name website.com;
  location /abcd {
  […]
  }
}


匹配情况:
    # 正好完全匹配
    http://www.baidu.com/abcd  
      
    # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配
    http://www.baidu.com/ABCD        

    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1?m2
    http://www.baidu.com/abcd?param1?m2   

    # 末尾存在反斜杠(trailing slash)也属于匹配范围内
    http://www.baidu.com/abcd/ 

    # 仍然匹配,因为 URI 是以 pattern 开头的   
    http://www.baidu.com/abcde    
 
3)~
这个 location modifier 对大小写敏感,且 pattern 须是正则表达式

server {
  server_name www.baidu.com;
  location ~ ^/abcd$ {
  […]
  }
}

匹配情况:

    # 完全匹配
    http://www.baidu.com/abcd  

    # 不匹配,~ 对大小写是敏感的      
    http://www.baidu.com/ABCD       

    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1?m2
    http://www.baidu.com/abcd?param1?m2 

    # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$   
    http://www.baidu.com/abcd/    

    # 不匹配正则表达式 ^/abcd$
    http://www.baidu.com/abcde    

注意:对于一些对大小写不敏感的系统,比如 Windows ,~ 和 ~* 都是不起作用的,这主要是操作系统的原因。
 
4)~*
与 ~ 类似,但这个 location modifier 不区分大小写,pattern 须是正则表达式

server {
  server_name website.com;
  location ~* ^/abcd$ {
  […]
  }
}


匹配情况:
    # 完全匹配
    http://www.baidu.com/abcd        

    # 匹配,这就是它不区分大小写的特性
    http://www.baidu.com/ABCD       
    
    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1?m2
    http://www.baidu.com/abcd?param1?m2    

    # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
    http://www.baidu.com/abcd/    

    # 不匹配正则表达式 ^/abcd$
    http://www.baidu.com/abcde    
 
5)^~
匹配情况类似 2. (None) 的情况,以指定匹配模式开头的 URI 被匹配,不同的是,一旦匹配成功,那么 Nginx 就停止去寻找其他的 Location 块进行匹配了(与 Location 匹配顺序有关)
 
6. @
用于定义一个 Location块,且该块不能被外部Client 所访问,只能被Nginx内部配置指令所访问,比如try_files 或 error_page
   

 location @resize {
            rewrite ^/(.*)/cache/(.*)?(.*)$ /resize.php?dir=$1&path=$2$3;
            rewrite ^/(.*)/orgi/cert/(.*)?(.*)$ /pass/resize?dir=$1&type=cert&path=$2$3&is_orgi=true;
            rewrite ^/(.*)/orgi/card/(.*)?(.*)$ /pass/resize?dir=$1&type=card&path=$2$3&is_orgi=true;
            rewrite ^/(.*)/orgi/(.*)/(.*)?(.*)$ /pass/resize?dir=$1&type=$2&path=$3$4&is_orgi=true;
            include fastcgi_params;
        }

6)、expires  缓存

#缓存保存时间

expires time # expires  10s 10秒后过期

#缓存保存具体时间点

expires @time  # expires @ 22h30m 22点30到期


#缓存提前过期

expires -time  # expires -1h 提前一个小时过去,相当于不设置缓存

#缓存关闭

expires off 

#缓存永久

expires max

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值