6

I'm trying to run some nodejs app on a server (Ubuntu 14.04), using Nginx and i'm almost done. Here's my server configuration (/etc/nginx/sites-available/default):

server {
    listen 80;
    server_name my_domain.com;

    location /test1 {
        proxy_pass http://127.0.0.1:5000/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location /test2 {
        proxy_pass http://127.0.0.1:5001/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

I've got several apps running, and they all works well, i can acces them with http://my_domain.com/test1, http://my_domain.com/test2, etc...

The problem is that inside one of this apps i've got several absolute paths:
e.g. <a href="/">Home</a>
or (inside express)
res.redirect('/');

This redirects don't go to http://my_domain.com/test1 but they go to http://my_domain.com/
Is there a way, through nginx configurations, to tell the app that the root location is actually http://my_domain.com/test1?

I'm really new to nginx and to virtual hosts in general, i'm trying to learn... Any help would be appreciated.
EDIT:
The result of curl -I http://127.0.0.1:5000 is:

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 1376
ETag: W/"560-GGm/YYltkhKxiOVNZ99jqQ"
set-cookie: connect.sid=s%3AkZYCqLKvbhHhK3W8ECBN8G91s41paws4.ekLWefrd3NdQatT3VzFNozfnFs65YBEW9k9MNTdbQT0; Path=/; HttpOnly
Date: Sat, 15 Aug 2015 13:13:20 GMT
Connection: keep-alive  

As you can see i don't get a Location Header...
By the way, i managed to solve the problem using subdomains, that seem to work as i expected... Anyway an answer would be appreciated, since i might need it in the future.

  • 2
    Hi Alexey, i've been reading answers in the last 3 hours or so... I couldn't find the one that matches my needs,, and what i found didn't work in my case. Maybe i'm not searching for the right things.. it's not so easy when you're new to something.. – marco.marinangeli Aug 14 '15 at 21:37
  • 1
    possible duplicate of nginx reverse proxy folder – womble Aug 15 '15 at 8:12
2

The only way I found to make this work is to use the HttpSubModule and adding sub_filter directives. Given your examples it could look like this:

sub_filter 'href="/' 'href="/test1/';
sub_filter "redirect('/')" "redirect('/test1/')";

Obviously the more specific your matching is the more options you'll have to add. If you go less specific, like just match "/ or '/ then you need less rules but run into the danger of substituting the wrong thing.

You probably also need to add:

sub_filter_types *;

so it doesn't just match text/html (which is the default) but also javascript and css files. Obviously * is the lazy approach which might break things and using specific mimetypes should be preferred.

Ultimately the correct way is to fix the web application. Most web frameworks support something like a base url/root url/url prefix (there doesn't seem to be standard name for this) which you can set to avoid exactly this problem.

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.