1

I have server A which takes https requests like:

https://example.com/CHANGEME/example/index.html

I have Server B which takes:

https://CHANGEME.example.com/example/index.html

The client should only talk to Server A.

The question is: How do I have to configure the NGINX proxy in server A to use the string between the first 2 "/" and put this as subdomain?

Its important to cut that part out of the path and use it as subdomain.

Sure, when Server A gets the response from server B the subdomain needs to be rewritten back as path for the client.

Anything I tried didn't work and this seems to be an uncommon problem.

Is there any pattern rewrite function I'm missing?

thanks in advance!

edit: I would like to change my question a bit as it is not working as expected.

What I now would like to achieve is to do the reverse proxy based on a cookie value.

For testing I set a cookie named url with value CHANGEME.

In NGINX config I tried:

location / { resolver 8.8.8.8; proxy_pass https://$cookie_url.example.com; } 

Which is generally working and loading the desired website. The problem is that the client also tries to connect to a mqtt broker wss://example.com/mqtt and this gives me an error. I think, due to the missing cookie read option? Any chance to get this working?

2 Answers 2

1

This should work with following approach:

server { location ~ ^/(?<subdomain>[^/]+)(?<path>.*)$ { proxy_pass http://$subdomain.example.com$path; } } 

The location block matches to paths, and captures the string between first two slashes to $subdomain variable. Then, the rest of the path is captured into $path variable. The captured values are then used as the proxy_pass destination.

1
  • Thanks, that is a really good start and kind of working. The problem now is that a websocket MQTT connection is not working. The client uses an absolute path like example.com/mqtt :( Commented Feb 18 at 6:41
0

I can answer the question myself. Got it working. The missing config part in location was:

proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.