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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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
  • Configuring Java Apps With Kubernetes ConfigMaps and Helm

Trending

  • How To Introduce a New API Quickly Using Quarkus and ChatGPT
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Supervised Fine-Tuning (SFT) on VLMs: From Pre-trained Checkpoints To Tuned Models
  • Modern Test Automation With AI (LLM) and Playwright MCP
  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.5K 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
  • Configuring Java Apps With Kubernetes ConfigMaps and Helm

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!