NGINX Location Regex Matching [duplicate]

Может ли nginx location blocks соответствовать строке запроса URL?

Например, какой location block может соответствовать HTTP GET request

GET /git/sample-repository/info/refs?service=git-receive-pack HTTP/1.1
34
задан 31 October 2016 в 18:22
3 ответа

Can nginx location blocks match a URL query string?

Short answer: No.

Long answer: There is a workaround if we have only a handful of such location blocks.

Here's a sample workaround for 3 location blocks that need to match specific query strings:

server {
  #... common definitions such as server, root

  location / {
    error_page 418 = @queryone;
    error_page 419 = @querytwo;
    error_page 420 = @querythree;

    if ( $query_string = "service=git-receive-pack" ) { return 418; }
    if ( $args ~ "service=git-upload-pack" ) { return 419; }
    if ( $arg_somerandomfield = "somerandomvaluetomatch" ) { return 420; }

    # do the remaining stuff
    # ex: try_files $uri =404;

  }

  location @queryone {
    # do stuff when queryone matches
  }

  location @querytwo {
    # do stuff when querytwo matches
  }

  location @querythree {
    # do stuff when querythree matches
  }
}

You may use $query_string, $args or $arg_fieldname. All will do the job. You may know more about error_page in the official docs.

Warning: Please be sure not to use the standard HTTP codes.

43
ответ дан 4 January 2021 в 09:55

Я знаю, что этому вопросу больше года, но я провел последние несколько дней, разрушая свой мозг из-за аналогичной проблемы. Мне нужны были разные правила аутентификации и обработки для публичных и частных репозиториев, включая отправку и извлечение. Это то, что я наконец придумал, поэтому решил поделиться. Я знаю , если - сложная директива, но мне кажется, это отлично работает:

# pattern for all repos, public or private, followed by username and reponame
location ~ ^(?:\/(private))?\/([A-Za-z0-9]+)\/([A-Za-z0-9]+)\.git(\/.*)?$ {

    # if this is a pull request
    if ( $arg_service = "git-upload-pack" ) {

        # rewrite url with a prefix
        rewrite ^ /upload$uri;

    }

    # if this is a push request
    if ( $arg_service = "git-receive-pack" ) {

        # rewrite url with a prefix
        rewrite ^ /receive$uri;

    }

}

# for pulling public repos
location ~ ^\/upload(\/([A-Za-z0-9]+)\/([A-Za-z0-9]+)\.git(\/.*)?)$ {

    # auth_basic "git";
    # ^ if you want

    # ...
    # fastcgi_pass unix:/var/run/fcgiwrap.socket;
    # ...

}

# for pushing public repos
location ~ ^\/receive(\/([A-Za-z0-9]+)\/([A-Za-z0-9]+)\.git(\/.*)?)$ {

    # auth_basic "git";
    # ^ if you want

    # ...
    # fastcgi_pass unix:/var/run/fcgiwrap.socket;
    # ...

}

# for pulling private repos
location ~ ^\/upload\/private(\/([A-Za-z0-9]+)\/([A-Za-z0-9]+)\.git(\/.*)?)$ {

    # auth_basic "git";
    # ^ if you want

    # ...
    # fastcgi_pass unix:/var/run/fcgiwrap.socket;
    # ...

}

# for pushing private repos
location ~ ^\/receive\/private(\/([A-Za-z0-9]+)\/([A-Za-z0-9]+)\.git(\/.*)?)$ {

    # auth_basic "git";
    # ^ if you want

    # ...
    # fastcgi_pass unix:/var/run/fcgiwrap.socket;
    # ...

}
4
ответ дан 4 January 2021 в 09:55

Есть другой способ сделать это, если вы используете nginx в качестве прокси.

установите переменную в серверном блоке:

set $nocache="0";

Внутри блока местоположения добавьте if:

if ( $arg_<query string to match> = "<query string value>") { set $nocache "1"; }

И добавить две новые директивы прокси:

proxy_cache_bypass $nocache ;
proxy_no_cache $nocache ;

он всегда будет перенаправлять на вышестоящий сервер без кеша

-1
ответ дан 4 January 2021 в 09:55

Теги

Похожие вопросы