nginx njs docker 试用

摘要:
它主要基于anadeeppolavarapu/nginxhttp3:edgedocker映像,并使用一个简单的环境来准备docker compose文件版本:“3”services:httpservice:image:ranadeppolavarabu/nginx http3:edge volumes:-“./nginx.conf:/etc/nginx/nginx.conf“-”./h3.n

主要是基于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;
  }
  • 访问效果

nginx njs docker 试用第1张

说明

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

免责声明:文章转载自《nginx njs docker 试用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇spark算法实现——svm支持向量机照度/感光度(Lux)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

docker pull镜像失败

版权声明:本文为博主原创文章,支持原创,转载请附上原文出处链接和本声明。  本文地址:https://www.cnblogs.com/wannengachao/p/12119840.html 问题描述: 服务器重启后docker pull镜像报错:TLS握手超时。(见下图)  第一反应查看网络是否正常,于是尝试下ping外部网络发现网络是通的。 因为毕竟...

源码编译安装nginx及设置开机启动项

1、上传nginx文档;解压到/data目录下,并安装依赖包tar xf nginx-1.20.1.tar.gz -C /data/cd /data/nginx-1.20.1/ && ll依赖关系安装,执行如下:yum -y install zlib pcre pcre-devel openssl openssl-devel 2、自定义安装...

如何使用 Secret?- 每天5分钟玩转 Docker 容器技术(108)

我们经常要向容器传递敏感信息,最常见的莫过于密码了。比如: docker run -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql 在启动 MySQL 容器时我们通过环境变量 MYSQL_ROOT_PASSWORD 设置了 MySQL 的管理员密码。不过密码是以明文的形式写在 docker run 命令中,有潜在的安...

docker(4)docker的网络,自定义网桥

Docker 的网络 运行 ifconfig 找到 docker0 : 虚拟网卡默认网卡名称为docker0 查看docker 的网桥: 我这里默认们没有进行安装 网桥管理设备:进行安装一下; yum install bridge-utils 命令:查看网桥crctl show: 注意上图中的interfaces 这里为空,我们启动一个新的容器如下:...

Ansible 系列之 Inventory 资源清单介绍

一、Inventory 库存清单文件 1.Inventory 作用 Ansible 可以在同一时间针对多个系统设施进行管理工作。它通过选择Ansible 资源清单文件中列出的系统,该清单文件默认是在/etc/ansible/hosts,也可以使用 -i <path> 进行路径的指定。文件内的格式INI 风格,中括号内为分组名。 除了这个文件之...

部署CentOS,集成Mysql、Nodejs、Nginx

1. Mysql安装 Step 1 安装源 https://dev.mysql.com/downloads/repo/yum/ 选择:Red Hat Enterprise Linux 8 / Oracle Linux 8 (Architecture Independent), RPM Package 这个download 跳转的页面对着No thanks,...