zoukankan      html  css  js  c++  java
  • nginx njs docker 试用

    主要是基于anadeeppolavarapu/nginx-http3:edge docker 镜像,使用比较简单

    环境准备

    • docker-compose 文件
    version: "3"
    services:
        httpservice:
            image: ranadeeppolavarapu/nginx-http3:edge
            volumes:
                - "./nginx.conf:/etc/nginx/nginx.conf"
                - "./h3.nginx.conf:/etc/nginx/conf.d/h3.nginx.conf"
                - "./status.conf:/etc/nginx/conf.d/status.conf"
                - "./localhost.crt:/etc/ssl/localhost.crt"
                - "./localhost.key:/etc/ssl/localhost.key"
                - "./http.js:/opt/http.js"
            ports:
                - "443:443/tcp"
                - "443:443/udp"
                - "8080:8080"
        prome:
            image: nginx/nginx-prometheus-exporter:0.8.0
            command: -nginx.scrape-uri http://httpservice:8080/stub_status
            ports:
                - "9113:9113"
    • njs 加载js配置
      js_import /opt/http.js;
      js_set $foo     http.foo;
      js_set $summary http.summary;
      include /etc/nginx/conf.d/*.conf;
    • http.js
    function foo(r) {
        r.log("hello from foo() handler");
        return "foo";
    }
    function summary(r) {
        var a, s, h;
        s = "JS summary
    
    ";
        s += "Method: " + r.method + "
    ";
        s += "HTTP version: " + r.httpVersion + "
    ";
        s += "Host: " + r.headersIn.host + "
    ";
        s += "Remote Address: " + r.remoteAddress + "
    ";
        s += "URI: " + r.uri + "
    ";
        s += "Headers:
    ";
        for (h in r.headersIn) {
            s += "  header '" + h + "' is '" + r.headersIn[h] + "'
    ";
        }
        s += "Args:
    ";
        for (a in r.args) {
            s += "  arg '" + a + "' is '" + r.args[a] + "'
    ";
        }
        return s;
    }
    function baz(r) {
        r.status = 200;
        r.headersOut.foo = 1234;
        r.headersOut['Content-Type'] = "text/plain; charset=utf-8";
        r.headersOut['Content-Length'] = 15;
        r.sendHeader();
        r.send("nginx");
        r.send("java");
        r.send("script");
        r.finish();
    }
    function hello(r) {
        r.return(200, "Hello world!");
    }
    export default {foo, summary, baz, hello};
     
     
    • nginx location 配置
      location / {
          add_header X-Foo $foo;
          js_content http.baz;
      }
      location = /summary {
          default_type text/plain;
          return 200 $summary;
      }
      location = /hello {
          default_type text/plain;
          js_content http.hello;
      }
    • 访问效果

    说明

    njs 目前来说是越来越强大了,目前就是提供的周边少点,还好是js ,我们可以使用其他工具加速生成帮助类

    参考资料

    http://nginx.org/en/docs/njs/node_modules.html
    http://nginx.org/en/docs/http/ngx_http_js_module.html
    http://nginx.org/en/docs/njs/
    http://nginx.org/en/docs/njs/compatibility.html
    http://nginx.org/en/docs/njs/reference.html#http_stream

  • 相关阅读:
    [排错] VO对象和POJO对象的关系
    celery(异步处理)+redis
    django开发经验(每日生鲜)
    开发流程
    linux 使用问题
    磁盘的操作
    文件系统的简单操作
    LINUX磁盘与档案系统
    文件操作
    Linux文档修改
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/13580573.html
Copyright © 2011-2022 走看看