在nginx后面运行Spring Boot应用程序 [英] Running a Spring Boot app behind nginx
问题描述
我在服务器上启动并运行了一个Spring Boot + MVC应用程序,它已绑定到http://localhost:8000
.
I have a Spring Boot + MVC app up and running on my server and it's bound to http://localhost:8000
.
有一个nginx代理(或者是反向代理,不确定名称)在端口80和443上侦听外界.根(/)可以正确解析,但其中的任何内容都无法解析.并导致404错误(/someControllerName/action,/images/,/css/).
There is an nginx proxy (or is it a reverse proxy, not sure about the name) that listens to the outside world on ports 80 and 443. The root ( / ) will resolve correctly, but anything under it will not resolve and results in a 404 error ( /someControllerName/action, /images/, /css/ ).
我将其作为配置:
upstream jetty {
server localhost:8000;
}
server {
listen 80;
server_name domain.com;
return 301 http://www.domain.com$request_uri;
}
server {
listen 443;
server_name domain.com;
ssl_certificate /etc/nginx/ssl/ssl.crt;
ssl_certificate_key /etc/nginx/ssl/ssl.key;
return 301 https://www.domain.com$request_uri;
}
server {
listen 80 default_server;
listen 443 ssl;
root /usr/share/nginx/html;
index index.html index.htm;
server_name www.domain.com localhost;
#ssl on;
ssl_certificate /etc/nginx/ssl/ssl-unified.crt;
ssl_certificate_key /etc/nginx/ssl/ssl.key;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
proxy_pass $scheme://jetty/$request_uri;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
try_files $uri $uri/ =404;
}
}
非常感谢您的帮助.
推荐答案
您无法以尝试的方式将proxy_pass
与try_files
组合在一起.正如配置中的注释所描述的那样,try_files
指令使nginx查找与URI匹配的文件,然后寻找与URI匹配的目录.如果找不到任何一个,它将以404响应.您可以在 nginx文档.
You can't combine proxy_pass
with try_files
in the way that you have attempted. As the comment in your configuration describes, the try_files
directive causes nginx to look for a file that matches the URI and then look for a directory that matches the URI. If it doesn't find either, it responds with a 404. You can read more about try_files
in the nginx documentation.
目前尚不清楚您是否需要使用try_files
,因此最简单的解决方案是删除try_files
行.
It's not clear from your question that you need to use try_files
at all so the simplest way to fix your configuration is to remove the try_files
line.
这篇关于在nginx后面运行Spring Boot应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!