0

I have a multi tenancy application that runs on port 3000, this app will dinamically load and generate contents for variuos websites ex. localhost:3000/webiste1.com/home , localhost:3000/webiste2.com/contacts and so on.

Of course i would like to access website1 from webiste1.com and not only from localhost:3000/webiste1.com.
Basically i need that webiste1.com is able to serve contents from localhost:3000/webiste1.com/
Since websites are not static doing some rewrites is not enough, so im trying to use proxy_pass:

First attempt :

server {
    server_name webiste1.com;
    location ~ ^/_next {
        proxy_pass http://127.0.0.1:3889;
        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  ~ ^(.*)$ {
        proxy_pass http://127.0.0.1:3889/$host$request_uri; <--adding a slash at the end cause infinite loop
        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;

    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/webiste1.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/webiste1.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = webiste1.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name webiste1.com;
    listen 80;
    return 404; # managed by Certbot


}

If i visit webiste1.com it redirects to webiste1.com/webiste1.com

Second attempt :

server {
    server_name webiste1.com;
    location ~ ^/_next {
        proxy_pass http://127.0.0.1:3889;
        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  ~ ^(.*)$ {
        proxy_pass http://127.0.0.1:3889/$host; <-- try to pass $host only
        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;

    }


}

If i visit website1.com it seems to works, content from localhost:3000/website1.com is served, however if i visit website1.com./some/path the proxy still serve content from localhost:3000/website1.com while it should serve content from localhost:3000/website1.com/some/path. How can i make that webistes1.com/any/path is proxied from localhost:3000/website1.com/any/path ?

Thanks

Nico
  • 101
  • 3

1 Answers1

0

The problem is that that proxy_pass destination is fixed in your second configuration.

This approach should work:

server {
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:3889/$host/;
        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;
    }
}

As described in proxy_pass documentation, here nginx replaces the part of normalized request URI specified in location with the URI specified in proxy_pass.

That is, in this case, the URL https://example.com/ is translated to http://127.0.0.1:3889/example.com/.

And https://example.com/123/ is translated to http://127.0.0.1:3889/example.com/123.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • This doesnt seems to works it will append the hostname to the url and then i get ERR_TOO_MANY_REDIRECTS error – Nico Feb 10 '21 at 20:44
  • Also @Tero Kilkanen i need to load content from `http://127.0.0.1:3889/example.com/123` when visiting `https://example.com/123/` – Nico Feb 11 '21 at 07:56
  • Are you sure your application is handling the situation correctly? Please share output of `curl -v https://example.com/` with this config. – Tero Kilkanen Feb 11 '21 at 14:49
  • i got `curl : Error from temote server: (308) Permanent Redirect.` can be that ssl redirect is a problem? i've updated original post with ssl redirect – Nico Feb 11 '21 at 15:30
  • Your application is making this redirect for some reason. – Tero Kilkanen Feb 11 '21 at 15:34