0

Can I use regexps or functions when creating variables? For example, I want to redirect some ports range (e.g., 7xxx -> 5xxx), but I would like do not create many servers - just one like this:

server {
    listen 127.0.0.1:7000-7200;
    listen 7000-7200;
    auth_basic "Restricted Content";
    auth_basic_user_file /etc/nginx/.htpasswd;

    location / {

        set $port 5${server_port:2};
        # or any substring function:
        set $port 5${substring($server_port,2)};
        proxy_pass http://0.0.0.0:$port/;
    }
}

i.e. $port is equal $server_port replacing the first character to symbol '5'. Can I do this?

Thank you for advance/

1 Answer 1

1

You can use map directive for this:

map $server_port $port {
    ~^.(.+)$ 5$1;
}

server {
    ...
    location / {
        proxy_pass http://0.0.0.0:$port/;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.