ÔõÑùʵÏÖNginx·´ÏòÊðÀíÉèÖÃ
ÔõÑùʵÏÖNginx·´ÏòÊðÀíÉèÖã¬ÐèÒªÏêϸ´úÂëʾÀý
NginxÊÇÒ»¿î¸ßÐÔÄܵĿªÔ´WebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷¡£ÔÚÏÖʵӦÓÃÖУ¬ÎÒÃǾ³£Ê¹ÓÃNginx×÷Ϊ·´ÏòÊðÀíÀ´ÊµÏÖ¸ºÔØƽºâ¡¢»º´æ¡¢SSLÖÕ¶ËÊðÀíµÈ¹¦Ð§¡£±¾ÎĽ«ÏÈÈÝÔõÑùÉèÖÃNginxµÄ·´ÏòÊðÀí£¬²¢¸ø³öÏêϸµÄ´úÂëʾÀý¡£
×°ÖÃNginx
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃNginx¡£¿ÉÒÔͨ¹ý°ü¹ÜÀíÆ÷£¨Èçapt-get¡¢yum£©À´×°Öã¬Ò²¿ÉÒÔ´Ó¹ÙÍøÏÂÔØÔ´Âë±àÒë×°Öá£ÕâÀïÒÔUbuntuΪÀý£¬Í¨¹ýapt-get×°ÖÃNginx£º
sudo apt-get update sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
ÉèÖ÷´ÏòÊðÀí
ÔÚNginxµÄÉèÖÃÎļþÖУ¬ÎÒÃÇÐèÒª¶Ô·´ÏòÊðÀí¾ÙÐÐÉèÖá£NginxµÄÉèÖÃÎļþͨ³£Î»ÓÚ/etc/nginx/nginx.conf»ò/etc/nginx/conf.d/default.conf¡£ÐÞ¸ÄÉèÖÃÎļþÇ°£¬½¨Ò鱸·ÝÔÓÐÉèÖá£
·¿ªÉèÖÃÎļþ£¬ÕÒµ½http½Úµã£¬²¢ÔڸýڵãÏÂÌí¼ÓÒÔÏÂÉèÖãº
server { listen 80; server_name example.com; location / { proxy_pass http://backend_server; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬listenÖ¸¶¨ÁËNginx¼àÌýµÄ¶Ë¿Ú£¨ÀýÈç80£©£¬server_nameÖ¸¶¨ÁË·´ÏòÊðÀíµÄÓòÃû£¨ÀýÈçexample.com£©¡£ÔÚlocation½ÚµãÖУ¬ÎÒÃÇÉèÖÃÁË·´ÏòÊðÀíµÄµØµã£¨ÀýÈçhttp://backend_server£©£¬²¢Ê¹ÓÃproxy_set_headerÖ¸Áîת´ïһЩHTTPÇëÇóÍ·ÐÅÏ¢¡£
ÉèÖúó¶ËЧÀÍÆ÷
ÔÚÉèÖÃÎļþÖУ¬ÎÒÃÇʹÓÃÁËhttp://backend_server×÷Ϊ·´ÏòÊðÀíµÄµØµã¡£ÕâÀïÐèÒª½«backend_serverÌ滻ΪÏÖʵµÄºó¶ËЧÀÍÆ÷µØµã¡£
¿ÉÒÔÓжàÖÖ·½·¨Ö¸¶¨ºó¶ËЧÀÍÆ÷µØµã¡£ÒÔÏÂÊÇÁ½ÖÖ³£¼ûµÄ·½·¨£º
A. ʹÓÃIPµØµãºÍ¶Ë¿Ú
proxy_pass http://192.168.1.100:8000;
µÇ¼ºó¸´ÖÆ
B. ʹÓÃÓòÃûºÍ¶Ë¿Ú
proxy_pass http://backend.example.com:8000;
µÇ¼ºó¸´ÖÆ
ÖØÆôNginx
Íê³ÉÉèÖúó£¬ÉúÑÄÉèÖÃÎļþ£¬²¢ÖØÐÂÆô¶¯NginxʹÉèÖÃÉúЧ£º
sudo service nginx restart
µÇ¼ºó¸´ÖÆ
ÑéÖ¤·´ÏòÊðÀí
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔͨ¹ý»á¼ûNginx¼àÌýµÄ¶Ë¿Ú£¨ÀýÈç80£©À´ÑéÖ¤·´ÏòÊðÀíÊÇ·ñÉúЧ¡£Í¨¹ýä¯ÀÀÆ÷»òcurlÏÂÁ»á¼ûhttp://example.com£¬Ó¦¸ÃÄܹ»»ñµÃºó¶ËЧÀÍÆ÷·µ»ØµÄÄÚÈÝ¡£
¸ß¼¶ÉèÖÃ
³ýÁË»ù±¾µÄ·´ÏòÊðÀíÉèÖÃÍ⣬Nginx»¹ÌṩÁËÐí¶à¸ß¼¶ÉèÖÃÑ¡Ï¿ÉÒÔ½øÒ»²½ÓÅ»¯·´ÏòÊðÀíµÄÐÔÄܺ͹¦Ð§¡£ÒÔÏÂÊÇһЩ³£Óõĸ߼¶ÉèÖÃʾÀý£º
A. ¸ºÔØƽºâ
upstream backend_servers { server backend1.example.com; server backend2.example.com; server backend3.example.com; } server { listen 80; server_name example.com; location / { proxy_pass http://backend_servers; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
µÇ¼ºó¸´ÖÆ
B. »º´æ
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_pass http://backend_server; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_cache my_cache; proxy_cache_valid 200 302 60m; proxy_cache_valid 404 1m; } } ... }
µÇ¼ºó¸´ÖÆ
C. SSLÖÕ¶ËÊðÀí
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; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏʾÀýÖ»ÊÇNginx·´ÏòÊðÀíÉèÖõĻù±¾Ó÷¨ºÍ³£¼û³¡¾°£¬²¢·ÇÏ꾡ÎÞÒÅ¡£¹ØÓÚÖØ´óµÄÓ¦Óó¡¾°£¬ÎÒÃÇ¿ÉÒÔƾ֤ÐèÒªÉîÈëÏàʶNginxµÄÉèÖÃÏîºÍÄ£¿é£¬ÎÞаÔËÓÃÆäÇ¿Ê¢µÄ¹¦Ð§À´ÊµÏÖËùÐèµÄÊðÀíºÍÓÅ»¯Õ½ÂÔ¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùʵÏÖNginxµÄ·´ÏòÊðÀíÉèÖ㬲¢¸ø³öÁËÏêϸµÄ´úÂëʾÀý¡£Í¨¹ýNginxµÄ·´ÏòÊðÀí£¬ÎÒÃÇ¿ÉÒÔʵÏÖ¸ºÔØƽºâ¡¢»º´æ¡¢SSLÖÕ¶ËÊðÀíµÈ¹¦Ð§£¬Ìá¸ßWebÓ¦ÓõÄÐÔÄܺͿɿ¿ÐÔ¡£Ï£Íû±¾ÎĶÔÊ״νӴ¥NginxµÄ¶ÁÕßÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÔõÑùʵÏÖNginx·´ÏòÊðÀíÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡