0

I have dynamic DNS set up and want to be able to access different computers from outside my home. Currently I have a raspberry pi running nginx on 192.168.1.2, with my router at 192.168.1.1. I also have port 80 forward from the router to the raspberry pi, so simply hitting the dynamic DNS URL shows me the phpinfo() page on my pi landing page.

Next I want to be able to go mydns.com/device1 and maintain that URL while redirecting under the hood to the other device.

In other words, I will see http://mydns.com/device1, but I will be access http://192.168.1.3 internally. Additionally, if I type mydns.com/device1/login it will redirect internally to http://192.168.1.3/login.

I'trying to use sub_filter like so:

sub_filter_once off;
sub_filter_types text/html;
sub_filter ""http://192.168.1.3" "http://192.168.1.2"
proxy_pass http://192.168.1.3/;
proxy_set_header Host $host;

This is working to some degree but not fully. The paths aren't redirecting, eg files in sub-folders. For example:

I want this: http://mydns.com/device1/style/basic.css

But get this: http://mydns.com/style/basic.css

This causes a 404, as can be seen in the Console view in Chrome. I get this error:

Failed to load resource: the server responded with a status of 404 (Not Found)

This is on the path: http://mydns.com/style/basic.css but I need http://mydns.com/device1/style/basic.css.

I can access the CSS file in the address bar by typing in http://mydns.com/device1/style/basic.css. so I know its accessible, I just can't get NGINX to forward/rewrite these folders.

Can anyone help? Thanks

3
  • This is a common problem and may not be solvable if you have no control over device1. Can you use subdomains instead? – Richard Smith Jan 30 '20 at 8:57
  • well device1 and all the other devices would be consumer equipment, ranging from routers to hard drives to linux-based TV boxes. I have control of them in the sense that they are in my house, but I don't have control of the HTML produced by their web UIs. In terms of a subdomain, I am accessing my router via Netgears own DynDNS service, in the form of {{user}}.mynetgear,com, where {{user}} is unique per account. So I can't do a subdomain. Only appending a path at the end. – eoinzy Jan 30 '20 at 9:21
  • I was thinking of adding 1 server per device and listening on a different port, then redirect mydns.com/device1 to mydns.com:81 but I'm not sure if that would give the same issue. – eoinzy Jan 30 '20 at 9:21
0

It could have several reasons:

  • either something rewrites your URI before it enters location with proxy_pass;
  • or it is supplied as matched sub-URI in some locations processed before;
  • or (in your nginx version) the slash / after IP in proxy_pass directive causes that nginx takes the slash as a proxy URI and rewrites it before sending.

One cannot see what exactly happens on your side without to see your whole config. Or you can try to enable debug log (to see what causes this).

Anyway related to above mentioned grounds you've 3 ways to solve that:

  • either you'd avoid rewrite or you have to add additional rewrite ... break before proxy_pass to extend the URI;
  • or you can try to define it as proxy_pass http://192.168.1.3/device1/; or with some (mapping) variable for like proxy_pass http://192.168.1.3/$device/; or even with complete originated URI like proxy_pass http://192.168.1.3$request_uri; (note there is no / here).
  • or you can try to remove trailing slash, so proxy_pass http://192.168.1.3; (without slash) instead of proxy_pass http://192.168.1.3**/**; (with slash)
    In this case "the request URI is passed to the server in the same form as sent by a client when the original request is processed" (if no rewrites take place), or "the full normalized request URI is passed when processing the changed URI" (if some rewrites are happened or you have some interim locations with alias and try_files ... @proxy passed).

Take a look at proxy_pass documentation for all possible variants of proxy_pass usage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.