'Nginx reverse proxy with prefix not working correctly with spring boot app

Hi Stackoverflow community !

i am trying to access to my spring boot app behind nginx reverse proxy in docker swarm architecture. the problem that when i go to http://subdomain.example.com/myapp it redirects me to http://subdomain.example.com/login with 404 no found page instead of the login page on http://subdomain.example.com/myapp/login

Below my nginx configuration

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

    server_name subdomain.example.com www.subdomain.example.com;

    access_log  /var/log/nginx/subdomain.example.com-access.log;
    error_log   /var/log/nginx/subdomain.example.com-error.log;


    location /myapp/ {

        proxy_pass    http://docker_service_name/;

        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;
        proxy_set_header    X-Forwarded-Server $host;
        proxy_set_header    X-Forwarded-Port   $server_port;
        proxy_set_header    X-Forwarded-Proto  $scheme;
        proxy_set_header    X-Forwarded-Prefix /myapp;


        add_header    'Access-Control-Allow-Origin' '*' always;
        add_header    'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header    'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept' always;
        add_header    'Access-Control-Allow-Credentials' 'true' always;

        }
}

i have added those two lines to my spring boot application.propoerties but keeping redirect to http://subdomain.example.com/login with 404 no found page instead of the login page on http://subdomain.example.com/myapp/login

server.forward-headers-strategy=framework
server.use-forward-headers=true

i tried all those solutions without sucess :

Adding X-Forwarded-Prefix

https://library.humio.com/stable/docs/installation/cluster/nginx-reverse-proxy/

Enable forward headers in Spring boot application.properties.

Running spring boot application behind nginx - missing location prefix when redirect

rewrite URL

https://developpaper.com/nginx-configure-reverse-proxy-to-remove-prefix/

Does someone have a solution for that ?

Best Regards



Solution 1:[1]

I resolved the problem by adding the following line to my application.properties

server.servlet.context-path=/myapp

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 DevOpsien