How to connect nginx to my java module How to connect nginx to my java module nginx nginx

How to connect nginx to my java module


You will need to build your Java service in its own app server -- Tomcat would be a good choice for this. From there, it's a simple matter of configuring nginx to act as a proxy to Tomcat. Your nginx configuration will look something like the following:

user www-data;worker_processes 4;pid /var/run/nginx.pid;events {    worker_connections 4096;    # multi_accept on;}http {    server {        listen 80; #incoming port for nginx        server_name localhost;        location / {            proxy_pass http://127.0.0.1:8080;        }    }#...and other things, like basic settings, logging, mail, etc. 

The important piece here is the setting for proxy_pass. That's telling nginx to accept requests on port 80 and redirect them to port 8080 (Tomcat's standard port).


With nginx-clojure we can write content handler,rewrite handler, access handler and header filter in java, clojure or groovy. e.g.

in nginx.conf

   location /java {      content_handler_type 'java';      content_handler_name 'mytest.HelloService';   }

HelloService.java

package mytest;import java.util.Map;import nginx.clojure.java.ArrayMap;import nginx.clojure.java.NginxJavaRingHandler;import static nginx.clojure.MiniConstants.*;public  class HelloService implements NginxJavaRingHandler {@Overridepublic Object[] invoke(Map<String, Object> request) {    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                };    }}