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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  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.

Yuexiang Zhang user avatar by
Yuexiang Zhang
·
Jan. 17, 14 · Tutorial
Like (0)
Save
Tweet
Share
27.65K 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.

Popular on DZone

  • Why It Is Important To Have an Ownership as a DevOps Engineer
  • Utilize OpenAI API to Extract Information From PDF Files
  • Continuous Development: Building the Thing Right, to Build the Right Thing
  • Building a Scalable Search Architecture

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: