首页 服务器技术 nginx

nginx+njs 配置 模拟

reference:

nginx.org/en/docs/njs/ An official nginx docs for njs.
github.com/nginx/njs An official read-only mirror of http://hg.nginx.org/njs/ which is updated hourly.

关于安装参考:

http://nginx.org/en/docs/njs/install.html

日常使用linux过程中,有时候我们需要从网上clone代码,大多数使用的是git,有些使用hg clone,比如说

hg clone http://hg.nginx.org/njs
yum -y install mercurial
# whereis hg
hg: /usr/bin/hg /usr/share/man/man1/hg.1.gz
# rpm -qf /usr/bin/hg
mercurial-2.6.2-11.el7.x86_64

编译安装nginx,我的系统已经安装了。简单模拟下

:~/software# ls
nginx-1.19.7  nginx-1.19.7.tar.gz  ngx_http_proxy_connect_module
# hg clone http://hg.nginx.org/njs
destination directory: njs
requesting all changes
adding changesets
adding manifests
adding file changes
added 1621 changesets with 6098 changes to 463 files
updating to branch default
275 files updated, 0 files merged, 0 files removed, 0 files unresolved
# ls
nginx-1.19.7  nginx-1.19.7.tar.gz  ngx_http_proxy_connect_module  njs

重新编译增加参数:

/root/software/njs/nginx

注意这个--add-module=/root/software/njs/nginx

# cd nginx-1.19.7/
# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/root/software/ngx_http_proxy_connect_module --with-http_realip_module --with-threads --with-stream --with-stream_ssl_preread_module  --with-stream_ssl_module --add-module=/root/software/njs/nginx
# make
# make install

先给一个最简单的配置:

centos7 root@parallels:/usr/local/nginx/conf# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
centos7 root@parallels:/usr/local/nginx/conf# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
centos7 root@parallels:/usr/local/nginx/conf# /usr/local/nginx/sbin/nginx -s reload
centos7 root@parallels:/usr/local/nginx/conf# curl -I http://localhost
HTTP/1.1 200 OK
Server: nginx/1.19.7
Date: Mon, 22 Mar 2021 11:50:52 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sun, 28 Feb 2021 06:47:57 GMT
Connection: keep-alive
ETag: "603b3c9d-264"
Accept-Ranges: bytes

参考:

http://nginx.org/en/docs/njs/examples.html

搞2个测试的js

centos7 root@parallels:~# cat /usr/local/nginx/conf/njs/utils.js
function version(r) {
    r.return(200, njs.version);
}
export default {version}
centos7 root@parallels:~# cat /usr/local/nginx/conf/njs/example.js
function hello(r) {
  r.return(200, "Hello world!\n");
}
export default {hello}

centos7 root@parallels:/usr/local/nginx/conf# cat nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
   js_import /usr/local/nginx/conf/njs/utils.js;
   js_import main from /usr/local/nginx/conf/njs/example.js;
   server {
     listen 80;
     location = /version {
        js_content utils.version;
     }
     location / {
       js_content main.hello;
     }
  }
}

注意这个路径

   js_import /usr/local/nginx/conf/njs/utils.js;
   js_import main from /usr/local/nginx/conf/njs/example.js;

centos7 root@parallels:~# curl http://localhost/version

0.5.2

centos7 root@parallels:~# curl http://localhost/

Hello world!

相关推荐