Spring boot app behind nginx redirects improperly

Mr. Wrong

I'm attempting to run my spring boot app behind nginx (on a dev machine), using a config file I found at Running a Spring Boot app behind nginx. Firefox however says it isn’t redirecting properly, and the Network tab in the javascript console confirms many 302 redirects.

This is nginx.conf I'm using:

http {
include       mime.types;
default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

keepalive_timeout  65;

upstream tomcat {
    server localhost:8080;
}

server {
    listen       80;
    server_name  localhost;

    access_log  /var/log/nginx/localhost.access.log  main;

    location / {
            proxy_pass              $scheme://tomcat/$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;
    }
}

Indeed, the mentioned log file has many lines like this:

127.0.0.1 - - [14/Apr/2018:11:25:38 +0200] "GET /login HTTP/1.1" 302 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"

It is redirecting to /login, which it should in fact:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{
    @Autowired private AuthService authService;

    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http
            .authorizeRequests()
            .anyRequest().authenticated()
        .and()
            .formLogin()
            .loginPage("/login")
            .permitAll()
        .and()
            .logout()
            .permitAll();

        http.csrf().disable();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
    {
        auth.userDetailsService(authService);
    }
}

Can someone tell me why calls to /login seem to be redirected to /login instead of returing the page on that url?

Thanks for any help!

Richard Smith

You should use:

proxy_pass http://tomcat;

The protocol scheme used to connect to Tomcat on port 8080 is fixed and should be http or https. The $scheme of this server block happens to be http, but there is no good reason to use that variable for the proxied connection.

By default, nginx will pass the requested URI to the proxied connection transparently, so there is no need to provide a URI on the proxy_pass statement, unless something has to be changed.

Using $request_uri is problematic when there are URL encoded characters (such as during a login sequence where a landing page is passed as a parameter) as it is the raw request and the characters have not been decoded. The proxy_pass directive is likely to encoded the encoded characters, turning the request into garbage.

See this document for more.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Running a Spring Boot app behind nginx

zk spring boot behind a proxy ( nginx )

Session timeout in Spring Boot app behind Apache

Spring boot swagger UI not working behind an nginx proxy

Spring Boot Too Many Redirects

Spring SAML 2.0 behind Nginx

Nginx behind a proxy: avoid auto-redirects to internal port

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

Spring Boot: OAuth endpoint redirects to 8443

Spring Boot Security Authentication - 302 Redirects

Behind a Reverse Proxy, Spring Security Login Redirects to Wrong Port

Nginx with Angular and Spring boot

NGINX - Spring Boot app ( "/etc/nginx/html/index.html" is not found )

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

Write behind with Apache Ignite in Spring boot application

Spring-Boot behind a network proxy

Spring Boot with embedded Tomcat behind Apache proxy

Docker + nginx + angular +Spring Boot

nginx default redirects and custom redirects

How do I force SSL on my Spring Boot app that uses OAuth2 on AWS ElasticBeanstalk and Nginx?

Docker Flask app behind nginx reverse-proxy sending 404

node.js proxy app in docker behind nginx proxy

rust yew app working on local laptop/pc not behind nginx

nodejs app accessible on port 3000 behind nginx reverse proxy

SSL SpringBoot App Docker container behind Nginx Proxy

Deploying Flask app with Gunicorn behind Nginx on Kubernetes cluster

Android Developer Options causing the app to work improperly

Internal App Camera Photos Improperly Saved to Gallery

Cant setup iredmail behind existing jwilder-nginx-proxy ERR_TOO_MANY_REDIRECTS 301 Moved Permanently