Skip to content

Instantly share code, notes, and snippets.

@pyhedgehog
Created May 6, 2025 08:05
Show Gist options
  • Save pyhedgehog/7aec4e2507a753aad96a48de7d418ce1 to your computer and use it in GitHub Desktop.
Save pyhedgehog/7aec4e2507a753aad96a48de7d418ce1 to your computer and use it in GitHub Desktop.
js_path /etc/nginx/;
js_import test.js;
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location @periodic {
set $testvar TESTVAR;
js_periodic test.test_periodic interval=60s;
}
location /_health {
default_type application/json;
return 200 '{"status": "ok"}';
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
2025/05/06 08:02:08 [notice] 7#7: js vm init njs: 0000585DA8AD9300
2025/05/06 08:02:08 [notice] 7#7: using the "epoll" event method
2025/05/06 08:02:08 [notice] 7#7: nginx/1.27.5
2025/05/06 08:02:08 [notice] 7#7: built by gcc 12.2.0 (Debian 12.2.0-14)
2025/05/06 08:02:08 [notice] 7#7: OS: Linux 6.11.0-25-generic
2025/05/06 08:02:08 [notice] 7#7: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2025/05/06 08:02:08 [notice] 7#7: start worker processes
2025/05/06 08:02:08 [notice] 7#7: start worker process 9
2025/05/06 08:02:08 [notice] 7#7: start worker process 10
2025/05/06 08:02:08 [notice] 7#7: start worker process 11
2025/05/06 08:02:08 [notice] 7#7: start worker process 12
2025/05/06 08:02:08 [notice] 7#7: start worker process 13
2025/05/06 08:02:08 [warn] 9#9: using uninitialized "testvar" variable
2025/05/06 08:02:08 [error] 9#9: js: testvar=
2025/05/06 08:02:08 [notice] 7#7: start worker process 14
2025/05/06 08:02:08 [notice] 7#7: start worker process 15
2025/05/06 08:02:08 [notice] 7#7: start worker process 16
2025/05/06 08:02:08 [notice] 7#7: start worker process 17
2025/05/06 08:02:08 [notice] 7#7: start worker process 18
2025/05/06 08:02:08 [notice] 7#7: start worker process 19
2025/05/06 08:02:08 [notice] 7#7: start worker process 20
2025/05/06 08:02:08 [notice] 7#7: start worker process 21
2025/05/06 08:02:08 [notice] 7#7: start worker process 22
2025/05/06 08:02:08 [notice] 7#7: start worker process 23
2025/05/06 08:02:08 [notice] 7#7: start worker process 24
2025/05/06 08:02:08 [notice] 7#7: start worker process 25
2025/05/06 08:02:08 [notice] 7#7: start worker process 26
2025/05/06 08:02:08 [notice] 7#7: start worker process 27
2025/05/06 08:02:08 [notice] 7#7: start worker process 28
2025/05/06 08:02:08 [notice] 7#7: start worker process 29
2025/05/06 08:02:08 [notice] 7#7: start worker process 30
127.0.0.1 - - [06/May/2025:08:02:09 +0000] "GET /_health HTTP/1.1" 200 16 "-" "curl/7.88.1" "-"
HTTP/1.1 200 OK
Server: nginx/1.27.5
Date: Tue, 06 May 2025 08:02:09 GMT
Content-Type: application/json
Content-Length: 16
Connection: keep-alive
{"status": "ok"}
2025/05/06 08:02:09 [notice] 33#33: js vm init njs: 00005CF85213B300
2025/05/06 08:02:09 [notice] 33#33: signal process started
2025/05/06 08:02:09 [notice] 7#7: signal 3 (SIGQUIT) received from 33, shutting down
2025/05/06 08:02:09 [notice] 12#12: gracefully shutting down
2025/05/06 08:02:09 [notice] 13#13: gracefully shutting down
2025/05/06 08:02:09 [notice] 12#12: exiting
2025/05/06 08:02:09 [notice] 13#13: exiting
2025/05/06 08:02:09 [notice] 14#14: gracefully shutting down
2025/05/06 08:02:09 [notice] 11#11: gracefully shutting down
2025/05/06 08:02:09 [notice] 15#15: gracefully shutting down
2025/05/06 08:02:09 [notice] 16#16: gracefully shutting down
2025/05/06 08:02:09 [notice] 19#19: gracefully shutting down
2025/05/06 08:02:09 [notice] 14#14: exiting
2025/05/06 08:02:09 [notice] 18#18: gracefully shutting down
2025/05/06 08:02:09 [notice] 16#16: exiting
2025/05/06 08:02:09 [notice] 11#11: exiting
2025/05/06 08:02:09 [notice] 15#15: exiting
2025/05/06 08:02:09 [notice] 19#19: exiting
2025/05/06 08:02:09 [notice] 18#18: exiting
2025/05/06 08:02:09 [notice] 20#20: gracefully shutting down
2025/05/06 08:02:09 [notice] 20#20: exiting
2025/05/06 08:02:09 [notice] 9#9: gracefully shutting down
2025/05/06 08:02:09 [notice] 9#9: exiting
2025/05/06 08:02:09 [notice] 21#21: gracefully shutting down
2025/05/06 08:02:09 [notice] 21#21: exiting
2025/05/06 08:02:09 [notice] 23#23: gracefully shutting down
2025/05/06 08:02:09 [notice] 26#26: gracefully shutting down
2025/05/06 08:02:09 [notice] 28#28: gracefully shutting down
2025/05/06 08:02:09 [notice] 23#23: exiting
2025/05/06 08:02:09 [notice] 28#28: exiting
2025/05/06 08:02:09 [notice] 26#26: exiting
2025/05/06 08:02:09 [notice] 22#22: gracefully shutting down
2025/05/06 08:02:09 [notice] 16#16: exit
2025/05/06 08:02:09 [notice] 11#11: exit
2025/05/06 08:02:09 [notice] 13#13: exit
2025/05/06 08:02:09 [notice] 22#22: exiting
2025/05/06 08:02:09 [notice] 15#15: exit
2025/05/06 08:02:09 [notice] 12#12: exit
2025/05/06 08:02:09 [notice] 18#18: exit
2025/05/06 08:02:09 [notice] 10#10: gracefully shutting down
2025/05/06 08:02:09 [notice] 20#20: exit
2025/05/06 08:02:09 [notice] 10#10: exiting
2025/05/06 08:02:09 [notice] 19#19: exit
2025/05/06 08:02:09 [notice] 14#14: exit
2025/05/06 08:02:09 [notice] 17#17: gracefully shutting down
2025/05/06 08:02:09 [notice] 17#17: exiting
2025/05/06 08:02:09 [notice] 21#21: exit
2025/05/06 08:02:09 [notice] 9#9: exit
2025/05/06 08:02:09 [notice] 24#24: gracefully shutting down
2025/05/06 08:02:09 [notice] 24#24: exiting
2025/05/06 08:02:09 [notice] 7#7: signal 15 (SIGTERM) received from 1, exiting
2025/05/06 08:02:09 [notice] 23#23: exit
2025/05/06 08:02:09 [notice] 28#28: exit
2025/05/06 08:02:09 [notice] 26#26: exit
2025/05/06 08:02:09 [notice] 22#22: exit
2025/05/06 08:02:09 [notice] 10#10: exit
2025/05/06 08:02:09 [notice] 28#28: signal 1 (SIGHUP) received, ignoring
2025/05/06 08:02:09 [notice] 21#21: signal 1 (SIGHUP) received, ignoring
2025/05/06 08:02:09 [notice] 26#26: signal 1 (SIGHUP) received, ignoring
2025/05/06 08:02:09 [notice] 23#23: signal 1 (SIGHUP) received, ignoring
2025/05/06 08:02:09 [notice] 22#22: signal 1 (SIGHUP) received, ignoring
2025/05/06 08:02:09 [notice] 24#24: signal 1 (SIGHUP) received, ignoring
2025/05/06 08:02:09 [notice] 20#20: signal 1 (SIGHUP) received, ignoring
2025/05/06 08:02:09 [notice] 9#9: signal 1 (SIGHUP) received, ignoring
2025/05/06 08:02:09 [notice] 10#10: signal 1 (SIGHUP) received, ignoring
2025/05/06 08:02:09 [notice] 18#18: signal 1 (SIGHUP) received, ignoring
2025/05/06 08:02:09 [notice] 13#13: signal 1 (SIGHUP) received, ignoring
2025/05/06 08:02:09 [notice] 16#16: signal 1 (SIGHUP) received, ignoring
2025/05/06 08:02:09 [notice] 15#15: signal 1 (SIGHUP) received, ignoring
#FROM nginx:1.23.4-bullseye
FROM nginx:1.27.5-bookworm
COPY ./nginx.conf ./test.js /etc/nginx/
COPY ./default.conf /etc/nginx/conf.d/
COPY ./once.sh /usr/local/bin/
RUN chmod 0755 /usr/local/bin/once.sh
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;
load_module modules/ngx_http_js_module.so;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
#!/bin/bash
#/docker-entrypoint.sh
nginx -g 'daemon off;'&ngxpid=$!
sleep ${ONCE_INIT_SLEEP:-1}
test ${ONCE_HEALTH:-0} == 0||curl -m ${ONCE_HEALTHCHECK_TIMEOUT:-3} http://127.0.0.1:8080/_health
sleep ${ONCE_HEALTH_SLEEP:-0}
curl "$@"
echo
nginx -s quit
kill $ngxpid
#!/bin/sh
set -e
docker build -t nginx-periodic:manual .
docker rm -f nginx-periodic &> /dev/null
docker run --rm -it --name=nginx-periodic nginx-periodic:manual once.sh -i "http://127.0.0.1/_health"
async function test_periodic(s) {
ngx.log(ngx.ERR, `testvar=${s.variables.testvar}`);
}
export default { test_periodic };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment