Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a Spring Boot app behind nginx

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.

like image 215
Jason La Avatar asked Aug 04 '14 18:08

Jason La


People also ask

Can we deploy Java application on NGINX?

The application files need to be deployed into a folder, because the Java web application folder must be specified for NGINX so that it can find and directly send static files like images (.

Can I use NGINX as a forward proxy?

By using the nginx forward proxy we can masking the location and IP for gaining access to services. Nginx forward proxy will continuing the request on behalf of the client. At the time when the host server will accept the request then only we can see the IP of the nginx proxy server.

Can we deploy jar in NGINX?

Nginx is a HTTP server. It can't run Jar files (or any Java code).

Can NGINX be used as reverse proxy?

Nginx is an open source web server that can also serve as a reverse proxy. Apart from being used to host websites, it's also one of the most widely used reverse proxy and load balancing solutions.


1 Answers

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.

like image 162
Andy Wilkinson Avatar answered Oct 08 '22 19:10

Andy Wilkinson