NGINX反向代理,前缀无法与Spring Boot App正确使用

发布于 2025-01-27 02:20:19 字数 2887 浏览 4 评论 0 原文

嗨,Stackoverflow社区!

我正在尝试访问Docker Swarm Architecture中NGINX反向代理后面的Spring Boot应用程序。 当我转到 http://subdomain.example.com/myapp 时,这个问题的问题是 http://subdomain.example.com/login 带有404没有找到的页面而不是登录页面,而不是登录页面 http://subdomain.example.com/myapp/login

在我的nginx配置下面

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;

        }
}

我已经添加了这两行是我的Spring Boot应用程序。 /a>带有404的未找到页面,而不是 http://subdomain.example.com/ myApp/login

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

我尝试了所有这些解决方案而没有成功:

添加 x-forwarded-prefix

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

在弹簧启动应用程序中启用前向标头。特性。

重定向

重写URL

有人对此有解决方案吗?

此致

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清泪尽 2025-02-03 02:20:19

我通过将以下行添加到我的 application.properties 来解决问题。

server.servlet.context-path=/myapp

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

server.servlet.context-path=/myapp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文