这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
作为《Java扩展Nginx》的第二篇,本想与大家一起开始nginx-clojure的深度之旅,但有个问题若不解决,会让大多数有兴趣的读者立即止步、关闭网页、再也不见
前文咱们使用的是nginx-clojure官方的安装包,这是个编译好的nginx可执行文件,开箱即用,当时我们还用命令查看过nginx版本是1.18.0,如下图:
直接使用nginx-clojure官方编译好的安装包,虽然好处是简单省事儿,但同样带来一些致命问题,导致咱们不敢将其用于生产环境,其实相信聪明的您已经想到了:
本次实战,所用nginx源码的版本是1.21.6,nginx-clojure源码的版本是0.5.2
整个编译和验证的过程,由以下步骤组成:
不说废话,直接开始动手
建议准备一个纯净的linux环境用来实战,我这里是租用的腾讯云轻应用服务器,安装了CentOS7.6,话说这轻应用服务器还真的方便,价格便宜,重装系统也很简单,如下图:
为了省事儿,全程使用root账号
远程连接腾讯云服务的客户端工具是FinalShell-3.9.2.2
nginx-clojure的源码中有java文件,因此要准备好JDK用于编译
去oracle官网下载jdk安装包,例如jdk-8u291-linux-x64.tar.gz,将其上传到linux服务器
解压,移动到指定目录:
tar -zxvf jdk-8u291-linux-x64.tar.gz <br /> && mkdir -p /usr/lib/jvm/ <br /> && mv jdk1.8.0_291 /usr/lib/jvm/
打开.bashrc,在尾部增加以下内容:
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_291
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
执行source .bashrc使得配置生效
检查是否安装成功,如下:
[root@VM-20-17-centos ~]# java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
更新yum:
yum update -y
安装必要的应用:
yum install -y epel-release <br /> vim <br /> net-tools <br /> bridge-utils <br /> firewalld <br /> bc <br /> iotop <br /> bc <br /> gcc <br /> gcc-c++ <br /> glibc <br /> glibc-devel <br /> pcre <br /> pcre-devel <br /> openssl <br /> openssl-devel <br /> zip <br /> unzip <br /> zlib-devel <br /> lrzsz <br /> tree <br /> ntpdate <br /> telnet <br /> lsof <br /> tcpdump <br /> wget <br /> libevent <br /> libevent-devel <br /> systemd-devel <br /> bash-completion <br /> traceroute <br /> psmisc
lein是编译nginx-clojure源码时用到的工具
安装步骤如下:
curl -o /usr/bin/lein https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein <br /> && chmod a+x /usr/bin/lein <br /> && lein
实测在腾讯云服务器上执行上述命令,可能出现连接超时的错误(Failed to download https://github.com/technomancy/leiningen/releases/download/2.9.8/leiningen-2.9.8-standalone.jar
),若遇到此类错误,请重试几次,即可成功
下载的过程有点耗时,就看您的网络状况了:
执行lein -version,控制台输出如下,表示lein安装成功:
[root@VM-20-17-centos ~]# lein -version
WARNING: You have $CLASSPATH set, probably by accident.
It is strongly recommended to unset this before proceeding.
Leiningen 2.9.8 on Java 1.8.0_291 Java HotSpot(TM) 64-Bit Server VM
用一行命令搞定下载nginx和nginx-clojure源码的压缩包,并将它们分别解压,然后删除压缩包:
cd ~ <br /> && curl -O http://nginx.org/download/nginx-1.21.6.tar.gz <br /> && curl -o nginx-clojure-0.5.2.zip https://codeload.github.com/nginx-clojure/nginx-clojure/zip/refs/tags/v0.5.2 <br /> && tar -zxvf nginx-1.21.6.tar.gz <br /> && unzip nginx-clojure-0.5.2.zip <br /> && rm -f nginx-1.21.6.tar.gz nginx-clojure-0.5.2.zip
此刻新增了两个文件夹,它们的完整路径分别是/root/nginx-1.21.6和/root/nginx-clojure-0.5.2,前者是nginx源码,后者是nginx-clojure模块的源码
执行以下命令,完成配置、编译、安装,注意add-module参数,里面指定了nginx-clojure模块的源码位置:
cd ~/nginx-1.21.6 <br /> && ./configure <br /> --prefix=/usr/local/nginx <br /> --sbin-path=/usr/local/nginx/sbin/nginx <br /> --conf-path=/usr/local/nginx/conf/nginx.conf <br /> --error-log-path=/var/log/nginx/error.log <br /> --http-log-path=/var/log/nginx/access.log <br /> --pid-path=/var/run/nginx/nginx.pid <br /> --lock-path=/var/lock/nginx.lock <br /> --user=nginx --group=nginx <br /> --with-http_ssl_module <br /> --with-http_stub_status_module <br /> --with-http_gzip_static_module <br /> --http-client-body-temp-path=/var/tmp/nginx/client/ <br /> --http-proxy-temp-path=/var/tmp/nginx/proxy/ <br /> --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ <br /> --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi <br /> --http-scgi-temp-path=/var/tmp/nginx/scgi <br /> --with-pcre <br /> --add-module=/root/nginx-clojure-0.5.2/src/c <br /> && make <br /> && make install
还要增加名为nginx的用户组和用户:
groupadd nginx && useradd -d /home/nginx -g nginx -m nginx
创建必要的文件夹:
mkdir -p /var/tmp/nginx/client
此时nginx已经安装好了,验证一下:
[root@VM-20-17-centos ~]# /usr/local/nginx/sbin/nginx -version
nginx version: nginx/1.21.6
二进制的nginx编译已经完成,还需要nginx-clojure模块的源码,得到的jar在运行时要用,执行以下命令:
cd ~/nginx-clojure-0.5.2 <br /> && lein jar
编译构建成功后,将得到的jar文件放入新建的目录/usr/local/nginx/jars:
mkdir /usr/local/nginx/jars <br /> && mv ~/nginx-clojure-0.5.2/target/nginx-clojure-0.5.2.jar /usr/local/nginx/jars/
nginx-clojure在运行的时候还要用到clojure-1.7.0.jar,我将其放在自己的GitHub仓库了,下载并放入新建的目录/usr/local/nginx/libs:
mkdir /usr/local/nginx/libs <br /> && curl -o /usr/local/nginx/libs/clojure-1.7.0.jar https://raw.githubusercontent.com/zq2599/blog_download_files/master/files/clojure-1.7.0.jar
至此,完整的nginx和nginx-clojure已经安装完成,接下来验证是否可用
既然是验证nginx-clojure是否可用,简简单单就好,就用前文的Hello World功能吧
前文的jar包,我已经上传到GitHub上,下载到/usr/local/nginx/jars/目录下:
curl -o /usr/local/nginx/jars/simple-hello-1.0-SNAPSHOT.jar https://raw.githubusercontent.com/zq2599/blog_download_files/master/files/simple-hello-1.0-SNAPSHOT.jar
还要修改/usr/local/nginx/conf/nginx.conf,先在http的配置中增加以下两行:
jvm_path auto;
jvm_classpath "/usr/local/nginx/libs/:/usr/local/nginx/jars/";
然后在server的配置中增加一个location:
location /java {
content_handler_type 'java';
content_handler_name 'com.bolingcavalry.simplehello.HelloHandler';
}
完整的/usr/local/nginx/conf/nginx.conf内容如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
jvm_path auto;
jvm_classpath "/usr/local/nginx/libs/*:/usr/local/nginx/jars/*";
server {
listen 80;
server_name localhost;#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /java {
content_handler_type 'java';
content_handler_name 'com.bolingcavalry.simplehello.HelloHandler';
}#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 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
启动nginx,命令是/usr/local/nginx/sbin/nginx
直接在服务器上用curl命令验证,命令是curl 127.0.0.1/java,响应如下图红框,可见服务正常,咱们写的java类被正常调用并且返回了预期的内容:
至此,nginx和nginx-clojure的源码编译以及验证都完成了,既然可以自由的编译源码,那么之前提到的安全、与其他模块共存的问题也就得以解决,接下来,咱们会深入研究nginx-clojure,以便更好的扩展nginx为实际项目所用。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章