'Nginx docker container to reverse proxy to a java app

An nginx container is running on an ubuntu vps, the command used was simply docker run -it -d=true --restart=unless-stopped --name=nginx -p=8080:80 nginx. I tried editing the default.conf file, currently it is :

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /color/green {
        proxy_pass http://127.0.0.1:8345;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }


}

My spring app is running with 2 endpoints correctly working,

$ java -jar colors-1.0.jar 

o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8345 (http) with context path ''

Without nginx, I can call the vps directly on my app's port 12.345.67.890:8345/color/green this works fine. However I need to call it on port 80 but this doesn't work, I'm clueless how to configure the nginx or debug this..

Thanks!



Solution 1:[1]

In a container, 127.0.0.1 is the container itself, so

proxy_pass http://127.0.0.1:8345;

tries to pass the request on to the nginx container at port 8345, where nothing is listening.

If I understand you correctly, then Tomcat is running on the host. If the host is Linux, then you can reach the host at 172.17.0.1. So change the address to

proxy_pass http://172.17.0.1:8345;

and it should work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hans Kilian