Nginx Reverse Proxy Websocket Authentication - HTTP 403 Nginx Reverse Proxy Websocket Authentication - HTTP 403 nginx nginx

Nginx Reverse Proxy Websocket Authentication - HTTP 403


I solved the problem by myself. Basically, Nginx needs to pass some additional header values if you want to use Websocket and Spring Security. The following lines need to be added to location section in your Nginx config:

    # Pass the csrf token (see https://de.wikipedia.org/wiki/Cross-Site-Request-Forgery)    # Default in Spring Boot and required. Without it nginx suppresses the value    proxy_pass_header X-XSRF-TOKEN;    # Set origin to the real instance, otherwise a of Spring security check will fail    # Same value as defined in proxy_pass    proxy_set_header Origin "http://testsysten:8080";  


The accepted solution did not work for me although I was using a very classical HTTPS configuration:

server {    listen 443 ssl;    location /ws {        proxy_http_version 1.1;        proxy_set_header Upgrade $http_upgrade;        proxy_set_header Connection "upgrade";        proxy_set_header Host $http_host;        proxy_pass http://127.0.0.1:8888;    }...

The problem is that Spring checks the origin and specifically that code was causing me trouble:

// in org.springframework.web.util.UriComponentsBuilder.adaptFromForwardedHeaders(HttpHeaders):        if ((this.scheme.equals("http") && "80".equals(this.port)) ||                (this.scheme.equals("https") && "443".equals(this.port))) {            this.port = null;        }

In that code the scheme is 'http' and the port is 8888, which is not discarded because it is not the standard port.

The browser however hits https://myserver/ and the 443 port is omitted because it is the default HTTPS one.

Therefore the ports do not match (empty != 8888) and origin check fails.

Either you can disable origin checks in Spring WebSockets:

registry.addHandler( resgisterHandler(), "/ws" ).setAllowedOrigins( "*" );

or (probably safer) you can add the scheme and port to the NGINX proxy configuration:

    proxy_set_header X-Forwarded-Proto $scheme;    proxy_set_header X-Forwarded-Port $server_port;

If you are interested, those headers are read in

org.springframework.web.util.UriComponentsBuilder.adaptFromForwardedHeaders(HttpHeaders)

For Spring Boot 2.2.2+

Starting with Spring Boot version 2.2.2 you should be adding following setting for these X-Forwarded-* headers to be taken into account:

server.forward-headers-strategy=native

(in application.properties for instance)


I had faced a similar problem. I was unable to use the basic Spring Security authentication with NGINX. Apart from setting the proxy_pass_header X-XSRF-TOKEN;, I also had to set underscores_in_headers on;, since NGINX by default does not allow headers with underscores and the CSRF token is named _csrf.

So my final configuration file looked like this:

server {    underscores_in_headers on;    listen 80 default_server;    listen [::]:80 default_server ipv6only=on;    root /usr/share/nginx/html;    index index.html index.htm;    # Make site accessible from http://localhost/    server_name localhost;    location / {            # First attempt to serve request as file, then            # as directory, then fall back to displaying a 404.            try_files $uri $uri/ =404;            # Uncomment to enable naxsi on this location            # include /etc/nginx/naxsi.rules    }    location /example/ {            proxy_pass_header X-XSRF-TOKEN;            proxy_pass      http://localhost:8080/;    }}