简体   繁体   English

使用Nginx的springboot,访问api 404

[英]springboot with nginx, access api 404

I start a springboot server at 8080 port by command java -jar xxx.jar .When I access http://localhost:8080/test/redis , it always return the correct data, like {"a": 1} . 我通过命令java -jar xxx.jar在8080端口启动springboot服务器。当我访问http:// localhost:8080 / test / redis时 ,它总是返回正确的数据,例如{"a": 1}

But after I have set the nginx proxy config below 但是我在下面设置了nginx代理配置后

server {

    #other location config

    location /job/ {
        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://localhost:8080/;
    }
}

When I access http://localhost/job/test/redis , nginx directly returns 404 page. 当我访问http:// localhost / job / test / redis时 ,nginx直接返回404页面。 The second time I access the url, it returns the right data. 第二次访问该URL时,它将返回正确的数据。 And then 404 again, and then right data again, and so on. 然后再次输入404,然后再次正确输入数据,依此类推。 And http://localhost:8080/test/redis is always good. 而且http:// localhost:8080 / test / redis总是很好。

I can't figured it out, why it happens like that. 我不知道为什么会这样。 Is there anyone who can help? 有谁可以帮忙吗? Thanks so much! 非常感谢! By the way, if you need more details, just tell me in the comments 顺便说一句,如果您需要更多详细信息,请在评论中告诉我

Try removing the last / after http://localhost:8080 . 尝试删除http://localhost:8080之后的最后一个/

Update, 更新,

It also might be possible that the nginx proxy passes the full url, so your api is getting the whole /job/test/redis which doesn't exist. Nginx代理也有可能传递完整的URL,因此您的api会获取不存在的整个/job/test/redis In that case you might need to use regex to remove the job part and get the rest of the url to pass with the proxy. 在这种情况下,您可能需要使用正则表达式删除job部分,并使其余的URL与代理一起传递。

编辑nginx配置后,您是否重新加载nginx?

Do you reload nginx by nginx -s reload ? 您是否通过nginx -s reload加载nginx?

You can check error message by tail -f <error-log-file> 您可以通过tail -f <error-log-file>检查错误消息。

Can you paste all of your config info, probably your request match another route which can not found response file 您是否可以粘贴所有配置信息,可能您的请求与找不到响应文件的其他路由匹配

oh,I found out that the port for my tomcat in springboot is 8080, same as the nginx default port I have set, which has caused the strange problem. 哦,我发现springboot中用于tomcat的端口是8080,与我设置的nginx默认端口相同,这引起了奇怪的问题。

after I changed the tomcat port to 8081, the problem gone. 在将tomcat端口更改为8081之后,问题消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM