0

I'm using Node.js with Express to make web applications which I run on a server in the cloud. Express apps typically listen on a specific port (3000 by default, but you can specify a port). So if I have multiple applications running on the server they will each listen on a unique port number, all well and good. But you don't want to reach the app with a port number in the URL, that's ugly.

I use Nginx on the server to perform a reverse-proxy. It can forward a path in a URL to a port where the appropriate application is listening.

So if "myApp" is listening on port 3010, it can be reached via the URL "https://myserver/myApp", which Nginx will route to port 3010 on the server. However on my local machine, which is where I do my development and testing, the app must be reached at "localhost:3010" because there is no proxy.

So here's the problem. When an index.html page loads up in a browser it pulls in CSS and JavaScript files from the server. The typical syntax is;

<link rel="stylesheet" href="/stylesheets/myStyle.css">

That pathname is supposed to be relative to the base directory of the app, and the appropriate prefix should get prepended to the request URL. This works when running on the local machine with "localhost:3010", the stylesheets and scripts get pulled in. But running on the remote server with the URL "https://myserver/myApp" the files won't load, I get a 404. I'm having to prepend the URL route name to the file pathnames in order for the requests to be proxy-routed properly like so;

<link rel="stylesheet" href="/myApp/stylesheets/myStyle.css"">

Unfortunately that pathname doesn't work on localhost where there is no myApp directory. This means that I don't have portable code. It has to be different when running locally or on the server, and the code has to know what the route name is on the server. That's bad.

Maybe I am configuring something incorrectly? This is what the routing clause looks like in Nginx;

        location /myApp {
                proxy_pass http://localhost:3010/;
                proxy_http_version 1.1;
                proxy_set_header Host $host;
                proxy_set_header X-NginX-Proxy true;

                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
        }
Capstan
  • 11
  • 2

2 Answers2

1

It turns out that the pathname prefix of the files that index.html is attempting to pull in from the remote server is very picky, and that small distinction was the problem (at least in my case).

Multiple prefixes work when running locally and being accessed in the browser by port number;

<link rel="stylesheet" href="/stylesheets/myStyle.css">

or <link rel="stylesheet" href="stylesheets/myStyle.css">

and also <link rel="stylesheet" href="./stylesheets/myStyle.css">

But the only syntax that seems to work on a remote server behind Nginx and ALSO locally is the "./" syntax; <link rel="stylesheet" href="./stylesheets/myStyle.css">

Capstan
  • 11
  • 2
0

First of all check your express static files configuration to garantee that you're serving your static files correctly.

About the nginx configuration, follow this discuss here. It's dealing the same problem as you: Express + Nginx. Can't serve static files and I just believe that you can fix your problem following the same solution

Julien Ambrosio
  • 311
  • 2
  • 6
  • Thanks for the suggestion, but I don't think it is quite the same problem. Express does serve up static files with no special configuration. I think I've got something wrong with my Nginx setup. – Capstan Dec 12 '22 at 15:19
  • Did you see the link that I put in my last comment? It's the same thing that you're trying to do. I prefer to send the link instead of try to change your nginx config – Julien Ambrosio Dec 12 '22 at 18:49
  • Yes I did look through all the answers in that link. The static files are not the problem, Express will serve them with no special code. The problem seems to be that the redirection of file requests to the appropriate port does not occur for files requested from within the index page unless the specific route prefix is prepended to the pathname as I describe above. I actually cloned my server today so I could safely experiment with many options on the clone, cannot find a way to force the appropriate routing in Nginx. – Capstan Dec 12 '22 at 21:18
  • During the week I will create a mvp test to do the same that you need and I will update my answer. Can you please add an answer if you fix your problem? – Julien Ambrosio Dec 12 '22 at 21:39
  • I definitely will post a solution (or at least a workaround) if I can figure this out. Thanks so much for your kind help. – Capstan Dec 12 '22 at 23:58
  • I've made some headway on this. The problem seems to be related to the forward slash in the pathname. There is different behavior between and – Capstan Dec 13 '22 at 22:06
  • It's make sense. If it works, share the answer here =D – Julien Ambrosio Dec 13 '22 at 22:12
  • Yes I did add an answer. Please see above, and thank you for your interest in the topic. I am in the process of modifying the pathnames on several apps and so far it is working. Can access them by path and by port number as needed. – Capstan Dec 14 '22 at 23:57