1

I have an issue with my Spring Boot API, which I use in combination with Docker and Nginx as a reverse proxy.

At the moment I am setting up a website together with a REST webservice. The webserver and the REST webservice (Spring Boot) are running in two different Docker containers. In order to provide HTTPS (encryption) I use NGINX as a reverse proxy (which is also running as a Docker-Container).

Now I am using the following setup in my nginx.conf to enable outside access to my Spring API.

upstream spring-backend {
    server spring:8081;
}

# ... some other configuration stuff

server {
    listen               7332;
    ssl                  on;

    #   ... ssl-config

    # all other traffic
    location / {
        # Specify the fields added/redefined to the request header passed to the proxied server.
        proxy_set_header        Upgrade $http_upgrade;
        proxy_set_header        Connection $connection_upgrade;
        # Timeout for reading a response from the proxied server.
        proxy_read_timeout      3600; # keep open even without any transmission
        proxy_pass              http://spring-backend;
    }
}

While this works well, I am encountering the issue that API URIs Spring creates for the created entities look as follows: http://spring-backend/{entity}/{id}.

This apparently cannot be accessed from another computer consuming the website and it's associated webservice. Instead I would need the entries to be https://{the-url-of-the-webserive}:7332/{entity}/{id}.

However I am not sure whether it is possible to get this resolution from the name-resolution NGINX uses (as it should replace server-backend with spring:8081) and the one of Docker (should replace at least the spring part with the actual adress).

Marco N.
  • 21
  • 1
  • 1
  • 4

1 Answers1

0

Actually this can be solved by adding X-Forward-* headers to the location part of server.

This should than look as follows:

location / {
    # Specify the fields added/redefined to the request header passed to the proxied server.
    proxy_set_header        Upgrade $http_upgrade;
    proxy_set_header        Connection $connection_upgrade;

    #--------------------------------- SOLUTION -------------------------------------------
    proxy_set_header        Host               $host;
    proxy_set_header        X-Real-IP          $remote_addr;
    proxy_set_header        X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Host   $host:443; #or $host:7332 in my case
    proxy_set_header        X-Forwarded-Server $host;
    proxy_set_header        X-Forwarded-Port   443; #or 7332 in my case
    proxy_set_header        X-Forwarded-Proto  https;
    #------------------------------- SOLUTION-END -----------------------------------------

    # Timeout for reading a response from the proxied server.
    proxy_read_timeout      3600; # keep open even without any transmission
    proxy_pass              http://spring-backend;
}

That code snippet has been taken from plone.lucidsolutions.co.nz where you can also find further information on this solution.

Marco N.
  • 21
  • 1
  • 1
  • 4