nignx一些不常见但实用的功能

记录nginx一些不常见但实用的功能

记录反向代理时的接口请求参数

# 打印http请求的参数 对于运维方便调试程序 注意可能会有敏感数据出现在日志内 注意安全
# http{} 内设置日志格式
log_format youngdebug escape=json '{ "@timestamp": "$time_iso8601", '
                   '"remote_addr": "$remote_addr",'
                   '"costime": "$request_time",'
                   '"realtime": "$upstream_response_time",'
                   '"status": $status,'
                   '"x_forwarded": "$http_x_forwarded_for",'
                   '"referer": "$http_referer",'
                   '"request": "$request",'
                   '"upstr_addr": "$upstream_addr",'
                   '"bytes":$body_bytes_sent,'
                   '"dm":$request_body,'
                   '"agent": "$http_user_agent" }';

# 针对server{} 中设置access_log 并设置日志格式 
server {
    .....
    access_log  /data/logs/${your_app}.log youngdebug;
}

限制非中国的IP及黑白名单

# 使用geoip 数据库对不同区域的IP进行判断 需要添加geoip相关的模块 只是知道这个东西暂未测试

# 使用默认的白名单对IP 地址进行限制 可根据子网进行限制
server{
    location / {
        deny  192.168.1.1;
        allow 192.168.1.0/24;
        allow 10.1.1.0/16;
        allow 2001:0db8::/32;
        deny  all;
    }
}

http_stub 模块的使用

# sub_filter 用于替换 页面内容 示例如下
# 更改nginx默认页内容

server{
    ......
        location / {
          sub_filter_once off;
          sub_filter 'Welcome to CentOS' '人生短短几个球';
        }

}

限流 防cc攻击 设置dns解析地址

# 大概意思
# limit_req_zone $remote_addr$request_uri zone=auth_limit:20m rate=5r/s;
# 根据变量$remote_addr$request_uri 定义20M的容器auth_limit 访问限制每秒5次
# limit_req_zone $cookie_token zone=session_limit:3m rate=5r/s;
# 根据变量$cookie_token 定义3M的容器 session_limit 访问限制每秒5次
# resolver 10.68.0.2 valid=1s;
#指定 dns解析地址为10.68.0.2 用于反向代理时设置的访问地址在固定dns区域内访问的情况、例如可直接在nginx中设置k8s的服务域名 

http{
limit_req_zone $remote_addr$request_uri zone=auth_limit:20m rate=5r/s;
limit_req_zone $cookie_token zone=session_limit:3m rate=5r/s;
resolver 10.68.0.2 valid=1s;
}

gzip 压缩 (定制网页优化)

# 主要问题在于优化gzip_types 压缩等级越高越消耗 
# 可F12查看资源大小 和是否有有经过压缩处理 如未经过压缩处理则在gzip_types 中加入该资源的类型
# 常见的格式 如js xml json png jpg等 
# text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php application/javascript application/json  image/jpeg image/gif image/png;

http{
....
gzip  on;
gzip_http_version 1.0;
gzip_vary on;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_buffers 4 16k;
gzip_min_length 1k;
}

定义自己header

# 使用 more_set_headers 修改默认的headers add_headers添加headers(如解决跨域等)
server{
        location / {
            #add_header Server younglinuxer-web;
            more_set_headers "Server: younglinuxer-web";
            more_set_headers "tel: MTg5ODMzNTkyMzkK";
            default_type text/html;
            content_by_lua_block {
                ngx.say("社区暂未开放 联系: younglinuxer@gmail.com tel-to:Response Headers")
        }

}

基础认证功能

# 实现基础认证功能
# 新增访问用户名younglinxuer 密码123456 echo "younglinuxer:$(openssl  passwd 123456)" >> /etc/ngx_passwd
# curl使用账号密码验证访问 curl  younglinuxer:younglinuxer@192.168.71.130/file

    location /file {
        auth_basic           "my site";
        auth_basic_user_file /etc/ngx_passwd;
    }

MP4流媒体播放

server{
    listen  80;
    server_name  _;
    root /data/src;
    location /video/ {
        mp4;
        mp4_buffer_size       1m;
        mp4_max_buffer_size   5m;
}
}

文件服务器

参考 https://iptables.cn/file/
server{
    listen  80;
    server_name  _;
        charset utf-8;
        location / {
            root /;
            autoindex on;
            autoindex_exact_size off;
            autoindex_format html;
            autoindex_localtime off; 
        }
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇