编译Njs,让Nginx使用js!

前言

Njs 是 JavaScript 语言的一个子集,它允许 扩展 nginx 功能。 NJS 是按照 ECMAScript 5.1(严格模式)和一些 ECMAScript 6 及更高版本的扩展创建的。 合规性仍在不断发展。本篇文章将会详细讲述如何编译安装njs模块。

操作步骤

这里以在/root目录下编译安装为例

准备工作

yum -y update
yum -y install vim curl zlib wget tar

安装系统自带的nginx

yum -y install epel-release
yum -y install nginx

更新gcc编译器

sudo yum install centos-release-scl
sudo yum install devtoolset-11-gcc*
echo "source /opt/rh/devtoolset-11/enable" >> /etc/profile
source /etc/profile

安装完成后输入 gcc -v 进行检验,显示如下则说明安装成功

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-8/root/usr --mandir=/opt/rh/devtoolset-8/root/usr/share/man --infodir=/opt/rh/devtoolset-8/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-8.3.1-20190311/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC)

更新Openssl

本文以更新openssl 1.1.1s为例,更新其他版本可以参照以下步骤,更换源码包即可

下载源码包并解压
wget https://www.openssl.org/source/openssl-1.1.1s.tar.gz
tar -xvf openssl-1.1.1s.tar.gz
开始编译
cd openssl-1.1.1s/
./config --prefix=/usr/local/openssl
make
make install

如果你的Vps配置较差,可能需要等待一些时间,请耐心等待即可。

安装新版本
#备份旧版本
mv /usr/bin/openssl /usr/bin/openssl.bak
mv /usr/include/openssl /usr/include/openssl.bak
#建立软连接
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
echo “/usr/local/openssl/lib” >> /etc/ld.so.conf
ldconfig -v
查看是否成功
openssl version 
#显示如下说明更新成功
[root@VM-5099JFN5YK8G openssl-1.1.1s]# openssl version
OpenSSL 1.1.1s  1 Nov 2022 (Library: OpenSSL 1.1.1k  FIPS 25 Mar 2021)
#返回主目录
cd /root

下载相应源码并解压

下载解压nginx源码
wget http://nginx.org/download/nginx-1.23.3.tar.gz
tar -xvf nginx-1.23.3.tar.gz
下载Njs源码
yum install -y mercurial
hg clone http://hg.nginx.org/njs
⚠注意
如果你准备编译的Nginx版本低于1.23.3,请下载Njs版本0.78及以下的源码包。Nginx在1.23修改了header参数,导致低版本Njs不适配将会安装失败!如下
/usr/src/nginx/njs/nginx/ngx_js_fetch.c:2286:23: error: 'ngx_table_elt_t' {aka 'struct <anonymous>'} has no member named 'next'
 2286 |             ph = &h[i].next;
      |                       ^
/usr/src/nginx/njs/nginx/ngx_js_fetch.c:2287:38: error: 'ngx_table_elt_t' {aka 'struct <anonymous>'} has no member named 'next'
 2287 |             while (*ph) { ph = &(*ph)->next; }
      |                                      ^~
/usr/src/nginx/njs/nginx/ngx_js_fetch.c:2307:6: error: 'ngx_table_elt_t' {aka 'struct <anonymous>'} has no member named 'next'
 2307 |     h->next = NULL;
      |      ^~
/usr/src/nginx/njs/nginx/ngx_js_fetch.c: In function 'ngx_headers_js_get':
/usr/src/nginx/njs/nginx/ngx_js_fetch.c:3210:20: error: 'ngx_table_elt_t' {aka 'struct <anonymous>'} has no member named 'next'
 3210 |             ph = ph->next;
      |                    ^~
/usr/src/nginx/njs/nginx/ngx_js_fetch.c:3226:16: error: 'ngx_table_elt_t' {aka 'struct <anonymous>'} has no member named 'next'
 3226 |         ph = ph->next;
      |                ^~
/usr/src/nginx/njs/nginx/ngx_js_fetch.c:3242:14: error: 'ngx_table_elt_t' {aka 'struct <anonymous>'} has no member named 'next'
 3242 |         if (h->next == NULL) {
      |              ^~
/usr/src/nginx/njs/nginx/ngx_js_fetch.c:3247:14: error: 'ngx_table_elt_t' {aka 'struct <anonymous>'} has no member named 'next'
 3247 |         h = h->next;
      |              ^~
/usr/src/nginx/njs/nginx/ngx_js_fetch.c: In function 'ngx_headers_js_ext_set':
/usr/src/nginx/njs/nginx/ngx_js_fetch.c:3606:23: error: 'ngx_table_elt_t' {aka 'struct <anonymous>'} has no member named 'next'
 3606 |             ph = &h[i].next;
      |                       ^
/usr/src/nginx/njs/nginx/ngx_js_fetch.c:3610:28: error: 'ngx_table_elt_t' {aka 'struct <anonymous>'} has no member named 'next'
 3610 |                 ph = &(*ph)->next;
      |                            ^~
make[1]: Leaving directory '/usr/src/nginx/nginx-1.22.0'
make[1]: *** [objs/Makefile:2900: objs/addon/nginx/ngx_js_fetch.o] Error 1
make: *** [Makefile:10: build] Error 2

具体情况请参见Failed build · Issue #610 · nginx/njs (github.com)

下载其他模块源码(可选)
#安装headers-more-nginx-module
wget https://github.com/openresty/headers-more-nginx-module/archive/refs/tags/v0.34.tar.gz
tar -xvf v0.34.tar.gz
#安装ngx_http_geoip2_module
wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.4.tar.gz
tar -xvf 3.4.tar.gz
mv ngx_http_geoip2_module-3.4 ngx_http_geoip2_module

编译Nginx

在开始之前安装一些依赖

#防止报错 error: the invalid value in --with-ld-opt="-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E"
yum -y install redhat-rpm-config.noarch
#防止报错 error: the geoip2 module requires the maxminddb library.

wget https://github.com/maxmind/libmaxminddb/releases/download/1.7.1/libmaxminddb-1.7.1.tar.gz
tar -xvf libmaxminddb-1.7.1.tar.gz
cd libmaxminddb-1.7.1/


./configure
make
make install
echo /usr/local/lib  >> /etc/ld.so.conf.d/local.conf
ldconfig
cd /root
#防止报错 error: the HTTP rewrite module requires the PCRE library.
sudo yum install pcre-devel -y
#防止报错 error: the HTTP gzip module requires the zlib library.
sudo yum install zlib-devel -y
#防止报错 error: the HTTP XSLT module requires the libxml2/libxslt
yum -y install libxslt-devel
#防止报错 error: the HTTP image filter module requires the GD library.
yum install gd gd-devel -y
#防止报错 error: perl module ExtUtils::Embed is required
yum install perl-ExtUtils-Embed -y
#防止报错 error: the GeoIP module requires the GeoIP library
sudo yum install -y GeoIP-devel
#防止报错 error: the Google perftools module requires the Google perftools
yum -y install gperftools  

注意
在编译前上面的依赖尽量都执行一遍安装命令防止出错。
#进入编译目录
cd nginx-1.23.3/
#生成编译配置
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-dynamic-module=/root/njs/nginx --with-openssl=/root/openssl-1.1.1s --add-module=/root/headers-more-nginx-module-0.34 --add-module=/root/ngx_http_geoip2_module --with-http_geoip_module --with-stream
#开始编译
make
编译成功
编译成功显示如下
objs/src/mail/ngx_mail.o \
objs/src/mail/ngx_mail_core_module.o \
objs/src/mail/ngx_mail_handler.o \
objs/src/mail/ngx_mail_parse.o \
objs/src/mail/ngx_mail_ssl_module.o \
objs/src/mail/ngx_mail_pop3_module.o \
objs/src/mail/ngx_mail_pop3_handler.o \
objs/src/mail/ngx_mail_imap_module.o \
objs/src/mail/ngx_mail_imap_handler.o \
objs/src/mail/ngx_mail_smtp_module.o \
objs/src/mail/ngx_mail_smtp_handler.o \
objs/src/mail/ngx_mail_auth_http_module.o \
objs/src/mail/ngx_mail_proxy_module.o \
objs/src/mail/ngx_mail_realip_module.o \
objs/ngx_mail_module_modules.o \
-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E \
-shared
sed -e "s|%%PREFIX%%|/usr/share/nginx|" \
	-e "s|%%PID_PATH%%|/run/nginx.pid|" \
	-e "s|%%CONF_PATH%%|/etc/nginx/nginx.conf|" \
	-e "s|%%ERROR_LOG_PATH%%|/var/log/nginx/error.log|" \
	< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/root/nginx-1.23.3'

安装Nginx及其模块

停止现有的Nginx进程
systemctl stop nginx
备份原有的Nginx
cd /usr/sbin
mv nginx nginx.old
安装Nginx程序
mv /root/nginx-1.23.3/objs/nginx /usr/sbin/nginx
安装Nginx的动态模块
cd /root
#批量移动后缀名为.so的文件
find /root/nginx-1.23.3/objs -type f -iname "*.so" -exec mv --backup=numbered -t /usr/share/nginx/modules {} +
#删除nginx.so
rm -f /usr/share/nginx/modules/nginx.so
#重启Nginx
systemctl restart nginx

验证Nginx安装

#验证Nginx安装
[root@VM-5099JFN5YK8G ~]# nginx -V
nginx version: nginx/1.23.3
built by gcc 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC) 
built with OpenSSL 1.1.1s  1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-dynamic-module=/root/njs/nginx --with-openssl=/root/openssl-1.1.1s --add-module=/root/headers-more-nginx-module-0.34 --add-module=/root/ngx_http_geoip2_module --with-http_geoip_module --with-stream

后记

经过上面一系列的步骤,Nginx全部安装完成。现在你可以愉快的使用Nginx来构建你的网站了!对于Njs的应用,最经典的使用方式便是通过Njs进行分流,阻止非法的访问请求,做到不同请求返回不同页面。之后在搭建emby视频站的时候,njs便可以派上很大用场!

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇