5

In my Nginx config I have redirect rules like this:

map $uri $target {
    '/test123' 'https://www.somedomain.com/test456';
}
map $uri $target_code {
    '/test123' 301;
}
server {
    listen 80 default;

    if ($target_code = 301) {
        return 301 $target;
    }
    if ($target_code = 302) {
        return 302 $target;
    }
}

and it works well. But regarding of If is evil

I want make statement like this:

server {
    listen 80 default;

    return $target_code $target;
}

But during the restart, Nginx returns error nginx: [emerg] invalid return code "$target_code" in /etc/nginx/nginx.conf

Is any possibility to use variable in that way? Or maybe is other way to make it without any if statements?

quentino
  • 1,101
  • 1
  • 10
  • 25
  • 2
    Your linked document only relates to the use of `if` within a `location` context. And if you use a `map` you need a conditional statement to implement the `return`, and in vanilla Nginx that requires an `if` statement. – Richard Smith Dec 02 '18 at 10:47
  • 1
    As far as I know only redirect URL or response body in `return` directive can contain variables. I think there is no way to get rid of `if` statements with this configuration. – Ivan Shatsky Dec 02 '18 at 12:09

1 Answers1

0

From what I can see this that is not possible. What follows is the relevant part of the code:

    ret->status = ngx_atoi(p, value[1].len);

    if (ret->status == (uintptr_t) NGX_ERROR) {

        if (cf->args->nelts == 2
            && (ngx_strncmp(p, "http://", sizeof("http://") - 1) == 0
                || ngx_strncmp(p, "https://", sizeof("https://") - 1) == 0
                || ngx_strncmp(p, "$scheme", sizeof("$scheme") - 1) == 0))
        {
            ret->status = NGX_HTTP_MOVED_TEMPORARILY;
            v = &value[1];

        } else {
            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                               "invalid return code \"%V\"", &value[1]);
            return NGX_CONF_ERROR;
        }

First it tries to convert the first parameter to an integer. If that fails it checks if that is an URL. If not, it bails out with the error.

x-yuri
  • 16,722
  • 15
  • 114
  • 161