ÏêϸÐðÊöNginxЧÀÍÆ÷ÔÚ´óÐÍÍøÕ¾¼Ü¹¹ÖеÄÓ¦Óó¡¾°
nginxЧÀÍÆ÷ÔÚ´óÐÍÍøÕ¾¼Ü¹¹ÖеÄÓ¦Óó¡¾°
СÐò£º
ÔÚÄ¿½ñ»¥ÁªÍøʱ´ú£¬´óÐÍÍøÕ¾µÄ»á¼ûÁ¿Ò»Ö±ÔöÌí£¬¶ÔЧÀÍÆ÷µÄÐÔÄܺÍÎȹÌÐÔÌá³öÁ˸ü¸ßµÄÒªÇó¡£NginxЧÀÍÆ÷×÷Ϊһ¿î¸ßÐÔÄÜ¡¢¸ß¿É¿¿ÐԵĿªÔ´Ð§ÀÍÆ÷£¬±»Ô½À´Ô½¶àµÄ´óÐÍÍøÕ¾½ÓÄÉ¡£±¾ÎĽ«ÏÈÈÝNginxÔÚ´óÐÍÍøÕ¾¼Ü¹¹ÖеÄÓ¦Óó¡¾°£¬²¢ÌṩÏìÓ¦µÄ´úÂëʾÀý¡£
Ò»¡¢·´ÏòÊðÀí
Ïà¹Ø¿´·¨£º·´ÏòÊðÀíÊÇÖ¸¿Í»§¶Ë»á¼ûЧÀÍÆ÷µÄÇëÇó²»Ö±½Ó·¢Ë͸øÄ¿µÄЧÀÍÆ÷£¬¶øÊÇ·¢Ë͸øÖÐÐĵķ´ÏòÊðÀíЧÀÍÆ÷£¬ÓÉ·´ÏòÊðÀíЧÀÍÆ÷ÈÏÕæת·¢ÇëÇóµ½Ä¿µÄЧÀÍÆ÷¡£ÕâÖּܹ¹¿ÉÒÔÌá¸ßÍøÕ¾µÄÐÔÄܺÍÇå¾²ÐÔ¡£
´úÂëʾÀý£º
location / {
proxy_pass http://backend_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
¶þ¡¢¸ºÔØƽºâ
Ïà¹Ø¿´·¨£º¸ºÔØƽºâÊǽ«ÇëÇó·Ö·¢µ½¶à¸öЧÀÍÆ÷ÉÏ£¬´Ó¶øÊèɢЧÀÍÆ÷µÄ¸ºÔØ£¬Ìá¸ßÍøÕ¾µÄ¿ÉÓÃÐÔºÍÐÔÄÜ¡£
´úÂëʾÀý£º
http {
upstream backend_servers {
server backend1.example.com; server backend2.example.com; server backend3.example.com;
µÇ¼ºó¸´ÖÆ
}
server {
location / { proxy_pass http://backend_servers; }
µÇ¼ºó¸´ÖÆ
}
}
Èý¡¢¾²Ì¬Îļþ»º´æ
Ïà¹Ø¿´·¨£º¾²Ì¬Îļþ»º´æÊǽ«³£Óþ²Ì¬ÎļþÈçͼƬ¡¢CSS¡¢JSµÈ»º±£´æNginxЧÀÍÆ÷ÉÏ£¬ïÔ̶Ժó¶ËЧÀÍÆ÷µÄ»á¼ûѹÁ¦£¬Ìá¸ßÍøÕ¾µÄÐÔÄܺÍÏìÓ¦ËÙÂÊ¡£
´úÂëʾÀý£º
http {
server {
location /static/ { root /var/www/html; expires 30d; }
µÇ¼ºó¸´ÖÆ
}
}
ËÄ¡¢·´ÏòÊðÀí»º´æ
Ïà¹Ø¿´·¨£º·´ÏòÊðÀí»º´æÊǽ«ºó¶ËЧÀÍÆ÷·µ»ØµÄ¶¯Ì¬Êý¾Ý»º±£´æNginxЧÀÍÆ÷ÉÏ£¬µ±Ï´ÎÇëÇóÏàͬµÄÊý¾Ýʱ£¬Ö±½Ó·µ»Ø»º´æµÄÊý¾Ý£¬ïÔ̶Ժó¶ËЧÀÍÆ÷µÄѹÁ¦¡£
´úÂëʾÀý£º
http {
server {
location / { proxy_pass http://backend_server; proxy_cache cache_zone; proxy_cache_valid 200 1d; }
µÇ¼ºó¸´ÖÆ
}
}
Îå¡¢SSL/TLSÖ§³Ö
Ïà¹Ø¿´·¨£ºSSL/TLSÊÇÒ»ÖÖ¼ÓÃÜͨѶÐÒ飬ÓÃÓÚ±£»¤ÍøÂçͨѶµÄÇå¾²ÐÔ¡£NginxЧÀÍÆ÷Ö§³ÖSSL/TLSÐÒ飬¿ÉÒÔΪÍøÕ¾ÌṩÇå¾²¼ÓÃܵĴ«ÊäͨµÀ¡£
´úÂëʾÀý£º
http {
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_server; }
µÇ¼ºó¸´ÖÆ
}
}
½áÂÛ£º
NginxЧÀÍÆ÷ÔÚ´óÐÍÍøÕ¾¼Ü¹¹ÖÐÓÐ×ÅÆÕ±éµÄÓ¦Óó¡¾°¡£Í¨¹ý·´ÏòÊðÀí¡¢¸ºÔØƽºâ¡¢¾²Ì¬Îļþ»º´æ¡¢·´ÏòÊðÀí»º´æºÍSSL/TLSÖ§³Ö£¬¿ÉÒÔÌá¸ßÍøÕ¾µÄÐÔÄÜ¡¢¿ÉÓÃÐÔºÍÇå¾²ÐÔ¡£Í¨¹ýÒÔÉϵĴúÂëʾÀý£¬¿ÉÒÔ×ÊÖú¿ª·¢Õ߸üºÃµØÃ÷È·ºÍÓ¦ÓÃNginxЧÀÍÆ÷£¬Îª¹¹½¨¸ßÐÔÄܵĴóÐÍÍøÕ¾Ìṩ֧³Ö¡£
£¨×¢£ºÒÔÉÏ´úÂëʾÀý½ö¹©²Î¿¼£¬ÏÖʵӦÓÃÖÐÐèҪƾ֤ÏêϸÇéÐξÙÐе÷½â¡££©
ÒÔÉϾÍÊÇÏêϸÐðÊöNginxЧÀÍÆ÷ÔÚ´óÐÍÍøÕ¾¼Ü¹¹ÖеÄÓ¦Óó¡¾°µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡