下载指定版本的nginx
nginx-clojure - Browse Files at SourceForge.net
这里我选择的是0.5.2版本,下载之后需要进行解压
编写插件
新建一个maven项目,配置仓库和依赖
<repositories>
<repository>
<id>clojars.org</id>
<url>http://clojars.org/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>nginx-clojure</groupId>
<artifactId>nginx-clojure</artifactId>
<version>0.5.2</version>
</dependency>
</dependencies>
新建插件类
package org.example;
import static java.util.jar.Attributes.Name.CONTENT_TYPE;
import static nginx.clojure.MiniConstants.NGX_HTTP_OK;
import nginx.clojure.java.ArrayMap;
import nginx.clojure.java.NginxJavaRingHandler;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;
public class Main implements NginxJavaRingHandler {
@Override
public Object[] invoke(Map<String, Object> map) throws IOException {
return new Object[] {
NGX_HTTP_OK, //http status 200
ArrayMap.create(CONTENT_TYPE, "text/plain"), //headers map
"Hello, Nginx clojure! " + LocalDateTime.now() //response body can be string, File or Array/Collection of them
};
}
}
打出jar包,放在解压出来的nginx目录的jars目录下
修改nginx配置文件, 增加一个location
注意这个name,就是上面写的java代码里的全类名
location /java {
content_handler_type 'java';
content_handler_name 'org.example.Main';
}