Nginx as a proxy to Jetty

apache-2.2jettymod-jknginxtomcat

Pardon me, this is my first attempt at Nginx-Jetty instead of Apache-JK-Tomcat.

I deployed myapp.war file to $JETTY_HOME/webapps/, and the app is accessible at the url:

http://myIP:8080/myapp

I did a default installation of Nginx, and the default Nginx page is accessible at myIP

Then, I modified the default domain under /etc/nginx/sites-enabled to the following:

server {
        listen   80;
        server_name mydomain.com;

        access_log  /var/log/nginx/localhost.access.log;

        location / {
                #root   /var/www/nginx-default;
                #index  index.html index.htm;
                proxy_pass http://127.0.0.1:8080/myapp/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   /var/www/nginx-default;
        }
}

Now I get the index page of mypp (running in jetty) when I hit myIP, which is good. But all the links are malformed. eg. The link to css is mydomain.com/myapp/css/style.css while what it should have been is mydomain.com/css/style.css. It seems to be mapping mydomain.com to 127.0.0.1:8080 instead of 127.0.0.1:8080/myapp/

Any idea what am missing? Do I need to change anything on the Jetty side too?

Best Answer

It is now not nginx configuration problem but your application or Jetty configuration problem. Your application, running under Jetty still 'thinks' its URL is /myapp/ not /, you should change your application configuration to use right base URL.

Theoretically the server could rewrite the links when passing the application responses back to the clients (Apache with right module can do that, I am not sure about nginx), but that is a good idea only if you cannot fix the application or server you proxy-pass to.