Nginx·´ÏòÊðÀíÉèÖÃÏê½â£¬ÓÅ»¯ÍøÕ¾ÐÔÄÜ
nginx·´ÏòÊðÀíÉèÖÃÏê½â£¬ÓÅ»¯ÍøÕ¾ÐÔÄÜ
ÏÈÈÝ£º
ÔÚWebÓ¦Óÿª·¢ÖУ¬·´ÏòÊðÀíÊÇÒ»ÖÖ³£¼ûµÄЧÀÍÆ÷°²ÅÅ·½·¨¡£Nginx×÷Ϊһ¸ö¸ßÐÔÄܵķ´ÏòÊðÀíЧÀÍÆ÷£¬¾ßÓÐÎÞаµÄÉèÖúÍÇ¿Ê¢µÄÐÔÄÜÓÅÊÆ¡£±¾ÎĽ«ÏêϸÏÈÈÝNginxµÄ·´ÏòÊðÀíÉèÖ㬲¢ÌṩһЩ´úÂëʾÀý£¬ÒÔ×ÊÖúÓÅ»¯ÍøÕ¾ÐÔÄÜ¡£
Ò»¡¢Nginx·´ÏòÊðÀíµÄ×÷ÓÃ
·´ÏòÊðÀíÊǽ«¿Í»§¶ËµÄÇëÇóת·¢¸øºó¶ËЧÀÍÆ÷µÄÒ»ÖÖ·½·¨¡£Í¨¹ýʹÓ÷´ÏòÊðÀí£¬¿ÉÒÔʵÏÖ¸ºÔØƽºâ¡¢»º´æ¡¢HTTPSÖ§³ÖµÈ¹¦Ð§£¬´Ó¶øÌá¸ßÍøÕ¾µÄÐÔÄܺÍÇå¾²ÐÔ¡£
¶þ¡¢·´ÏòÊðÀíÉèÖÃʾÀý
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄNginx·´ÏòÊðÀíÉèÖÃʾÀý£º
http { upstream backend { server backend1.example.com; server backend2.example.com; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬upstreamÓÃÓÚ½ç˵ºó¶ËЧÀÍÆ÷£¬serverÓÃÓÚ½ç˵¼àÌýµÄ¶Ë¿ÚºÍÓòÃû£¬locationÓÃÓÚ½ç˵ÇëÇóµÄת·¢¹æÔòºÍÉèÖÃÊðÀíÏà¹ØµÄHTTPÍ·¡£
Èý¡¢·´ÏòÊðÀíµÄ¸ºÔØƽºâ
¸ºÔØƽºâÊÇ·´ÏòÊðÀíµÄÒ»ÏîÖ÷Òª¹¦Ð§¡£ÔÚÉÏÊöʾÀýÖУ¬upstream backend²¿·Ö½ç˵ÁËÁ½¸öºó¶ËЧÀÍÆ÷£¬Nginx»áƾ֤һ¶¨µÄ¸ºÔØƽºâËã·¨½«ÇëÇóת·¢µ½²î±ðµÄºó¶ËЧÀÍÆ÷ÉÏ¡£³£¼ûµÄ¸ºÔØƽºâËã·¨ÓÐÂÖѯ£¨Ä¬ÈÏ£©¡¢¼ÓȨÂÖѯ¡¢IP¹þÏ£µÈ¡£
ËÄ¡¢·´ÏòÊðÀíµÄ»º´æ
»º´æÊÇÌá¸ßÍøÕ¾ÐÔÄܵÄÒªº¦¡£NginxÔÚ·´ÏòÊðÀíÖÐÌṩÁËÎÞаµÄ»º´æÉèÖá£ÏÂÃæÊÇÒ»¸ö¼òÆӵĻº´æÉèÖÃʾÀý£º
http { proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m; server { listen 80; server_name example.com; location / { proxy_cache my_cache; proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } }
µÇ¼ºó¸´ÖÆ
ͨ¹ýÉèÖÃproxy_cache_pathÖ¸¶¨»º´æ·¾¶£¬²¢Í¨¹ýproxy_cacheÖ¸ÁîÆôÓûº´æ¡£Æ¾Ö¤ÐèÇ󣬻¹¿ÉÒÔʹÓÃproxy_cache_validÖ¸ÁîÉèÖûº´æÓÐÓÃÆڵȡ£
Îå¡¢·´ÏòÊðÀíµÄHTTPSÖ§³Ö
ÔÚÏÖ´úWebÓ¦ÓÃÖУ¬HTTPSÒѾ³ÉΪ±êÅ䣬ΪÁËÌṩ¸üÇå¾²µÄ»á¼û£¬ÐèÒª½«HTTPÇëÇóת·¢µ½HTTPSЧÀÍÆ÷ÉÏ¡£ÏÂÃæÊÇÒ»¸ö·´ÏòÊðÀíHTTPSÉèÖÃʾÀý£º
http { server { listen 80; server_name example.com; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name example.com; ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/key.pem; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } }
µÇ¼ºó¸´ÖÆ
ͨ¹ýÉèÖÃlisten 80½«HTTPÇëÇóÖض¨Ïòµ½HTTPS£¬Í¨¹ýÉèÖÃssl_certificateºÍssl_certificate_keyÖ¸¶¨SSLÖ¤ÊéºÍ˽Կ¡£
Áù¡¢½áÓï
ͨ¹ýºÏÀíÉèÖÃNginxµÄ·´ÏòÊðÀí¹¦Ð§£¬¿ÉÒÔʵÏÖ¸ºÔØƽºâ¡¢»º´æ¡¢HTTPSÖ§³ÖµÈ¹¦Ð§£¬´Ó¶øÌá¸ßÍøÕ¾µÄÐÔÄܺÍÇå¾²ÐÔ¡£ÉÏÊö½öÊǼòÆÓµÄʾÀý£¬¶ÁÕß¿ÉÒÔƾ֤ÏÖʵÐèÇó¾ÙÐÐÉèÖá£Ï£Íû±¾ÎÄÄܶԸ÷ÈËÃ÷È·ºÍÓ¦ÓÃNginx·´ÏòÊðÀíÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇNginx·´ÏòÊðÀíÉèÖÃÏê½â£¬ÓÅ»¯ÍøÕ¾ÐÔÄܵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡