$ rm -rf /tmp/bug-njs-fetch;mkdir -p /tmp/bug-njs-fetch
$ nginx -c "$PWD/nginx.conf" -g 'daemon off;'&
$ curl -s http://127.0.0.1:8080/123
*.example.com=GET /123 HTTP/1.1
$ curl -s http://127.0.0.1:8080/123 --json '123'
*.example.com=POST /123 HTTP/1.1
123
$ curl -s http://127.0.0.1:8080/bug/123
*.example.com=GET /sub/bug/123 HTTP/1.1
$ curl -s http://127.0.0.1:8080/fix/123
*.example.com=GET /sub/fix/123 HTTP/1.1
$ curl -s http://127.0.0.1:8080/bug/123 --json '123'
<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.24.0 (Ubuntu)</center>
</body>
</html>
$ curl -s http://127.0.0.1:8080/fix/123 --json '123'
*.example.com=POST /sub/fix/123 HTTP/1.1
123
$ curl -s http://127.0.0.1:8080/bug/123 -XPOST
*.example.com=POST /sub/bug/123 HTTP/1.1
$ curl -s http://127.0.0.1:8080/fix/123 -XPOST
*.example.com=POST /sub/fix/123 HTTP/1.1
Last active
June 16, 2025 02:38
-
-
Save pyhedgehog/8f3bf0e8373aeb3f75c7b23347a1e578 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# nginx.conf | |
#user nginx; | |
worker_processes 3; | |
error_log /tmp/bug-njs-fetch/error.log warn; | |
pid /tmp/bug-njs-fetch/nginx.pid; | |
load_module modules/ngx_http_js_module.so; | |
events { | |
} | |
http { | |
default_type application/octet-stream; | |
log_subrequest on; | |
access_log /tmp/bug-njs-fetch/access.log combined; | |
js_import nginx.js; | |
server { | |
listen 8080; | |
server_name *.example.com; | |
root /usr/share/nginx/html; | |
location /bug/ { | |
js_content nginx.fetchbug; | |
} | |
location /fix/ { | |
js_content nginx.fetchfix; | |
} | |
location /sub/ { | |
add_header X-Nginx-Block /sub/; | |
add_header X-Nginx-Host $host; | |
# js_set $body nginx.getbody; | |
# return 200 '$server_name=$request$body'; | |
js_content nginx.bodypage; | |
} | |
location / { | |
try_files $uri /sub/$uri; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function fetchbug(r, fix) { | |
var url = `${r.variables.scheme}://127.0.0.1:${r.variables.server_port}/sub${r.variables.uri}`; | |
ngx.log(ngx.ERR, `fetchbug(${fix}): url=${url}`); | |
var headers = new Headers(r.headersIn); | |
var method = r.method; | |
var body = r.requestText||''; | |
if(fix) | |
headers.delete('content-length'); | |
ngx.log(ngx.ERR, `fetchbug(${fix}): url=${url}, opts=${njs.dump({method, headers, body})}`); | |
var reply = await ngx.fetch(url, {method, headers, body}); | |
reply.headers.forEach((n)=>{ | |
var l = reply.headers.getAll(n), ln = n.toLowerCase(); | |
var skip = (ln === 'connection')||(ln==='content-length'); | |
if(!skip) | |
r.headersOut[n] = l; | |
}); | |
var outbody = await reply.text(); | |
r.return(reply.status, outbody); | |
} | |
async function fetchfix(r) { | |
return fetchbug(r, true); | |
} | |
function getbody(r) { | |
var res = (r.requestText===undefined)?'':('\n'+r.requestText); | |
ngx.log(ngx.ERR, `getbody: text=${njs.dump(r.requestText)}, empty=${r.requestText===undefined}, res=${njs.dump(res)}`); | |
return res; | |
} | |
async function bodypage(r) { | |
var body = getbody(r); | |
r.return(200, `${r.variables.server_name}=${r.variables.request}${body}`); | |
} | |
export default { | |
fetchbug, | |
fetchfix, | |
getbody, | |
bodypage | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
cd "$(dirname "$0")" | |
mkdir -p /tmp/bug-njs-fetch | |
ln -sf /proc/self/fd/2 /tmp/bug-njs-fetch/error.log | |
ln -sf /proc/self/fd/1 /tmp/bug-njs-fetch/access.log | |
nginx -c "$PWD/nginx.conf" -g 'daemon off;' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment