8

In my project, web app is developed using Spring boot with default tomcat server. I am using NGINX as load-balancer and have configured my spring-boot-web-app in NGINX configuration as follows:

location /spring-boot-web-app {
     proxy_pass http://spring-boot-web-app/
}

http {
    upstream /spring-boot-web-app {
        server <IP_of_spring_boot_app>:<Port_of_spring_boot_app>
    }
}

Now lets say NGINX IP and port as nginx_ip and nginx_port respectively. Also working URL for my web app as: http://web_app_ip:web_app_port/rest/echo/hi

The above URL works fine. But when i try to hit same URI via NGINX it throws 404. URL used via NGINX as: http://nginx_ip:nginx_port/spring-boot-web-app/rest/echo/hi

Is there something i am missing?

Gangaraju
  • 4,406
  • 9
  • 45
  • 77
Sam
  • 859
  • 3
  • 12
  • 23
  • Did you check for the nginx logs? – Gangaraju May 05 '16 at 18:49
  • Yes. Even i see app log that it intercepting the URL. – Sam May 05 '16 at 18:50
  • Somehow i feel its more of CORS related problem. Do i need to set any header value? – Sam May 05 '16 at 18:51
  • 1
    You may need to setup proxy_pass_reverse https://www.nginx.com/resources/wiki/start/topics/examples/likeapache/, http://stackoverflow.com/questions/12847771/configure-nginx-with-proxy-pass – Gangaraju May 05 '16 at 18:52
  • is $remote_add and $host should be IP of my web-app? – Sam May 05 '16 at 19:14
  • Keep the variables as is. Nginx resolves them – Gangaraju May 05 '16 at 19:26
  • did not work for me.. i also tried with replacing with correct IP. One thing- I have vagrant provisioned VM where my web app and nginx are running as docker container. – Sam May 05 '16 at 19:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111160/discussion-between-gangaraju-and-sam). – Gangaraju May 05 '16 at 19:40

1 Answers1

12

This works for me. Can you try this?

  1. Running tomcat

    docker run -d -p 8080:8080 --name=tomcat tomcat:8 
    
  2. Running nginx

    docker run -d -p 80:80 --link tomcat:tomcat --name=nginx nginx
    
  3. Go inside nginx container and update the conf

    docker exec -it nginx bash
    

    /etc/nginx/nginx.conf:

    server {
       listen 80 default_server;
      server_name subdomain.domain.com;
      location / {
          proxy_pass http://tomcat:8080;
          proxy_set_header Host      $host;
          proxy_set_header X-Real-IP $remote_addr;
      }
    }
    
  4. Restart nginx service

    nginx -s reload
    
  5. Access the tomcat through nginx from host browser. You may need to add entry to /etc/hosts

    http://subdomain.domain.com
    

Complete nginx conf: nginx.conf

Gangaraju
  • 4,406
  • 9
  • 45
  • 77
  • Really appreciate your help "Gangaraju". But the docker command you proposed i cant use. Because i am using consul to do it and define dependency. – Sam May 05 '16 at 20:47
  • It may not fit into your environment. But try to debug your issue, by comparing with these steps and check what you are missing. I would suggest you to run these containers in other environment to cross verify the nginx configuration/ for better understanding – Gangaraju May 05 '16 at 20:56
  • Thank you very much Gangaraju for your prompt help..Really appreciate!1 – Sam May 05 '16 at 21:14
  • 2
    Accepted the answer as it gave some route to debug. Will post the solution once i get it. – Sam May 05 '16 at 21:15