部署在Tomcat上的Spring Boot应用程序创建了错误的URL。

huangapple go评论83阅读模式
英文:

Deploed Spring Boot application on Tomcat create bad URLS

问题

这是我的应用程序属性文件:

server.forward-headers-strategy=framework

spring.freemarker.expose-request-attributes=true
spring.freemarker.suffix= .ftl

spring.data.mongodb.authentication-database=aibolitDB
spring.data.mongodb.username=admin
spring.data.mongodb.password=admin
spring.data.mongodb.database=aibolitDB
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost

springdoc.swagger-ui.path=/swagger-ui-aibolit.html

spring.servlet.multipart.max-file-size=256MB
spring.servlet.multipart.max-request-size=256MB
spring.servlet.multipart.enabled=true

app.url.base=http://aibolitbackend.unitbeandev.com/

ub.vet.jwtSecret= bezKoderSecretKey
ub.vet.jwtExpirationMs= 86400000

logging.level.org.springframework.web = trace
logging.level.org.apache = trace

在尝试发送 API 请求时,我收到了以下错误:

部署在Tomcat上的Spring Boot应用程序创建了错误的URL。

如您所见,它试图在 /AibolitBackend/ 路径中查找。这是我的 NGINX 配置属性:

location / {
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8080/AibolitBackend/;
}

我忘记设置了什么?我需要在 Tomcat 主机上添加一些内容吗?我不能在基本路径上进行设置,因为这里将有3个应用程序。

英文:

I have Spring Boot application& It is REST API for mobile app. So it works ok localy, but after deply on tomcat remote server, it have problems.
So, here my app in tomcat application manager

部署在Tomcat上的Spring Boot应用程序创建了错误的URL。

Here is properites file

server.forward-headers-strategy=framework


spring.freemarker.expose-request-attributes=true
spring.freemarker.suffix= .ftl
# location of the swagger json


spring.data.mongodb.authentication-database=aibolitDB
spring.data.mongodb.username=admin
spring.data.mongodb.password=admin
spring.data.mongodb.database=aibolitDB
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost

springdoc.swagger-ui.path=/swagger-ui-aibolit.html



spring.servlet.multipart.max-file-size=256MB
spring.servlet.multipart.max-request-size=256MB
spring.servlet.multipart.enabled=true

app.url.base=http://aibolitbackend.unitbeandev.com/

ub.vet.jwtSecret= bezKoderSecretKey
ub.vet.jwtExpirationMs= 86400000

logging.level.org.springframework.web = trace
logging.level.org.apache = trace

And When
I try to send api request, it gives me an error

部署在Tomcat上的Spring Boot应用程序创建了错误的URL。

As you can see, it try to find in /AibolitBackend/ path
Here my NGINX properties

 location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://127.0.0.1:8080/AibolitBackend/;
    #       try_files $uri $uri/ =404;
    }

What I forgot to set up? Need I add some thing to Tomcat hosts?? And I cant to make it on Base / path, because here will by 3 apps.

答案1

得分: 1

你在同一个位置使用了try_filesproxy_pass指令,这没有任何意义。通常在nginx中,为了提供静态文件并将其他请求传递到后端,你应该使用类似以下的配置:

location /
    # 首先尝试提供静态文件请求,然后将请求传递到后端
    root <设置正确的静态文件根目录!>;
    try_files $uri @tomcat;
}

location @tomcat {
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8080;
}

使用proxy_pass http://127.0.0.1:8080/AibolitBackend/; 会将像/some/path这样的任何请求转换为在传递到后端之前变为/AibolitBackend/some/path。我不理解你想通过/AibolitBackend/前缀实现什么目的。

英文:

You have a weird mix of try_files and proxy_pass directives in the same location. That doesn't make any sense. Usually to serve static files with nginx and pass other requests to the backend you should use something like

location /
    # First attempt to serve request as static file, then pass request to the backend
    root &lt;set_correct_root_for_your_static_files_here!&gt;;
    try_files $uri @tomcat;
}
location @tomcat {
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8080;
}

With proxy_pass http://127.0.0.1:8080/AibolitBackend/; you get any request like /some/path transformed to /AibolitBackend/some/path before going to the backend. I didn't understand what do you want to achieve with this /AibolitBackend/ prefix.

huangapple
  • 本文由 发表于 2020年10月23日 16:49:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64496857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定