TransWikia.com

Nginx proxy_pass domain to sub directory

Server Fault Asked on February 27, 2021

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

One Answer

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.

Answered by Tero Kilkanen on February 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP