thenounoursk
thenounoursk

Reputation: 91

Variable in nginx proxy_pass causes MIME type issue

I've googled the topic at length all evening and can't seem to make sense of this. My situation is the following: I have a NAS on my network (hostname MYHOSTNAME) and I also have a nginx reverse proxy, on another machine. I want to use a variable for the proxy_pass section of my configuration file such that in case the NAS is offline when the proxy starts, it doesn't crash. Basically:

location /MYNAS {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_redirect   off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://MYHOSTNAME:1234/; 

works just fine. But the following:

location /MYNAS {
        set $VAR_HOSTNAME MYHOSTNAME;
        resolver 192.168.x.x;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_redirect   off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://$VAR_HOSTNAME:1234/;

gives me trouble. The webpage loads an empty page, and in the browser console, I can see:

Refused to execute script from 'something' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
mydynamicdns/:1 Refused to execute script from 'somethingelse' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
desktop.js?v=1562143318:3 Uncaught TypeError: Cannot read property 'diskless' of undefined
    at _S (desktop.js?v=xxx:x)
    at aa.defaultCSSSelectors (desktop.js?v=xxx:x)
    at desktop.js?v=xxx:x
    at ext-all.js?v=xxx:x
    at b (ext-all.js?v=xxx:x)

I'm at a loss for new ideas of how to make this work... Thanks in advance guys, any help would be greatly appreciated.

Upvotes: 5

Views: 2314

Answers (1)

thenounoursk
thenounoursk

Reputation: 91

I don't think I can quite explain the technical details of why, but by turning on debugging in nginx and comparing with the usecase that works, I eventually figured that I needed to nullify the /MYNAS/ piece. The key is the rewrite line below:

location /MY_NAS/ {
        set $MY_NAS http://MYNAS:1234;
        resolver 192.168.x.x;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_redirect   off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        rewrite /MY_NAS/(.*) /$1 break;
        proxy_pass $MY_NAS;
    }

Hopefully that will help someone else one day and save you the headache.

Upvotes: 4

Related Questions