1

I have set up an nginx reverse proxy to node essentially using this set up reproduced below:

upstream nodejs {
    server localhost:3000;
}

server {
    listen 8080;
    server_name localhost;
    root ~/workspace/test/app;

    location / {
        try_files $uri $uri/ @nodejs;
    }

    location @nodejs {
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_pass http://nodejs;
        proxy_set_header Host $host ; 
        proxy_set_header X-Real-IP $remote_addr; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

Now all my AJAX POST requests travel just fine to the node with this set up, but I am polling for files afterward that I cannot find when I make a clientside AJAX GET request to the node server (via this nginx proxy).

For example, for a clientside javascript request like .get('Users/myfile.txt') the browser will look for the file on localhost:8080 but won't find it because it's actually written to localhost:3000

http://localhost:8080/Users/myfile.txt // what the browser searches for
http://localhost:3000/Users/myfile.txt // where the file really is

How do I set up the proxy to navigate through to this file?

Community
  • 1
  • 1
o1sound
  • 480
  • 7
  • 22
  • It looks like Node.js is routing it to 8080. Can you show the relevant config? – Keenan Lawrence Nov 18 '16 at 22:54
  • I'm using node and express. The only config file I can think of is for nginx, which I posted. One scenario is that I have `app.post('/data')` to handle the data in the node server code but no `app.get()`. The file is instead written by a separate engine. Do I need to set up `app.get()` in node explicitly for the proxy to be recognized? Or is the culprit in nginx.conf? – o1sound Nov 18 '16 at 23:58
  • 1
    Sorry, I read wrong. It's routing to 8080 because that's what your NGINX config is listening on. In any event, NGINX will proxy this to port 3000 if it fails at the `try_files` directive, so it should still find the file. Are you receiving 404 in the log files? – Keenan Lawrence Nov 19 '16 at 10:26
  • Yes 404 in the logfiles. I'm polling for HEAD and I get `.... "HEAD /Users/myfile.txt HTTP/1.1" 404 0 "http://localhost:8080/" ...` – o1sound Nov 19 '16 at 15:07

1 Answers1

1

Okay, I got it working. The set up in the nginx.conf file posted above is just fine. This problem was never an nginx problem. The problem was in my index.js file over on the node server.

When I got nginx to serve all the static files, I commented out the following line from index.js

app.use(express.static('Users')); // please don't comment this out thank you

It took me a while to troubleshoot my way back to this as I was pretty wrapped up in understanding nginx. My thinking at the time was that if nginx is serving static files why would I need express to serve them? Without this line however, express won't serve any files at all obviously.

Now with express serving static files properly, nginx handles all static files from the web app and node handles all the files from the backend and all is good.

Thanks to Keenan Lawrence for the guidance and AR7 for the config!

Community
  • 1
  • 1
o1sound
  • 480
  • 7
  • 22