Score:0

Nginx reverse proxy base URI to change port

bz flag

Hi i want reverse proxy my Nginx base port used for example

http://example.com:443/ips/8443 -> http://example.com:8443/ips/8443
http://example.com:443/ips/23950 -> http://example.com:23950/ips/23950

If it supports this, it would be much better, but it is not mandatory

http://example.com:443/ips/23950/xyz -> http://example.com:23950/ips/23950/xyz

And this is my config

    location ~ /ips/([0-9]+)(/|)(\S+|)$ {
            proxy_pass http://127.0.0.1:$1/$3;
    }

I don't understand REGEX in nginx properly, but I can say, I've tried my best

Lex Li avatar
vn flag
Please first try to use `return 302 xxxx` to test out your regular expressions and the target URLs. That makes it rather easy to send mock requests via commands like `curl` and observe the responses. You can later switch to `proxy_pass` when you are 100% confident of the regular expression.
Mohsen avatar
bz flag
Oh that's a good point, thanks for your advice
Score:1
tc flag

To fix this issue, you will need to update the proxy_pass directive to include the full URL path in the proxied request. This can be done by using the $request_uri variable, which contains the full URL path of the incoming request.

Here is an example of how the updated proxy_pass directive might look:

proxy_pass http://127.0.0.1:$1$request_uri;

This will pass the request to the local port, along with the full URL path of the incoming request. So a request to /ips/23950/xyz will be proxied to http://127.0.0.1:23950/xyz, which should allow the request to be handled correctly by the local server.

It's also worth noting that the location block containing the proxy_pass directive should be the only block inside the location /ips block. This is because the location blocks are processed in order, and the return 404 statement in the outer location block will prevent any requests that match the inner location block from being processed. So you will need to move the return 404 statement inside the location block that matches the URLs with port numbers, and change it to return 403 or some other error code that indicates that the requested URL is not allowed.

Here is an example of how the updated configuration might look:

location ~ ^/ips/([0-9]+) {
    proxy_redirect off;
    proxy_pass http://127.0.0.1:$1$request_uri;
    proxy_http_version 1.1;
   
I sit in a Tesla and translated this thread with Ai:

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.