NginxÐéÄâÖ÷»úÓÅ»¯ÉèÖã¬ÌáÉýÍøÕ¾ÐÔÄÜ
nginxÐéÄâÖ÷»úÓÅ»¯ÉèÖã¬ÌáÉýÍøÕ¾ÐÔÄÜ
ÔÚ¾ÙÐÐÍøÕ¾¿ª·¢ºÍ°²ÅŵÄÀú³ÌÖУ¬ÓÅ»¯ÉèÖÃÊÇÌá¸ßÍøÕ¾ÐÔÄܵÄÒªº¦»·½ÚÖ®Ò»¡£Nginx×÷Ϊһ¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬Í¨¹ýºÏÀíµÄÐéÄâÖ÷»úÉèÖÿÉÒÔ³ä·ÖÑéÕ¹ÆäÓÅÊÆ£¬Ê¹ÍøÕ¾Ô½·¢¸ßЧÎȹ̡£±¾ÆªÎÄÕ½«ÏÈÈÝһЩNginxÐéÄâÖ÷»úµÄÓÅ»¯ÉèÖÃÒªÁ죬²¢ÌṩÏìÓ¦µÄ´úÂëʾÀý¡£
ºÏÀíÉèÖüàÌý¶Ë¿Ú
Ê×ÏÈ£¬ÎÒÃÇÐèҪ˼Á¿Ñ¡ÔñÒ»¸öºÏÊʵļàÌý¶Ë¿Ú¡£NginxĬÈϼàÌý80¶Ë¿Ú£¬µ«ÈôÊÇÓÐÆäËûÈí¼þÕ¼ÓÃÁ˸ö˿ڣ¬»òÕßÐèҪͬʱÔËÐжà¸öÍøÕ¾£¬¾ÍÐèÒªÐÞ¸ÄĬÈÏÉèÖ᣿ÉÒÔͨ¹ý¸Ä±älistenÖ¸ÁîµÄÖµÀ´Ð޸ļàÌý¶Ë¿Ú¡£ÀýÈ磬½«¶Ë¿ÚÐÞ¸ÄΪ8080£º
server { listen 8080; ...... }
µÇ¼ºó¸´ÖÆ
¿ªÆôgzipѹËõ
ÆôÓÃgzipѹËõ¿ÉÒÔÓÐÓüõС´«ÊäµÄÊý¾ÝÁ¿£¬Ìá¸ßÍøÕ¾µÄÏìÓ¦ËÙÂÊ¡£¿ÉÒÔͨ¹ýÔÚÐéÄâÖ÷»úÉèÖÃÖÐÌí¼ÓÒÔÏ´úÂëÀ´ÆôÓÃgzipѹËõ£º
server { gzip on; gzip_min_length 1024; gzip_types text/plain text/css application/javascript application/json; ...... }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖÃÌåÏÖµ±ÏìÓ¦Êý¾ÝÁè¼Ý1024×Ö½Úʱ£¬²Å»áÆôÓÃgzipѹËõ¡£Í¬Ê±£¬Ö»ÓÐtext/plain¡¢text/css¡¢application/javascriptºÍapplication/jsonÕ⼸ÖÖÀàÐ͵ÄÎļþ²Å»á¾ÙÐÐѹËõ¡£
ÉèÖûº´æ
ºÏÀíÉèÖûº´æ¿ÉÒÔïÔ̶Ժó¶ËЧÀÍÆ÷µÄÇëÇó´ÎÊý£¬Ìá¸ßÍøÕ¾µÄÏìÓ¦ËÙÂÊ¡£¿ÉÒÔͨ¹ýÒÔÏ´úÂëÉèÖÃNginx»º´æ£º
server { # ÉèÖûº´æ·¾¶ºÍ¾Þϸ proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m; # »º´æÉèÖà proxy_cache my_cache; proxy_cache_valid 200 302 12h; proxy_cache_valid 404 1m; # ÊðÀíÉèÖà location / { proxy_pass http://backend_server; proxy_cache_use_stale off; proxy_cache_bypass $http_cache_control; add_header Cache-Control no-store; } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬proxy_cache_pathÖ¸ÁîÓÃÓÚÉèÖûº´æ·¾¶ºÍ¾Þϸ£¬proxy_cacheÖ¸ÁîÓÃÓÚÖ¸¶¨Ê¹ÓÃÄĸö»º´æ£¬proxy_cache_validÖ¸ÁîÓÃÓÚÉèÖûº´æµÄÓÐÓÃʱ¼ä¡£
ÆôÓÃHTTP/2
HTTP/2ÊÇÐÂÒ»´úµÄHTTPÐÒ飬ÓëHTTP/1Ïà±È¾ßÓиü¸ßµÄ²¢·¢ÐÔºÍÐÔÄÜ¡£¿ÉÒÔͨ¹ýÒÔÏ´úÂ뿪ÆôHTTP/2£º
server { listen 443 ssl http2; ...... }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Í¨¹ýÔÚlistenÖ¸ÁîÖÐÌí¼Óhttp2²ÎÊýÆôÓÃHTTP/2¡£
ÉèÖ÷´ÏòÊðÀí
Nginx×÷Ϊ·´ÏòÊðÀíЧÀÍÆ÷³£ÓÃÀ´¸ºÔØƽºâºÍÌṩ»º´æ¹¦Ð§£¬¿ÉÒÔͨ¹ýÒÔÏ´úÂëÉèÖ÷´ÏòÊðÀí£º
upstream backend { server backend_server1 weight=3; server backend_server2; ...... } server { location / { proxy_pass http://backend; ...... } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖÃÖУ¬upstreamÖ¸ÁîÓÃÓÚ½ç˵ºó¶ËЧÀÍÆ÷¼¯Èº£¬¿ÉÒÔƾ֤ÏÖʵÐèÒªÉèÖÃȨÖØ¡£locationÖ¸ÁîÖеÄproxy_passÓÃÓÚÖ¸¶¨·´ÏòÊðÀíЧÀÍÆ÷µÄµØµã¡£
×ܽ᣺
±¾ÎÄÏÈÈÝÁËһЩNginxÐéÄâÖ÷»úµÄÓÅ»¯ÉèÖÃÒªÁ죬°üÀ¨ºÏÀíÉèÖüàÌý¶Ë¿Ú¡¢¿ªÆôgzipѹËõ¡¢ÉèÖûº´æ¡¢ÆôÓÃHTTP/2ºÍÉèÖ÷´ÏòÊðÀí¡£ÕâЩÓÅ»¯ÉèÖÿÉÒÔÌáÉýÍøÕ¾µÄÐÔÄܺÍÎȹÌÐÔ£¬ÈÃÓû§»ñµÃ¸üºÃµÄÌåÑé¡£ËäÈ»£¬ÏêϸµÄÉèÖÃÒªÁ컹Ҫƾ֤ÏÖÕæÏàÐÎÀ´Ï¸»¯µ÷½â¡£
Ï£Íûͨ¹ý±¾ÎĵÄÏÈÈݺÍʾÀý´úÂ룬¶ÁÕßÄܹ»¸üºÃµØÃ÷È·NginxÐéÄâÖ÷»úÓÅ»¯ÉèÖõÄÏà¹Ø֪ʶ£¬²¢ÔÚÏÖʵӦÓÃÖмÓÒÔÔËÓá£
ÒÔÉϾÍÊÇNginxÐéÄâÖ÷»úÓÅ»¯ÉèÖã¬ÌáÉýÍøÕ¾ÐÔÄܵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡