简体   繁体   中英

How redirect partial uri in nginx to another app?

In my server with nginx with certbot i have two api running, in port 5000 and 3000.

All requests are for the app on port 5000, but when i request it in the 'pdf-generator' uri i want to send to the app on port 3000

Examples:

  • my-domain/abc -> localhost:5000
  • my-domain/pdf-generator -> localhost:3000
  • my-domain/pdf-generator/about -> localhost:3000/about
  • my-domain/pdf-generator/abc-> localhost:3000/abc

I searched in several places, I saw some similar situations but I didn't get success

tried:

    location = /pdf-generator/ {
        proxy_pass    http://localhost:3001;
    }
    location /pdf-generator {
        proxy_pass    http://localhost:3001$request_uri;
    }
    location = /pdf-generator/ {
        proxy_pass    http://localhost:3001/$request_uri;
    }
    location /pdf-generator {
        rewrite /pdf-generator/(.*) /$1  break;
        proxy_pass    http://localhost:3001;
    }
    location /pdf-generator {
        rewrite /pdf-generator/(.*) /$1  break;
        proxy_pass    http://localhost:3001/;
    }

Accessing the app's port 3001 directly through the server's IP in the uri "about" appears correctly what I need, but I would like to do it through my domain in the uri "pdf-generator"

  • myip:3001/about -> "ok"
  • my-domain/pdf-generator/about -> needs to show "ok"

At first i got confuse because I do not understand your pdf-generator should be sent to 3001 or 3000. by the way I use 3000 in the answer.

You can do it like this:

    location /pdf-generator {
        rewrite ^/(.*?)pdf-generator[^/] /$1? break;
        rewrite ^/(.*?)pdf-generator(?:/(.*))?$ /$1$2? break;
        proxy_pass    http://localhost:3000;
    }
    location / {
        proxy_pass    http://localhost:5000;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM