Search code examples
spring-bootnginx-reverse-proxyjava-17

Spring Boot 3.0.0 not working on Reverse Proxy


I have a Spring Boot application running behind a NGinx reverse proxy and after I've upgraded Spring Boot version from 2.7.0 to 3.0.0 I'm getting a 404 error.

NGinx Reverse Proxy:

location /cpoprocesscontrol {
      proxy_pass  http://spring-docker-container-name:8014/;
      proxy_http_version                 1.1;
      # don't cache it
      proxy_no_cache                     1;
      # even if cached, don't try to use it
      proxy_cache_bypass                 1;

      # Proxy headers
      proxy_set_header Upgrade           $http_upgrade;
      proxy_set_header Connection        "upgrade";
      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-Proto $scheme;
      proxy_set_header X-Forwarded-Host  $host;
      proxy_set_header X-Forwarded-Port  $server_port;
      # Proxy headers

      # Proxy timeouts
      proxy_connect_timeout              3600s;
      proxy_send_timeout                 3600s;
      proxy_read_timeout                 3600s;
} 

I don't know if it is related but I've also added some libraries to swagger works:

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.0.2</version>
        </dependency>

I accessed the API in this way:

https://company-name/cpoprocesscontrol/workflow/getAllWorkflowContainingNamePageable?page=0&size=20

Now I'm getting:

{"timestamp":1674146900016, "status":404, "error":"Not Found", "path":"//workflow/getAllWorkflowContainingNamePageable"}

What would have changed from one version to another for this to stop working?

nginx/1.19.7


Solution

  • I needed to add the field X-Forwarded-Prefix on proxy headers:

    # Proxy headers
    proxy_set_header Upgrade           $http_upgrade;
    proxy_set_header Connection        "upgrade";
    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-Proto $scheme;
    proxy_set_header X-Forwarded-Host  $host;
    proxy_set_header X-Forwarded-Port  $server_port;
    proxy_set_header X-Forwarded-Prefix /cpoprocesscontrol;
    # Proxy headers
    
    

    And also added two fields on application.properties:

    "server.forward-headers-strategy": "framework",
    "server.use-forward-headers": true,
    

    Following this documentation: Running Behind a Front-end Proxy Server