0

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

Mohsen
  • 103
  • 5
  • 1
    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. – Lex Li Dec 11 '22 at 01:38
  • Oh that's a good point, thanks for your advice – Mohsen Dec 11 '22 at 08:25

1 Answers1

1

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;
   
Benjamin B
  • 36
  • 2