nginxÖÐlocationÓ÷¨
nginx µÄ location Ö¸ÁîÓÃÓÚ½ç˵´¦Öóͷ£ÇëÇóµÄ¹æÔò¼¯£¬Í¨¹ýÆ¥Åä uri¡¢http ÒªÁìµÈÌõ¼þ£¬Ö¸¶¨ÇëÇó´¦Öóͷ£·½·¨¡£Óï·¨°üÀ¨£º×¼È·Æ¥Åä¡¢ÕýÔò±í´ïʽƥÅ䡢ǰ׺ƥÅäµÈ¡£location Ö¸Áî¿ÉÒÔǶÌ×£¬ÓÅÏȼ¶ÓÉÆ¥Åä˳Ðò¾öÒ飬ºóÐøÇëÇó¿ÉÄÜÊÜÆäÓ°Ïì¡£
nginx ÖÐ location Ó÷¨
location Ö¸ÁîÓÃÓÚ½ç˵ nginx ÖÐÓÃÓÚ´¦Öóͷ£ÇëÇóµÄ¹æÔò¼¯¡£ËüÓÃÓÚÆ¥ÅäÇëÇóµÄijЩ·½Ã棬ÀýÈç URI¡¢HTTP ÒªÁì»òÆäËû±êÍ·£¬²¢Ö¸¶¨Ó¦ÈçÄÇÀïÖÃÇëÇó¡£
Ó÷¨
location [= | ~ | ^~] [uri] { # Ö¸Áî¿é }
µÇ¼ºó¸´ÖÆ
=£º×¼È·Æ¥Åä URI
~£ºÕýÔò±í´ïʽƥÅä URI
^~£ºÇ°×ºÆ¥Åä URI
Óï·¨
location Ö¸Áî¿ÉÒÔ°üÀ¨ÒÔÏÂÓï·¨£º
alias£ºÖ¸¶¨ÎļþϵͳÖеÄÎļþ»òĿ¼µÄÓÖÃû
auth_basic£ºÆôÓûù×Ô¼º·ÝÑéÖ¤
auth_basic_user_file£ºÖ¸¶¨°üÀ¨Óû§Æ¾Ö¤µÄÎļþ
charset£ºÉèÖÃ×Ö·û¼¯
deny£º¾Ü¾øÆ¥ÅäÇëÇó
error_page£ºÖ¸¶¨¹ýʧҳÃæ
if£º»ùÓÚÌõ¼þÖ´ÐÐÖ¸Áî¿é
include£º´ÓÍⲿÎļþ°üÀ¨Ö¸Áî
limit_except£ºÆ¾Ö¤ÇëÇó URI »ò IP µØµãɨ³ýÇëÇóÏÞÖÆ
limit_req£ºÏÞÖÆÿÃë´¦Öóͷ£µÄÇëÇóÊý
location£ºÇ¶Ì× location Ö¸Áî
proxy_pass£ºÊðÀíÇëÇ󵽺ó¶ËЧÀÍÆ÷
rewrite£ºÖØд URI
return£ºÏÔʽ·µ»ØÇëÇó
ʾÀý
Æ¥Åä/hello URI µÄÇëÇó²¢·µ»Ø¾²Ì¬Îļþ£º
location = /hello { alias /var/www/html/hello.html; }
µÇ¼ºó¸´ÖÆ
Æ¥ÅäÒÔ/api/¿ªÍ·µÄ URI µÄÇëÇó²¢½«ÆäÊðÀíµ½ºó¶ËЧÀÍÆ÷£º
location ^~ /api/ { proxy_pass http://backend_server; }
µÇ¼ºó¸´ÖÆ
ÓÅÏȼ¶
location Ö¸ÁîµÄÓÅÏȼ¶ÓÉÆ¥Åä˳Ðò¾öÒé¡£µÚÒ»¸öÆ¥ÅäµÄ location Ö¸Á´¦Öóͷ£ÇëÇó¡£
×¢ÖØÊÂÏî
location Ö¸Áî¿ÉÒÔǶÌס£
¿ÉÒÔʹÓÃÕýÔò±í´ïʽ½ç˵ÖØ´óµÄÆ¥Åä¹æÔò¡£
location Ö¸ÁîÖеÄÖ¸Áî¿ÉÒÔÓ°ÏìºóÐøÇëÇó¡£ÀýÈ磬return Ö¸ÁÁ¬Ã¦×èÖ¹ÇëÇó´¦Öóͷ£¡£
ÒÔÉϾÍÊÇnginxÖÐlocationÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡