DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Visually Designing Views for Java Web Apps
  • Migrating Spring Java Applications to Azure App Service (Part 1: DataSources and Credentials)
  • Beginners Guide for Web Scraping Using Selenium
  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java

Trending

  • Building a Production-Ready AI Agent in 2026: Beyond the Hello World Demo
  • Observability in Spring Boot 4
  • From Data Movement to Local Intelligence: The Shift from Centralized to Federated AI
  • Detecting Advanced Persistent Threats Using Behavioral Analytics and Log Correlation
  1. DZone
  2. Coding
  3. Java
  4. Fast Clojure/Java Web Apps on NGINX Without a Java Web Server

Fast Clojure/Java Web Apps on NGINX Without a Java Web Server

Nginx-Clojure is a Nginx module for embedding Clojure or Java programs, typically those Ring based handlers.

By 
Yuexiang Zhang user avatar
Yuexiang Zhang
·
Jan. 17, 14 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
28.9K Views

Join the DZone community and get the full member experience.

Join For Free

Nginx-Clojure  is a Nginx module for embedding Clojure or Java programs, typically those Ring based handlers. It is an opensource project hosted on Github, the site url is HERE. With it we can develope high performance Clojure/Java web apps on Nginx without any Java web server and with several benefits:

  • Clojure/Java controlled static files will get almost the same performance with Nginx static file service.
  • We just deploy one Nginx (compiled with Nginx-Clojure module) server instead of Nginx + some Java web server, eg. Tomcat, Jetty etc.
  • Ring Handler  is dead easy compared to Java Servlet
  • We can use Nginx modules such as GZip, Image Filter etc. with our static and dynamic content on the fly without any cost of Proxy level.
  • We can use Java rewrite handler + proxy_pass to implement a dynamic proxy/balancer very quickly.

There are three typical examples for writing a configuration about Ring handlers :


1. Pure Java Handler


package my;

import static nginx.clojure.MiniConstants.CONTENT_TYPE;
import static nginx.clojure.MiniConstants.NGX_HTTP_OK;

import java.io.IOException;
import java.util.Map;

import nginx.clojure.java.ArrayMap;
import nginx.clojure.java.NginxJavaRingHandler;

public class HelloHandler implements NginxJavaRingHandler {

@Override
public Object[] invoke(Map<String, Object> request) throws IOException {

return new Object[] { 
NGX_HTTP_OK, //http status 200
ArrayMap.create(CONTENT_TYPE, "text/plain"), //headers map
"Hello, Java & Nginx!"  //response body can be string, File or Array/Collection of string or File
};
}
}


In nginx.conf, eg.


location /java {
  content_handler_type java;
  content_handler_name my.HelloHandler;
 }



2. Inline Clojure Handler


       location /clojure {
          content_handler_code ' 
                        (fn[req]
                          {
                            :status 200,
                            :headers {"content-type" "text/plain"},
                            :body  "Hello Clojure & Nginx!" 
                            })
          ';
       }




3. External Clojure Handler


(ns my.hello)
(defn hello-world [request]
  {:status 200
  :headers {"Content-Type" "text/plain"}
  :body "Hello World"})



You should set your clojure JAR files to class path, see JVM path & class path from Nginx-Clojure Website.


       location /myClojure {
          content_handler_name 'my.hello/hello-world';
       }



For more details about the features from nginx-clojure please visit it Website https://nginx-clojure.github.io/ .

For more details and more useful examples for Compojure which is a small routing library for Ring that allows web applications to be composed of small, independent parts, please refer to https://github.com/weavejester/compojure

By the way the result of simple performance test with Nginx-Clojure is inspiring, more details can be got HERE  . Here 's the simple testing results , Nginx-Clojure is almost 6 times faster than Nginx-Php5


Java (programming language) Web apps Web server Web Service app

Opinions expressed by DZone contributors are their own.

Related

  • Visually Designing Views for Java Web Apps
  • Migrating Spring Java Applications to Azure App Service (Part 1: DataSources and Credentials)
  • Beginners Guide for Web Scraping Using Selenium
  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook