×ðÁú¿­Ê±¹ÙÍøµÇ¼

Nginx – ×îСÉèÖÃ

Ç徲ЧÀÍÆ÷ÊÇÖ»ÔÊÐíËùÐèÊýÄ¿µÄЧÀÍÆ÷ ¡£ÀíÏëÇéÐÎÏ£¬ÎÒÃǽ«Í¨¹ýµ¥¶ÀÆôÓÃÆäËû¹¦Ð§À´»ùÓÚ×îСϵͳ¹¹½¨Ð§ÀÍÆ÷ ¡£¾ÙÐÐ×îÉÙµÄÉèÖÃÒ²ÓÐÖúÓÚµ÷ÊÔ ¡£ÈôÊǸùýʧÔÚ×îСϵͳÖв»¿ÉÓã¬Ôò»®·ÖÌí¼Ó¹¦Ð§£¬È»ºó¼ÌÐøËÑË÷¹ýʧ ¡£

ÕâÊÇÔËÐÐnginxËùÐèµÄ×îµÍÉèÖÃ:

# /etc/nginx/nginx.confevents {}         # event context have to be defined to consider config validhttp { server {    listen 80;    server_name  javatpoint.co  www.javatpoint.co  *.javatpoint.co;    return 200 "Hello";  }

µÇ¼ºó¸´ÖÆ

Root,LocationºÍtry_filesÖ¸Áî

Root Ö¸Áî

rootÖ¸ÁîÓÃÓÚÉèÖÃÇëÇóµÄ¸ùĿ¼£¬´Ó¶øÔÊÐínginx½«´«ÈëµÄÇëÇóÓ³Éäµ½ÎļþϵͳÉÏ ¡£

server {  listen 80;  server_name javatpoint.co;  root /var/www/javatpoint.co;}

µÇ¼ºó¸´ÖÆ

ËüÔÊÐínginxƾ֤ÇëÇó·µ»ØЧÀÍÆ÷ÄÚÈÝ:

javatpoint.co:80/index.html     # returns /var/www/learnfk.com/index.htmljavatpoint.co:80/foo/index.html # returns /var/www/learnfk.com/foo/index.html

µÇ¼ºó¸´ÖÆ

LocationÖ¸Áî

locationÖ¸ÁîÓÃÓÚƾ֤ÇëÇóµÄURI(ͳһ×ÊÔ´±êʶ·û)À´ÉèÖÃÉèÖà ¡£

Ó﷨Ϊ:

location [modifier] path

µÇ¼ºó¸´ÖÆ

ʾÀý:

location /foo {  # ...}

µÇ¼ºó¸´ÖÆ

ÈôÊÇδָ¶¨ÐÞÊηû£¬Ôò½«Â·¾¶ÊÓΪǰ׺£¬Ö®ºó¿ÉÒÔ×·ËæÈκÎÄÚÈÝ ¡£ÉÏÃæµÄʾÀý½«Æ¥Åä:

/foo/fooo/foo123/foo/bar/index.html...

µÇ¼ºó¸´ÖÆ

ÎÒÃÇ»¹¿ÉÒÔÔÚ¸ø¶¨µÄÉÏÏÂÎÄÖÐʹÓöà¸ölocationÖ¸Áî:

server {  listen 80;  server_name javatpoint.co;  root /var/www/javatpoint.co;  location/{    return 200 "root";  }  location /foo {    return 200 "foo";  }}javatpoint.co:80  /      # => "root"javatpoint.co:80   /foo    # => "foo"javatpoint.co:80   /foo123 # => "foo"javatpoint.co:80   /bar    # => "root"

µÇ¼ºó¸´ÖÆ

Nginx»¹ÌṩÁËһЩ¿ÉÒÔÓë location Ö¸ÁîÍŽáʹÓõÄÐÞÊηû ¡£

ËÑË÷ÃñÖÚºÅLinuxÖÐÎÄÉçÇøºǫ́»Ø¸´¡°Ë½·¿²Ë¡±£¬»ñÈ¡Ò»·Ý¾ªÏ²Àñ°ü ¡£

ÐÞÊηûÒÑ·ÖÅÉÓÅÏȼ¶:

=           - Exact match^~          - Preferential match~ && ~*     - Regex matchno modifier - Prefix match

µÇ¼ºó¸´ÖÆ

Ê×ÏÈ£¬nginx½«¼ì²éËùÓÐ׼ȷƥÅäÏî ¡£ÈôÊDz»±£´æ£¬Ëü½«Ñ°ÕÒÓÅÏÈÑ¡Ïî ¡£ÈôÊÇ´ËÆ¥ÅäҲʧ°Ü£¬Ôò½«°´Æä·ºÆð˳Ðò²âÊÔÕýÔò±í´ïʽƥÅä ¡£ÈôÊÇÆäËûËùÓвÙ×÷¾ùʧ°Ü£¬Ôò½«Ê¹ÓÃ×îºóÒ»¸öǰ׺ƥÅä ¡£

location /match {  return 200 'Prefix match: will match everything that starting with /match';}location ~* /match[0-9] {  return 200 'Case insensitive regex match';}location ~ /MATCH[0-9] {  return 200 'Case sensitive regex match';}location ^~ /match0 {  return 200 'Preferential match';}location = /match {  return 200 'Exact match';}/match     # => 'Exact match'/match0    # => 'Preferential match'/match1    # => 'Case insensitive regex match'/MATCH1    # => 'Case sensitive regex match'/match-abc # => 'Prefix match: matches everything that starting with /match'

µÇ¼ºó¸´ÖÆ

try_filesÖ¸Áî

¸ÃÖ¸ÁîʵÑé²î±ðµÄ·¾¶£¬²¢·µ»ØÕÒµ½µÄÈκη¾¶ ¡£

try_files $uri index.html =404;

µÇ¼ºó¸´ÖÆ

Òò´Ë£¬/foo.html½«ÊµÑé°´ÒÔÏÂ˳Ðò·µ»ØÎļþ:

$uri(/foo.html);index.html

µÇ¼ºó¸´ÖÆ

ÈôÊÇδÕÒµ½:404

ÈôÊÇÎÒÃÇÔÚЧÀÍÆ÷ÉÏÏÂÎÄÖнç˵try_files£¬È»ºó½ç˵²éÕÒËùÓÐÇëÇóµÄλÖã¬Ôò²»»áÖ´ÐÐtry_files ¡£±¬·¢ÕâÖÖÇéÐÎÊÇÓÉÓÚЧÀÍÆ÷ÉÏÏÂÎÄÖеÄtry_files½ç˵ÁËÆäαλÖ㬸ÃαλÖÃÊÇ¿ÉÄܵÄ×îµÍÌض¨Î»Öà ¡£Òò´Ë£¬½ç˵location/ »á±ÈÎÒÃǵÄαλÖøüÏêϸ ¡£

server {  try_files $uri /index.html =404;  location/{  }}

µÇ¼ºó¸´ÖÆ

Òò´Ë£¬ÎÒÃÇÓ¦¸Ã×èÖ¹ÔÚЧÀÍÆ÷ÉÏÏÂÎÄÖÐʹÓÃtry_files:

server {  location/{    try_files $uri /index.html =404;  }}

µÇ¼ºó¸´ÖÆ

ÒÔÉϾÍÊÇNginx – ×îСÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ