Ïêϸ½â¶ÁNginx·´ÏòÊðÀíЧÀÍÆ÷µÄHTTPÐÒéÖ§³ÖºÍÐÔÄÜÓÅ»¯
nginx·´ÏòÊðÀíЧÀÍÆ÷ÊÇÒ»ÖÖ¹¦Ð§Ç¿Ê¢µÄwebЧÀÍÆ÷£¬Ëü²»µ«¿ÉÒÔ´¦ÀíhttpÇëÇóºÍÏìÓ¦£¬»¹ÄÜÌṩhttpÐÒéÖ§³ÖºÍÐÔÄÜÓÅ»¯¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ïêϸ½â¶Ánginx·´ÏòÊðÀíЧÀÍÆ÷µÄhttpÐÒéÖ§³ÖºÍÐÔÄÜÓÅ»¯£¬²¢ÌṩһЩ´úÂëʾÀý¡£
Ò»¡¢HTTPÐÒéÖ§³Ö
ÇëÇó´¦Àí
Nginx·´ÏòÊðÀíЧÀÍÆ÷¿ÉÒÔÎüÊÕÀ´×Ô¿Í»§¶ËµÄHTTPÇëÇ󣬲¢½«Æäת·¢¸øºó¶ËЧÀÍÆ÷¡£¹ØÓÚÿ¸öÇëÇó£¬Nginx»á¾ÙÐÐÒÔϲÙ×÷£º
ÆÊÎöHTTPÇëÇóÐУ¬°üÀ¨ÇëÇóÒªÁì¡¢URIºÍHTTP°æ±¾¡£
ÆÊÎöHTTPÇëÇóÍ·²¿£¬°üÀ¨Host¡¢User-Agent¡¢AcceptµÈ¡£
¿ÉÒÔʹÓÃrewriteÖ¸Áî¶ÔÇëÇóURI¾ÙÐÐÖØд¡£
¿ÉÒÔÉèÖÃproxy_passÖ¸ÁÇëÇóת·¢¸øºó¶ËЧÀÍÆ÷£¬Ö§³ÖHTTP¡¢HTTPSºÍFastCGIµÈÐÒé¡£
ÏìÓ¦´¦Àí
Nginx·´ÏòÊðÀíЧÀÍÆ÷¿ÉÒÔÎüÊÕÀ´×Ôºó¶ËЧÀÍÆ÷µÄHTTPÏìÓ¦£¬²¢½«Æäת·¢¸ø¿Í»§¶Ë¡£¹ØÓÚÿ¸öÏìÓ¦£¬Nginx»á¾ÙÐÐÒÔϲÙ×÷£º
ÆÊÎöHTTPÏìÓ¦ÐУ¬°üÀ¨×´Ì¬ÂëºÍHTTP°æ±¾¡£
ÆÊÎöHTTPÏìӦͷ²¿£¬°üÀ¨Content-Type¡¢Content-LengthµÈ¡£
¿ÉÒÔʹÓÃproxy_hide_headerÖ¸ÁîÒþ²Ø²¿·ÖÏìӦͷ²¿¡£
¿ÉÒÔÉèÖÃproxy_bufferingÖ¸ÁîÔÚÊðÀíʱÆôÓûò½ûÓÃÏìÓ¦»º³å¡£
¸ºÔØƽºâ
Nginx·´ÏòÊðÀíЧÀÍÆ÷¿ÉÒÔͨ¹ý¸ºÔØƽºâËã·¨½«ÇëÇó·Ö·¢¸ø¶à¸öºó¶ËЧÀÍÆ÷£¬ÒÔÌá¸ßϵͳµÄÐÔÄܺͿɿ¿ÐÔ¡£³£ÓõĸºÔØƽºâËã·¨ÓÐÂÖѯºÍIP¹þÏ£µÈ¡£ÒÔÏÂÊÇÒ»¸ö¸ºÔØƽºâµÄʾÀýÉèÖãº
http { upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; } server { listen 80; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
¶þ¡¢ÐÔÄÜÓÅ»¯
ÅþÁ¬³ØÖÎÀí
Nginx·´ÏòÊðÀíЧÀÍÆ÷ʹÓÃÅþÁ¬³ØÖÎÀíÓëºó¶ËЧÀÍÆ÷µÄÅþÁ¬£¬ÒÔïÔÌÅþÁ¬½¨ÉèºÍ¹Ø±ÕµÄ¿ªÏú¡£¿ÉÒÔͨ¹ýÉèÖÃngx_http_upstreamÄ£¿éµÄ²ÎÊýÀ´ÓÅ»¯ÅþÁ¬³ØµÄ¾ÞϸºÍ³¬Ê±ÉèÖã¬ÀýÈ磺
http { upstream backend { server backend1.example.com max_conns=100; server backend2.example.com max_conns=100; server backend3.example.com max_conns=100; } keepalive_timeout 65; keepalive_requests 1000; }
µÇ¼ºó¸´ÖÆ
ÆôÓûº´æ
Nginx·´ÏòÊðÀíЧÀÍÆ÷¿ÉÒÔÆôÓûº´æ£¬½«¾³£»á¼ûµÄÏìÓ¦´æ´¢ÔÚÄÚ´æÖУ¬ÒÔÌá¸ßÏìÓ¦ËÙÂÊ¡£¿ÉÒÔͨ¹ýÉèÖÃproxy_cacheÖ¸ÁîÆôÓûº´æ£¬ÀýÈ磺
http { proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m; server { listen 80; location / { proxy_pass http://backend; proxy_cache my_cache; proxy_cache_valid 200 1d; } } }
µÇ¼ºó¸´ÖÆ
ѹËõ´«Êä
Nginx·´ÏòÊðÀíЧÀÍÆ÷¿ÉÒÔÆôÓÃÏìӦѹËõ£¬ïÔÌ´«ÊäÊý¾ÝÁ¿£¬Ìá¸ßÍøÂç´«ÊäЧÂÊ¡£¿ÉÒÔͨ¹ýÉèÖÃgzipÖ¸ÁîÆôÓÃÏìӦѹËõ£¬ÀýÈ磺
http { gzip on; gzip_types text/plain text/html text/css application/javascript; server { listen 80; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÊǹØÓÚNginx·´ÏòÊðÀíЧÀÍÆ÷µÄHTTPÐÒéÖ§³ÖºÍÐÔÄÜÓÅ»¯µÄÏêϸ½â¶Á£¬²¢ÌṩÁËһЩ´úÂëʾÀý¡£Í¨¹ýºÏÀíÉèÖÃNginx·´ÏòÊðÀíЧÀÍÆ÷£¬¿ÉÒÔÌá¸ßϵͳµÄÐÔÄܺͿɿ¿ÐÔ£¬ÎªÓû§Ìṩ¸üºÃµÄWebЧÀÍÌåÑé¡£
ÒÔÉϾÍÊÇÏêϸ½â¶ÁNginx·´ÏòÊðÀíЧÀÍÆ÷µÄHTTPÐÒéÖ§³ÖºÍÐÔÄÜÓÅ»¯µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡