How do I run nginx as proxy to jetty?

2019-09-06 16:33发布

问题:

I have this setup:

I installed nginx and Jetty on an ubuntu machine that I am using as the server. I tested Jetty and it is running on 0.0.0.0.:8080 and I see the Jetty Welcome page.

The domain that I will be using is nomilkfor.me. The nginx conf file is in /etc/nginx/sites-available/nomilkfor.me.conf.

I am programming on an another laptop (OSX) and I created a project web_test with lein. I created a .war file of web_test and when I run lein ring server I see the content of core.clj on the browser.

Initially, I set up nginx conf file after this answer but I could not make it work.

I want to ask how I can fix the conf file below (with my questions) and how to deploy web_test with nginx as proxy to jetty.

I pasted the conf file below and added my questions for each directive:

# what should the ip address and port be?
# i assume this instructs nginx to send request it receives on port 80 to Jetty server
upstream ring {
    server ????? fail_timeout=0;
}

# what directory do I need to enter here?
# do I use the clojure project root?
# do I use ~/web_test/src ?
server {
    root /?????;

    # make site accessible from http://localhost
    server_name nomilkfor.me;

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

    # we will need to understand these settings
    location @ring {
        proxy_redirect off;
        proxy_buffering off;
        proxy_set_header Host $http_host;

        # what do I use here???
        # is http://ring; correct???
        proxy_pass http://ring;
    }

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

Note: This is my third question about the same subject but I still could not solve. Thanks for all the help.

  • Can I use Clojure with nginx?
  • How to serve clojure pages with nginx?

回答1:

server should contains ip:port of upstream server (jetty is your case).

root is the web root folder where your static files reside. Nginx will look for files there. Such folder usually named 'public' because it's accessible from outside. For example /js/bootstrap.min.js request will make nginx search for public/js/bootstrap.min.js (see try_files directive). If such file not found request will be forwarded to jetty server.