Running a Spring Boot app behind nginx

Jason La

I have a Spring Boot + MVC app up and running on my server and it's bound to http://localhost:8000.

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/ ).

I have this as my configuration:

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;
        }
}

Any help is very much appreciated.

Andy Wilkinson

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.

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pass command line args to Java app (Spring Boot) running in Docker

Spring boot web app not running on tomcat 9

Issues running example keycloak spring-boot app

Running a MVC app using Spring Boot + Hibernate + MySql

zk spring boot behind a proxy ( nginx )

Java app working when running spring boot but not with Apache

error while running spring boot app on localhost

Spring Boot Testing - Keep the App running for longer

Feign UnsatisfiedDependecyException when running my spring boot app

Spring Boot ApplicationListener not running at app startup

Spring Boot - long running application for non-web app

Kubernetes Ingress running behind nginx reverse proxy

Angular App running on nginx and behind an additional nginx reverse proxy

problems running a docker container behind nginx and/or firewall

Spring boot app behind nginx redirects improperly

Spring boot swagger UI not working behind an nginx proxy

securing spring boot app with mTLS - running on Swisscom App Cloud

JVM crashes when running Spring Boot web app

Running a Spring Boot app using ./gradlew appRun

Connection refused by Spring boot app running in different docker container

Running Spring Boot 2 app on SAP Neo Cloud Tomcat

What's the way of running KSQL from spring boot app

sockjs XHR requests 404ing with Meteor app running behind Passenger/NGINX

Session timeout in Spring Boot app behind Apache

JSF components are not rendered when running Spring Boot app on Eclipse Tomcat

Running a Spring Boot App on Azure WebJob with a Schedule

Should multiple Spring Boot App instances running behind a load balancer work with spring CAS user authentication?

Spring SAML 2.0 behind Nginx

Spring Boot + Postgresql not creating table when running app