DEV Community

qianduan
qianduan

Posted on

How to use js in nginx

  1. I heard that js can be used in nginx. nginx has use quickjs ,so we can use js in nginx config. That is exciting to me, I want to try it.

  2. But when I see the doc, I am crying. It is so complicated. I have to download source for njs and quickjs, and nginx. After all three source download, I have to run configure \ make && make install . Oh my god

  3. I try to download all the source files

git clone https://github.com/nginx/njs 
Enter fullscreen mode Exit fullscreen mode
 wget http://nginx.org/download/nginx-1.25.3.tar.gz # 确保版本 ≥ 1.23.0 
Enter fullscreen mode Exit fullscreen mode
 # 下载 OpenSSL 源码(以 3.0.8 为例) wget https://www.openssl.org/source/openssl-3.0.8.tar.gz tar -zxvf openssl-3.0.8.tar.gz cd openssl-3.0.8 
Enter fullscreen mode Exit fullscreen mode
 git clone https://github.com/bellard/quickjs cd quickjs/ CFLAGS='-fPIC' make libquickjs.a # 生成位置无关代码 
Enter fullscreen mode Exit fullscreen mode
 project_root/ ├── nginx-1.25.3/ # Nginx 源码目录 └── quickjs/ # QuickJS 源码目录(需手动创建或克隆) └── njs/ # QuickJS 源码目录(需手动创建或克隆) 
Enter fullscreen mode Exit fullscreen mode
 # 授予当前用户读取权限 sudo chmod -R 755 ../quickjs 
Enter fullscreen mode Exit fullscreen mode
  1. I feel so disappointed. We need to configure the file with command below
./configure --add-dynamic-module=../njs/nginx --with-openssl=../openssl-3.0.8 --with-cc-opt="-I $(pwd)/../quickjs" --with-ld-opt="-L $(pwd)/../quickjs" 
Enter fullscreen mode Exit fullscreen mode

After configure step, we still need to make and make install

make && sudo make install 
Enter fullscreen mode Exit fullscreen mode

I run nginx start .something wrong again, It shows mismatch for njs and nginx. I try to run command

whereis nginx 
Enter fullscreen mode Exit fullscreen mode
nginx: /opt/homebrew/bin/nginx /opt/homebrew/share/man/man8/nginx.8 
Enter fullscreen mode Exit fullscreen mode

It seems that I dont use the nginx I compiled. I tried to use direct path for nginx and then It can not find correct config file. So I decide to use all direct path

./objs/nginx -c ~/workspace/nginx-1.25.3/conf/nginx.conf 
Enter fullscreen mode Exit fullscreen mode

even if the config file use direct path

Stop the nginx

./objs/nginx -c ~/workspace/nginx-1.25.3/conf/nginx.conf -s stop 
Enter fullscreen mode Exit fullscreen mode

start

./objs/nginx -c ~/workspace/nginx-1.25.3/conf/nginx.conf 
Enter fullscreen mode Exit fullscreen mode

visit the http://localhost

{ "message": "Request processed at 1758537787024", "stats": { "headerCount": 15, "customHeaders": 0 }, "serverInfo": "GET / HTTP/1.1" } 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)