Node JS - Nginx - proxy_pass to a subdirectory - Koa
Asked Answered
R

1

8

I'm running a Koa app on port 5000, and i'd like Ngnix to serve the app in a sub-directory - e.g: http://example.com/myNodeApp

Here's what I've currently got in /etc/nginx/sites-enabled/default

        location ^~ /myNodeApp/ {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass    http://localhost:5000/;
        }

This kind of works... apart from the fact that any redirect e.g this.redirect('/') in my Koa app goes to the the nginx web root /

Also, it doesn't render anything from my Koa apps' public directory e.g. stylesheets, javascript and images.

What am I doing wrong? Thanks.

Reisinger answered 11/10, 2016 at 18:22 Comment(7)
Why are you using regexp match of location? Switch to simple string match location /myNodeApp/ {Fulk
Thanks @Terra. I read a similar SO question where they did this and thought I would try it too. Changing to location /myNodeApp/ { ... } (without the regular expression as you suggested), gives the same result.Reisinger
Can you check and show full Location header? With domain part. Because Location header rewriting work only if your node application redirect to localhost:5000. But if node application redirects to localhost - custom rewrite required. So, need to know how location header looks like.Fulk
Not quite sure what you're asking Terra (sorry). I have a stock nginx config, and have bolted the code above inside the server { ... } block. This is all a bit new to me!Reisinger
curl -I http://localhost:5000 HTTP/1.1 200 OK Content-Type: text/html; charset=utf-8 Content-Length: 2730 Date: Tue, 11 Oct 2016 20:49:53 GMT Connection: keep-aliveReisinger
Can you curl node's URL which execute this.redirect('/')Fulk
Oh, let me explain that you need to find. Read this nginx.org/en/docs/http/…, especially default behavior. Node application doing redirect with HTTP header Location: http://xxxxx/xxx. Depend on domain part - nginx can rewrite it or not. If this domain part localhost:5000 - nginx can rewrite this redirect as default behavior. If node application using another domain part (http://localhost/xxxx for example) - you need to add custom proxy_redirect directive.Fulk
M
14

I have recently come across the same problem and here's what I did to fix it.

In Server Config:

I had to add

rewrite ^/myNodeApp/(.*)$ /$1 break;

to the NGINX config, in the

location /myNodeApp/ {...}

block, under what you already have in your example.

In client side:

I added

<base href='/myNodeApp/'>

to the <head> of my html files (or pug layout file in my case). This prefixes any links with your subdirectory.

Note that you will need to remove any leading /'s from your existing links. Eg

<link rel="stylesheet" href="layout.css">

instead of

<link rel="stylesheet" href="/layout.css">

That one caught me out for a while.

Bonus:

If you're using Socket.IO, like I am, you'll need to make a few more changes to stop some errors appearing in your console. You need to pass it a path option and specify your subdirectory.

In your html files

var socket = io.connect("/", {path: "/myNodeApp/socket.io"})
Megathere answered 21/10, 2016 at 11:9 Comment(1)
Perfect! Thank you - this was exactly what I was looking for. +1Reisinger

© 2022 - 2024 — McMap. All rights reserved.