Can nginx locationbloklar bir URL sorgu dizesi maç?
Örneğin, hangi konum bloğu HTTP GETisteğiyle eşleşebilir?
GET /git/sample-repository/info/refs?service=git-receive-pack HTTP/1.1
?)?
Can nginx locationbloklar bir URL sorgu dizesi maç?
Örneğin, hangi konum bloğu HTTP GETisteğiyle eşleşebilir?
GET /git/sample-repository/info/refs?service=git-receive-pack HTTP/1.1
?)?
Yanıtlar:
Nginx konum blokları bir URL sorgu dizesiyle eşleşebilir mi?
Kısa cevap : Hayır.
Uzun cevap : Bu tür konum bloklarından sadece birkaçı varsa bir çözüm var.
Aşağıda, belirli sorgu dizeleriyle eşleşmesi gereken 3 konum bloğu için örnek bir geçici çözüm verilmiştir:
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
}
}
$ Query_string, $ args veya $ arg_fieldname kullanabilirsiniz. Herkes işi yapacak. Resmi dokümanlardaki error_page hakkında daha fazla bilgi edinebilirsiniz .
Uyarı: emin olun değil kullanmak standart HTTP kodlarını .
$args ~ "service=git-send-pack"yerine $args = "service=git-send-pack"? Bu formda birden çok sorgu parametresi bulunur.
ifve $arg_fieldnamefakat kullanımları rewriteyerine error_pageve location @name. Bu örnekte, içinde değiştirilecek parametre @nameiçin kullanma denemelerimin başarısız olduğunu unutmayın. rewrite
$args ~ve $arg_somerandomfield =.
mapDaha hızlı olan bu amaç için nginx özelliği de kullanılabilir .
queryparametre feedback/{auth_key}yerine böyle bir url yolu ile değiştirmektir /feedback?auth_key=abc. Bu şekilde kullanmama gerek yok if, kullanarak konum kalıbı tanımlayabilirim regexve hepsi bu kadar.
Bu sorunun bir yıldan fazla olduğunu biliyorum, ama son birkaç günü benzer bir sorun yüzünden beynimi yok ederek geçirdim. Genel ve özel depolar için itme ve çekme de dahil olmak üzere farklı kimlik doğrulama ve işleme kuralları istedim. Sonunda bu kadar geldim, bu yüzden paylaşacağımı düşündüm. ifZor bir direktif olduğunu biliyorum , ama bu benim için işe yarıyor gibi görünüyor:
# 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;
# ...
}