【问题标题】:Running a Spring Boot app behind nginx在 nginx 后面运行 Spring Boot 应用程序
【发布时间】:2014-08-04 18:47:53
【问题描述】:

我的服务器上启动并运行了一个 Spring Boot + MVC 应用程序,它绑定到 http://localhost:8000。

在 80 和 443 端口上有一个 nginx 代理(或者是反向代理,不确定名称)监听外部世界。根 ( / ) 将正确解析,但它下面的任何内容都不会解析并导致 404 错误(/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;
        }
}

非常感谢任何帮助。

【问题讨论】:

    标签: nginx spring-boot


    【解决方案1】:

    您不能以您尝试的方式将proxy_pass 与try_files 结合使用。正如您配置中的注释所述,try_files 指令使 nginx 查找与 URI 匹配的文件,然后查找与 URI 匹配的目录。如果两者都没有找到,它会以 404 响应。您可以在 nginx documentation 中阅读有关 try_files 的更多信息。

    从您的问题中不清楚您是否需要使用 try_files,因此修复配置的最简单方法是删除 try_files 行。

    【讨论】:

      猜你喜欢
      • 2018-10-01
      • 2020-02-08
      • 2017-08-27
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      • 2017-04-14
      • 2020-10-03
      相关资源
      最近更新 更多