4

I set up a home server with ubuntu and ngingx and I can serve static files. Now I want to test some clojure files but I am not clear about how to do this. For php this seems very easy, for instance in this tutorial he adds a location for php files. Is that all I need to do, just indicate where Clojure files are in the config file?

I have the Web Development with Clojure by Dmitry Sotnikov and he talks about deployment but not specifically about nginx.

Can you point me in the right direction where I can find documentation about this?

Zeynel
  • 13,145
  • 31
  • 100
  • 145

2 Answers2

9

Please try Nginx-Clojure module. You can run clojure Ring handlers with Nginx without any Java Web Server, eg. Jetty. Further more it 's very fast. The benchmarks can be found HERE.

enter image description here

xfeep
  • 1,091
  • 11
  • 12
6

Consider next setup:

nginx -> ring server (jetty)

You need to start lein ring server (using lein-ring plugin) on some port (say 8080). Nginx will listen at 80 port and forward requests to 8080. Here is a sample nginx config:

upstream ring {
  server 127.0.0.1:8080 fail_timeout=0;
}

server {
    root <path-to-public-dir>;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
        # First attempt to serve request as file
        try_files $uri $uri/ @ring;
    }

    location @ring {
        proxy_redirect off;
        proxy_buffering off;
        proxy_set_header Host $http_host;
        proxy_pass http://ring;
    }

    location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
        expires     max;
        add_header  Cache-Control public;
    }
}

Change path-to-public-dir to directory where your static files reside (resources/public). Nginx will server static files (if file found) and forward other requests to ring server.

edbond
  • 3,921
  • 19
  • 26
  • 4
    Lein should not be run from production. Use lein uberjar to create a server jar. – noisesmith Dec 17 '13 at 15:19
  • I created a project with lein called `guestbook` and I did `lein ring server` which is running on `localhost:3000` and I see "Hello World" in the browser. I changed the conf file to "3000" from "8080" that you had (correct?) but now I am confused where I need to put the project files. At this point they are in `~/guestbook`. Can you help with this? – Zeynel Dec 18 '13 at 14:54
  • 1
    great, now change in nginx config: `root /home/zeynel/guestbook/resources/public` (make sure folder exists) and visit http://localhost/. This should hit your ring server. – edbond Dec 18 '13 at 15:45
  • ok, but I am still confused. If I change `root ~/guestbook/resources/public` where do I put put my html files, like index.html? – Zeynel Dec 18 '13 at 16:12
  • 1
    If it's a static page you can put it in public folder and request /index.html. For dynamic responses you need to learn about compojure routes - https://github.com/weavejester/compojure/wiki – edbond Dec 18 '13 at 17:20
  • I created the `~/guestbook/resources/public/html/index.html` file and made it the root in the nginx config file but I get "Not Found` when I try `http://localhost:3000/index.html`. I think that I am not really understanding what is going on here. I think nginx is not involved in this process at all. localhost is just the server that I started with `lein ring server`. I will ask a new question with more details with my setup. Thanks for your help. – Zeynel Dec 18 '13 at 18:49
  • try http://localhost/html/index.html. You are correct, nginx serves 80 port (url without a port), ring server listen on 3000 port. So you hit ring server not nginx. You are welcome! – edbond Dec 18 '13 at 18:59
  • ok, `http://localhost:3000/html/index.html` works, `localhost/html/index.html` does not work. But, I realized that my confusion is because I want to use my own domain `example.com` not `localhost`. So I think I need to start over. – Zeynel Dec 18 '13 at 19:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43450/discussion-between-edbond-and-zeynel) – edbond Dec 18 '13 at 19:09