Score:0

Another nginx revproxy + rewrite + url mask

us flag

I know there's been a lot of posts about nginx + rewrite but I have a case where I cannot get it to work properly.

I have a multiple dockerized flask apps let's name it: vm1,vm2,vm3...vm6

I have a domain: manager.mydomain.com

What I'd like to achieve: when user navigates to https://manager.mydomain.com/vm6

gets redirected e.g to

https://manager.mydomain.com/vm6/login (and others endpoints /admin ....)

So generally speaking that is covered https://manager.mydomain.com/vm6/(.*) gets redirected to flask app. Unfortunately there are links that shall be rewritten as well.

My current trial:

http {

  upstream managervm4 {
    least_conn;
    server X.X.X.X:5004;
}

  upstream managervm6 {
    least_conn;
    server Y.Y.Y.Y:5006;
}

  server {

    server_name manager.mydomain.com;

    listen 443 ssl;

    ssl_certificate /etc/letsencrypt/live/manager.mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/manager.mydomain.com/privkey.pem;

    access_log /var/log/nginx/dtmanager.access.log;
    error_log /var/log/nginx/dtmanager.error.log;

    log_subrequest on;

    location /vm6 {
      rewrite ^/vm6$ https://manager.mydomain.com/vm6/ permanent;
    }


    location /vm6/ {
      #rewrite /vm6/(.*) /vm6/$1 break;
      sub_filter '"/' '"/vm6/';
      sub_filter_once off;
      proxy_pass http://managervm6/;
      #proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto https;
    }
}

When I navigate curl -i https://manager.mydomain.com/vm6/admin I get in response:

Server: nginx/1.13.9
Date: Fri, 02 Jul 2021 09:14:40 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: http://manager.mydomain.com/admin/

BUT if I navigate to curl -i https://manager.mydomain.com/vm6/login I get correct response, but then log-in does not work (I guess there's something wrong with redirections)

What am I doing wrong? Thanks!

morf avatar
us flag
I guess it is something with the response headers that should be modified as well.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.