The Reverse Proxy, the Mobile Browser, and the “Upgrade” Header

About the Problem: I was having some issue with displaying a simple WordPress site properly on a mobile browser. Not the desktop browser, there everything was working fine, it was really only on a mobile browser, which was weird on its own.

Another puzzling fact: As soon as I disabled the cache on my NGINX reverse proxy, everything was working fine. Or so I thought…

The Reverse Proxy

Now what do we blame first? Right, the reverse proxy who handles the request. This led me down a trail to search for a reason why NGINX can’t handle mobile browsers anymore. After all, everything was working fine before. And of course “I didn’t change a thing(TM).

Turns out I’m the first one with exactly this issue. This can’t be right? Everybody uses NGINX, someone must have had the same problem… let’s keep digging.

The Mobile Browser

On my iPhone I used Safari at first, and knowing Safari I expected an issue there as well, so let’s try Firefox.

Ah, (insert swear word)… well, for once it’s not Safari. I was already happy to annoy Apple with yet another feedback.

I’m back to digging.

The “Upgrade” Header

After a long time of searching, I found… OK, let’s rather say “stumbled” over a possible solution. Someone had an issue with the “Upgrade” header which was sent to the browser.

proxy_pass_header Upgrade;

The “Upgrade” header is mostly used in combination with web sockets. Having just had an issue with a service which required the configuration of web sockets, I thought I was clever by just adding the header to my global NGINX config, to prevent such issues in the future. Well, I should not have done that.

The best solution I then came up with, which also solved the problem across all sites I was having, was to hide the Upgrade header globally and only add it to the sites which were needing web sockets.

proxy_hide_header Upgrade;
proxy_set_header Upgrade $http_upgrade;

While this solution is far from elegant, it works. And with services which require web sockets, I can still use the proxy_pass_header in the configuration where I need to.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.