设为首页 加入收藏

TOP

精选版:用Java扩展Nginx(nginx-clojure 入门)(三)
2023-09-09 10:25:50 】 浏览:60
Tags:Java 扩展 Nginx nginx-clojure 入门
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有个清晰的认识,接下来的文章咱们会深度探索它,让它更好的服务于业务

源码下载

名称 链接 备注
项目主页 https://github.com/zq2599/blog_demos 该项目在GitHub上的主页
git仓库地址(https) https://github.com/zq2599/blog_demos.git 该项目源码的仓库地址,https协议
git仓库地址(ssh) git@github.com:zq2599/blog_demos.git 该项目源码的仓库地址,ssh协议
  • 这个git项目中有多个文件夹,本篇的源码在nginx-clojure-tutorials文件夹下,如下图红框所示:
    在这里插入图片描述

欢迎关注博客园:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...

首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇15000字、6个代码案例、5个原理图.. 下一篇如何理解SpringBoot的Starter

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目