6

I've been trying to set up nginx as a proxy to jetty. I have one laptop running ubuntu server. Now I made jetty working at localhost:8080 and it serves the home page for the app at http://192.168.1.5:8080/my-webapp-0.1.0-standalone/.

I configured nginx like this (I adapted it from this page):

server {
  listen 80;
  server_name nomilkfor.me;
  rewrite ^(.+?)/?$ http://nomilkfor.me$1 permanent;
}

server {
  listen 80;
  server_name www.nomilkfor.me;
  root /usr/share/nginx/html;

  location / {
    try_files $uri @my-webapp;
  }

  location @my-webapp {
    proxy_pass http://localhost:8080;
  }
}

And I can connect to nginx from my home network and I see the nginx welcome screen.

I also tried $ sudo netstat -tanpl|grep nginx

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3264/nginx: worker 

and I see that nginx is listening at port 80.

But when I try to load nomilkfor.me I get "Chrome could not connect to nomilkfor.me" error.

What am I doing wrong?



EDIT

I created a very simple configuration, and this one too servers the index.html in /usr/share/nginx/ instead of the app through jetty:

server {
    listen 80;
    server_name nomilkfor.me;

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

    location / {
        proxy_pass http://127.0.0.1:8080;
    }

    location /html { 
        root /usr/share/nginx/;
    }
}


EDIT 2

It seems that nginx is using another conf file than I think. I added a typo to the conf file /etc/nginx/sites-available/nomilkfor.me (I removed a closing curly brace) and ran $ nginx -s reload and it compiled without error and showed the nginx splash page on the browser. Can there be another conf file somewhere? Is there a way to find which conf file nginx is using?



EDIT 3

As per Pazis comment I added root but not sure exactly where it should be or what it should be. I added a few suggestions. Which one is correct? /usr/share/nginx/html is where I have the index.html.

server {
    listen 80;
    server_name nomilkfor.me;
    # root /home/z/jetty/jetty-dist/webapps
      root /usr/share/nginx/html;

    location / {
        proxy_pass http://127.0.0.1:8080;
    }

    #location /html {
    #    root /usr/share/nginx/;
    }
}


EDIT 4

This is my jetty conf file. It is in /home/z/jetty/jetty-dist/jetty-distribution-9.1.0.v20131115/demo-base/webapps/my-webapp.xml

<Configure class="org.eclipse.jetty.webapp.WebAppContext">

  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- Required minimal context configuration :                        -->
  <!--  + contextPath                                                  -->
  <!--  + war OR resourceBase                                          -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <Set name="contextPath">/test</Set>
  <Set name="war"><Property name="jetty.webapps" default="."/>/my-webapp-0.1.0-standalone.war</Set>
  • Did you try to restart the Nginx service? – Ronny Dec 25 '13 at 19:13
  • 1
    I think the problem is with your rewrite rule. Have you tried www.nomilkfor.me? – Ali Hashemi Dec 25 '13 at 20:16
  • Could not connect to www.nomilkfor.me either. What can be the problem with the rewrite rule? – Zeynel Dec 25 '13 at 21:03
  • @Pazis I changed my conf file to a simpler one. I've added it to the question. – Zeynel Dec 26 '13 at 1:43
  • do you change /etc/nginx/conf.d/default.conf? And your new configuration has no root. – Ali Hashemi Dec 26 '13 at 9:22
8

Can you try the following steps?

  1. Make sure that the nginx.conf file in parent nginx directory has the include directive pointing to the default file in the sites-available directory

  2. If you need to proxy to another service (e.g. jetty), use the upstream option (in the sites-available/default file) as shared below. You can refer to the upstream from the server section.

Once you have the basic config working, you can check the rewrite option and whether anything needs to be done for the hostname. Hope it helps.

Nginx.conf:

include /etc/nginx/sites-enabled/*;

In the sites-available directory (default file):

upstream nomilkforme {  
        server 0.0.0.0:8080; ##nomilkforme running on 0.0.0.0:8080;
        keepalive 500;
}

server {
        listen       80;
        server_name  localhost;

        #access_log  logs/host.access.log  main;

        location / {
                proxy_redirect off;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header Host $http_host;
                proxy_set_header X-Nginx-Proxy true;
                proxy_set_header Connection "";
                proxy_http_version 1.1;
                proxy_pass http://nomilkforme
        }

Also, one can check the nginx conf file (before reloading/restarting) as follows: nginx -t -c /etc/nginx/nginx.conf

Update: Can you check if jetty was running on 0.0.0.0:8080 when you tried the nginx config (perhaps confirm using netstat). Also, can you share the access/error logs from nginx when you access the URL via the browser?

  • Thanks. I just checked and include /etc/nginx/sites-enabled/*; is included in nginx.conf in the http block. Is this correct? – Zeynel Dec 28 '13 at 11:28
  • I decided not to use jetty, I am now trying to make it work with this setup https://groups.google.com/d/msg/clojure/lnGvHyRzX7w/N8eDnwJPXuAJ but so far I am unable to start it with upstart. Any thoughts? – Zeynel Dec 28 '13 at 11:30
  • I'll update my answer with some more information. – ali haider Dec 28 '13 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.