
æè¿ãNginx ã® location è¨å®ã®åªå é ä½ã誤解ãã¦ãã¦è»½ãã¤ã¾ã¥ãã¾ããããã®ã¸ãæã ããããããªããªãã®ã§ãèªåã®ããã«ã¾ã¨ãã¦ããã¾ãã
æ å ±æº
å ¬å¼ã®èª¬æã¯ãããã¥ã¢ã«ã® Module ngx_http_core_module ãã¼ã¸å ã® "location" ã®é ã«ããã¾ãã
location ã®ä¿®é£¾å(modifier)
location ãå®ç¾©ããéã«ä½¿ãã修飾åã¯ã以ä¸ã®5種é¡ã§ãã
| 修飾å | å¤å®æ¹æ³ |
|---|---|
= |
å®å ¨ä¸è´ |
| ãªã | åæ¹ä¸è´ |
^~ |
åæ¹ä¸è´ |
~* |
æ£è¦è¡¨ç¾ï¼case-insensitiveï¼ |
~ |
æ£è¦è¡¨ç¾ï¼case-sensitiveï¼ |
åæ¹ä¸è´ã2種é¡ããã¾ãããããã¯ã^~ ã¯çç¥å¯è½ãã¨ããæå³ã§ã¯ããã¾ããã
location ã®åªå é ä½
location ã®åªå
é ä½ãèããä¸ã§ãããããç¹ã¯ãæ¸ãããé ã«è©ä¾¡ãããè¨å®ã¨ãæé·ä¸è´ã§è©ä¾¡ãããè¨å®ãæ··å¨ãã¦ãããã¨ã§ããç¹ã«ãåªå
é ä½ãããããããã¦ããã®ã ^~ ã®åå¨ã§ãã
Nginx ã¯ããããªã¯ã¨ã¹ã URI ã«å¯¾ãã configuration ãæ±ºããããã«ã以ä¸ã®æé ã§ location ã鏿ãã¾ãã
- å®å ¨ä¸è´ã® location ãå¤å®ãããããããããã® location ãé¸ãã§çµäº
- åæ¹ä¸è´ã® location ãå¤å®ããæãé·ãæååãããããããã®ãè¨é²ããï¼æãé·ãæååãããããã location ã
^~修飾åã使ã£ã¦ããå ´åã«éãããã® location ãé¸ãã§çµäºï¼ - æ£è¦è¡¨ç¾ã® location ããè¨å®ãã¡ã¤ã«ã«æ¸ãããé ã§å¤å®ããæåã«ããããããã®ãè¦ã¤ãã£ãæç¹ã§çµäº
- 3 ã§ããããããã®ãè¦ã¤ãããªãã£ãå ´åã¯ã2 ã§è¦ã¤ãã£ã location ãé¸ãã§çµäº
ãã¤ã³ãã¯ã^~ ã«ãããããããã®å ´ã§å¤å®ãçµããããã§ã¯ãªããã¨ãããã¨ã§ãã^~ ãªãã®åæ¹ä¸è´ã®ã»ããæãé·ãæååï¼longest matching prefixï¼ã®å ´åã¯ãæé 3ã®æ£è¦è¡¨ç¾ã®å¤å®ã«é²ãã§ãã¾ããã¨ããç¹ã«æ³¨æãå¿
è¦ã§ãã
ããã ^~ 修飾åãå
¨ã使ããªããã°ã以ä¸ã®ããã«ç°¡åã«åªå
é ä½ã説æã§ãã¾ãã
- å®å ¨ä¸è´
~*ã¾ãã¯~修飾åã§å®ç¾©ãããæ£è¦è¡¨ç¾ã®ãã¡ãè¨å®ãã¡ã¤ã«ã«æ¸ãããé ã§æãæåã«ããããããã®- 修飾åãªãã§å®ç¾©ãããåæ¹ä¸è´ã®ãã¡ãæãé·ãæååãããããããã®
çµå± location ã¯ã©ãããé ã«æ¸ããããããï¼
å人çã«ã¯èªã¿ããããèãã¦ãè©ä¾¡ãããé ï¼èªåãè©ä¾¡ãã¦ã»ããã¨æå¾
ããé ï¼ã«æ¸ãã¦ããã®ãããã¨æãã¾ããã¾ãã^~ ããã®åæ¹ä¸è´ã¯æ··ä¹±ãæãããã^/ ããå§ã¾ãæ£è¦è¡¨ç¾ã¨ãã¦æ¸ãã¦ãã¾ã£ãã»ããããã®ã§ã¯ãªãããã¨æãã¾ãã
- å®å ¨ä¸è´ã® location ãæ¸ã
- åªå
ãããåæ¹ä¸è´ã® location ãã
^/ããå§ã¾ãæ£è¦è¡¨ç¾ã® location ã¨ãã¦æ¸ãï¼^~ã¯ä½¿ããªãï¼ - æ£è¦è¡¨ç¾ã® location ããåªå ããããã®ããé ã«æ¸ã
- 修飾åãªãã®åæ¹ä¸è´ã® location ããé·ããã®ããé ã«æ¸ã
Nginx ã®ããã¥ã¢ã«ã«æ¸ããã location ã®ä¾ã微修æ£ãã¦ãä¸ã®ã«ã¼ã«ã«å¾ã£ã¦æ¸ãç´ãããã®ããã¡ãã§ãã
# ã«ã¼ãã¸ã®å®å
¨ä¸è´
location = / {
[ configuration A ]
}
# /images/ 以ä¸ã®ç»å
location ~ ^/images/ {
[ configuration D ]
}
# /images/ 以å¤ã«ç½®ãããç»å
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
# /documents/ 以ä¸ã®ãã¼ã¸
location /documents/ {
[ configuration C ]
}
# ãã®ä»ãã¹ã¦ã®ãã¼ã¸
location / {
[ configuration B ]
}
ã¾ã¨ã
ããããã§ãããé©å½ãªé åºã§ location ãæ¸ãã¦ãã¾ã£ã¦ããªãã¨ãªããããªãã«åãããã«ããããã仿§ã«ãªã£ã¦ããã®ã§ã¯ãªãããã¨æãã¾ããã¨ã¯ããããã®ã¸ããæ£ç¢ºã«çè§£ãã¦ãããªãã¨ãä»ã®äººãæ¸ãã nginx.conf ããããã¨ãã«ã¤ã¾ã¥ããããã®ã§æ°ãã¤ãã¾ãããã
åèãã¼ã¸
location ã«é¢ãã説æãè²ã æ¢ãã¦ã¿ã¦ããã®ããã°ã®è¨è¼ãä¸çªããããããã¦åèã«ãªãã¾ããã
ãã¾ãï¼location ã®èª¬æã®æ¥æ¬èªè¨³
以ä¸ãããã¥ã¢ã«ã®è©²å½ç®æã®æ¥æ¬èªè¨³ã§ããå¤ããã¼ã¸ã§ã³ã® Nginx ã«ã¤ãã¦æ¸ãããè¨è¼ããç¹æ®ãªã±ã¼ã¹ï¼macOS ãªã©ï¼ã«ã¤ãã¦æ¸ãããã±ã¼ã¹ã¯å²æãã¾ããã
Sets configuration depending on a request URI.
location ã¯ãªã¯ã¨ã¹ã URI ã«å¿ã㦠configuration ãè¨å®ããã
The matching is performed against a normalized URI, after decoding the text encoded in the â%XXâ form, resolving references to relative path components â.â and â..â, and possible compression of two or more adjacent slashes into a single slash.
ãããã³ã°ã¯æ£è¦åããã URI ã«å¯¾ãã¦å®æ½ããããæ£è¦åã¨ã¯ã"%XX" å½¢å¼ã§ã¨ã³ã³ã¼ããããããã¹ãã®ãã³ã¼ããç¸å¯¾ãã¹ã表ãã³ã³ãã¼ãã³ã "." ããã³ ".." ã¸ã®åç §ã®è§£æ±ºãããã¦2ã¤ä»¥ä¸ã®é£æ¥ããã¹ã©ãã·ã¥ã®1åã®ã¹ã©ãã·ã¥ã¸ã®å§ç¸®ã§ããã
A location can either be defined by a prefix string, or by a regular expression. Regular expressions are specified with the preceding â~*â modifier (for case-insensitive matching), or the â~â modifier (for case-sensitive matching). To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.
location 㯠prefix string ã¾ãã¯æ£è¦è¡¨ç¾ã§å®ç¾©ã§ããã æ£è¦è¡¨ç¾ã¯ "~*" 修飾åï¼case-insensitive ãªãããã³ã°ï¼ãã¾ã㯠"~" 修飾åï¼case-sensitive ãªãããã³ã°ï¼ã§æå®ãããã ä¸ããããããããã location ãæ¢ãããã«ãnginx ã¯æåã« prex strings (prefix locations) ã使ã£ã¦å®ç¾©ããã location ããã§ãã¯ããã ãã®ãã§ãã¯ã®éã«ãæãé·ã prefix ã¨ããããã location ãé¸ã°ãã¦è¨é²ãããã ãã®æ¬¡ã«æ£è¦è¡¨ç¾ããè¨å®ãã¡ã¤ã«ã®ä¸ã§ç»å ´ããä½ç½®ã®é ã«ãã§ãã¯ãããã æ£è¦è¡¨ç¾ã®æ¤ç´¢ã¯ãæåã«ãããããæ®µéã§çµäºãã¦ããã® location ã«æ¸ããã configuration ã使ãããã ããããã®ãªã¯ã¨ã¹ã URL ã«ãããããæ£è¦è¡¨ç¾ãè¦ã¤ãããªãã£ãå ´åã¯ããã®åã®æ®µéã§è¨é²ããã prefix location ã® configuration ã使ãããã
location blocks can be nested, with some exceptions mentioned below.
location ãããã¯ã¯ãã¹ãå¯è½ã§ããããã ãã以ä¸ã«ç¤ºãããã¤ãã®ä¾å¤ãããã
Regular expressions can contain captures (0.7.40) that can later be used in other directives.
æ£è¦è¡¨ç¾ã¯ captures ãå«ããã¨ãã§ãã(0.7.40)ãããã¯å¾ã® directives ã§ä½¿ããã¨ãã§ããã
If the longest matching prefix location has the â^~â modifier then regular expressions are not checked.
ãããlongest matching prefix location ã "^~" 修飾åãæã£ã¦ããããæ£è¦è¡¨ç¾ã¯ãã§ãã¯ãããªãã
Also, using the â=â modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a â/â request happens frequently, defining âlocation = /â will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.
ã¾ãã"=" 修飾å ã使ãã¨ãURI 㨠location ã®å®å
¨ä¸è´ãå®ç¾©ã§ããã
ãããå®å
¨ä¸è´ãè¦ã¤ãã£ãããlocation ã®æ¤ç´¢ã¯çµäºãããä¾ãã°ããã "/" ã¸ã®ãªã¯ã¨ã¹ããé »ç¹ã«èµ·ãããªããlocation = / ãå®ç¾©ãããã¨ã§ãããã®ãªã¯ã¨ã¹ãã®å¦çãé«éåã§ãããæ¤ç´¢ã¯æåã®æ¯è¼ã§å®äºããããã§ããããã®ãã㪠location ãããã¹ãããã location ãæã¤ãã¨ã¯æããã«ã§ããªãã
Letâs illustrate the above by an example:
ä¸è¨ã®ãã¨ããä¾ã使ã£ã¦èª¬æãããã
location = / {
[ configuration A ]
}
location / {
[ configuration B ]
}
location /documents/ {
[ configuration C ]
}
location ^~ /images/ {
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
The â/â request will match configuration A, the â/index.htmlâ request will match configuration B, the â/documents/document.htmlâ request will match configuration C, the â/images/1.gifâ request will match configuration D, and the â/documents/1.jpgâ request will match configuration E.
/ ã¸ã®ãªã¯ã¨ã¹ã㯠configuration A ã«ãããããã"/index.html" ã¸ã®ãªã¯ã¨ã¹ã㯠B ã«ããããã"/documents/document.html" 㯠C ã«ãããããã"images/1.gif" 㯠D ã«ããããã"/documents/1.jpg" 㯠E ã«ãããããã
The â@â prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection. They cannot be nested, and cannot contain nested locations.
"@" åç½®è©ã¯ååä»ãã® location ãå®ç¾©ããããã®ãã㪠location ã¯æ£è¦è¡¨ç¾ã®å¦çã«ã¯ä½¿ãããªããããªã¯ã¨ã¹ãã®ãªãã¤ã¬ã¯ãã®ããã«ä½¿ãããã"@" ä»ãã® location ã¯ãã¹ãã§ããªããããã¹ãããã location ãå«ããã¨ãã§ããªãã
If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, or grpc_pass, then the special processing is performed. In response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended. If this is not desired, an exact match of the URI and location could be defined like this:
ãããlocation ãã/ ã§çµäºãã prefix string ã«ãã£ã¦å®ç¾©ãããå ´åããªã¯ã¨ã¹ã㯠proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, ã¾ã㯠grpc_pass ã®ãã¡ã®1ã¤ã«ãã£ã¦å¦çããã¦ããã®ç¹å¥ãªå¦çãå®è¡ãããã ãã®æååã«ä¸è´ãã URI ãæã¤ãªã¯ã¨ã¹ãããããæ«å°¾ã« / ãä»ããªã URI ã®å ´åã¯ããã® URI ã«å¯¾ããæä¹ çãªãªãã¤ã¬ã¯ããã/ ä»ãã® URI ã«å¯¾ãã¦è¿ãããã ããããã®åä½ãæã¾ãããªãå ´åãURI 㨠location ã®å®å ¨ä¸è´ã以ä¸ã®ããã«å®ç¾©ããã°ããã
location /user/ {
proxy_pass http://user.example.com;
}
location = /user {
proxy_pass http://login.example.com;
}