x.htm;
}
#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;
}
location /clojure {
handler_type 'clojure';
handler_code '
(fn[req]
{
:status 200,
:headers {"content-type" "text/plain"},
:body "Hello Clojure & Nginx!"
})
';
}
location /java {
content_handler_type 'java';
content_handler_name 'com.bolingcavalry.simplehello.HelloHandler';
}
# location /groovy {
# handler_type 'groovy';
# handler_code '
# import nginx.clojure.java.NginxJavaRingHandler;
# import java.util.Map;
# public class HelloGroovy implements NginxJavaRingHandler {
# public Object[] invoke(Map<String, Object> request){
# return [200, //http status 200
# ["Content-Type":"text/html"], //headers map
# "Hello, Groovy & Nginx!"]; //response body can be string, File or Array/Collection of them
# }
# }
# ';
# }
#
}
}
启动nginx
- 启动命令很简单,在nginx-clojure-0.5.2目录下执行./nginx
- 如果启动失败了,请打开nginx-clojure-0.5.2/logs/error.log查看问题,例如我这里遇到过端口占用导致启动失败:
2022/02/02 17:45:07 [emerg] 27703#0: bind() to 0.0.0.0:8080 failed (48: Address already in use)
2022/02/02 17:45:07 [emerg] 27703#0: bind() to 0.0.0.0:8080 failed (48: Address already in use)
2022/02/02 17:45:07 [emerg] 27703#0: bind() to 0.0.0.0:8080 failed (48: Address already in use)
2022/02/02 17:45:07 [emerg] 27703#0: bind() to 0.0.0.0:8080 failed (48: Address already in use)
2022/02/02 17:45:07 [emerg] 27703#0: bind() to 0.0.0.0:8080 failed (48: Address already in use)
2022/02/02 17:45:07 [emerg] 27703#0: still could not bind()
验证
- 打开postman验证服务是否正常,请求地址是http://127.0.0.1:8080/java
- 响应如下图所示,符合预期,返回的就是咱们定制的HelloHandler的内容
- 至此,nginx-clojure的入门操作就完成了,虽然寥寥几行代码,但却给java程序员打开了一扇窗:用咱们熟悉的技术去扩展nginx,打造更符合业务的web服务器,以最短链路完成web响应
- 本篇只是nginx-clojure之旅的开端,先对nginx-clojure有个清晰的认识,接下来的文章咱们会深度探索它,让它更好的服务于业务
源码下载
- 这个git项目中有多个文件夹,本篇的源码在nginx-clojure-tutorials文件夹下,如下图红框所示:
欢迎关注博客园:程序员欣宸
学习路上,你不孤单,欣宸原创一路相伴...
|