Ã붮nginxÉèÖý̳̣¬¿ìËÙÉÏÊÖ¿ª·¢ÍøÕ¾
Ã붮nginxÉèÖý̳̣¬¿ìËÙÉÏÊÖ¿ª·¢ÍøÕ¾
×î½ü£¬Ô½À´Ô½¶àµÄ¿ª·¢ÕßÑ¡ÔñʹÓÃnginx×÷ΪÍøÕ¾¿ª·¢µÄÊðÀíЧÀÍÆ÷¡£nginxÒÔÆä¸ßÐÔÄÜ¡¢µÍ×ÊÔ´ÏûºÄºÍÎÞаµÄÉèÖ㬳ÉΪÁËÐí¶àÈËÐÄÄ¿ÖеÄÊ×Ñ¡¡£¿ÉÊǹØÓÚÐÂÊÖÀ´Ëµ£¬ÉèÖÃnginx¿ÉÄÜ»áÓÐÒ»¶¨µÄÄѶȡ£±¾ÎĽ«´øÄãÒ»²½²½Ã÷È·nginxÉèÖ㬲¢Í¨¹ý´úÂëʾÀý¿ìËÙÉÏÊÖ¿ª·¢ÍøÕ¾¡£
Ò»¡¢×°ÖÃnginx
Ê×ÏÈ£¬ÄãÐèÒªÔÚÄãµÄµçÄÔ»òЧÀÍÆ÷ÉÏ×°ÖÃnginx¡£ÏêϸµÄ°ì·¨Òò²Ù×÷ϵͳ¶øÒ죬ÕâÀïÒÔUbuntu LinuxΪÀý£º
·¿ªÖնˣ¬ÊäÈëÒÔÏÂÏÂÁî×°ÖÃnginx£º
sudo apt-get update sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÊäÈëÒÔÏÂÏÂÁîÆô¶¯nginxЧÀÍ£º
sudo service nginx start
µÇ¼ºó¸´ÖÆ
·¿ªä¯ÀÀÆ÷£¬ÊäÈëhttp://localhost£¬ÈôÊÇÄÜ¿´µ½nginxµÄ½Ó´ýÒ³Ã棬Ôò˵Ã÷nginx×°ÖÃÀֳɡ£
¶þ¡¢ÉèÖÃnginx
nginxµÄÉèÖÃÎļþλÓÚ/etc/nginx/nginx.conf£¬ÎÒÃÇ¿ÉÒÔʹÓÃÈκÎÎı¾±à¼Æ÷·¿ªËü¡£ÒÔÏÂÊÇÒ»¸ö»ù±¾µÄnginxÉèÖÃʾÀý£º
worker_processes 1; events { worker_connections 1024; } http { server { listen 80; server_name localhost; location / { root /var/www/html; index index.html; } } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖãº
worker_processesÌåÏÖnginxʹÓõÄÀú³ÌÊý£¬Í¨³£ÉèÖÃΪCPU½¹µãÊýµÄ2±¶¡£
worker_connectionsÌåÏÖÿ¸öÀú³ÌµÄ×î´óÅþÁ¬Êý¡£
http¿éÓÃÓÚÉèÖÃHTTPЧÀÍ£¬Ä¬ÈϼàÌý80¶Ë¿Ú¡£
server¿éÓÃÓÚÉèÖÃÒ»¸öÐéÄâÖ÷»ú£¬¿ÉÒÔÓжà¸ö¡£
listenÖ¸¶¨¼àÌýµÄ¶Ë¿Ú¡£
server_nameÖ¸¶¨ÐéÄâÖ÷»úµÄÓòÃû¡£
location¿éÓÃÓÚÉèÖÃÇëÇóµÄ·Óɺʹ¦Àí·½·¨¡£
Èý¡¢¿ª·¢ÍøÕ¾
ÔÚ/var/www/htmlĿ¼Ï½¨ÉèÒ»¸öÃûΪexample.comµÄÎļþ¼Ð£¬ÓÃÓÚ´æ·ÅÍøÕ¾Îļþ¡£
ÔÚexample.comÎļþ¼ÐÖн¨ÉèÒ»¸öÃûΪindex.htmlµÄÎļþ£¬ÊäÈëÒÔÏÂÄÚÈÝ£º
<!DOCTYPE html> <html> <head> <title>Welcome to example.com</title> </head> <body> <h1>Hello, world!</h1> </body> </html>
µÇ¼ºó¸´ÖÆ
·¿ªÖնˣ¬ÊäÈëÒÔÏÂÏÂÁîÖØмÓÔØnginxµÄÉèÖÃÎļþ£º
sudo service nginx reload
µÇ¼ºó¸´ÖÆ
·¿ªä¯ÀÀÆ÷£¬ÊäÈëhttp://localhost»òhttp://example.com£¬Ä㽫¿´µ½ÊʲŽ¨ÉèµÄÍøÒ³¡£
ËÄ¡¢½øÒ»²½ÉèÖÃ
³ýÁË»ù±¾ÉèÖÃÍ⣬nginxÉÐÓÐÐí¶à¹¦Ð§Ç¿Ê¢µÄÄ£¿é¿ÉÒÔʹÓá£ÒÔÏÂÊÇһЩ³£ÓõÄÉèÖÃʾÀý£º
¾²Ì¬Îļþ»º´æ
http { server { ... location /static { root /var/www/html; expires 7d; } } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖý«/var/www/html/staticĿ¼Ïµľ²Ì¬Îļþ¾ÙÐлº´æ£¬ÓÐÓÃÆÚΪ7Ìì¡£
·´ÏòÊðÀí
http { server { ... location /api { proxy_pass http://api.example.com; } } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖý«Æ¥Åä/api·¾¶µÄÇëÇóת·¢¸øhttp://api.example.com¡£
¸ºÔØƽºâ
http { upstream backend { server backend1.example.com; server backend2.example.com; } server { ... location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖý«ÇëÇó·Ö·¢¸øbackendÖнç˵µÄ¶à¸öºó¶ËЧÀÍÆ÷£¬ÊµÏÖ¸ºÔØƽºâ¡£
Îå¡¢×ܽá
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÐÅÍи÷ÈËÒѾ¶ÔnginxµÄÉèÖÃÓÐÁËÒ»¶¨µÄÏàʶ£¬²¢Äܹ»¿ìËÙÉÏÊÖ¿ª·¢ÍøÕ¾¡£ËäÈ»£¬nginxµÄ¹¦Ð§Ô¶²»Ö¹ÓÚ´Ë£¬ÉÐÓÐÐí¶à¸ß¼¶ÌØÕ÷ºÍ²å¼þÆÚ´ýÄãȥ̽Ë÷¡£Ï£Íû±¾ÎĶÔÄãÓÐËù×ÊÖú£¬×£ÄãÔÚnginxµÄÌìÏÂÖпª·¢³öÒ»Á÷µÄÍøÕ¾£¡
ÒÔÉϾÍÊÇÃ붮nginxÉèÖý̳̣¬¿ìËÙÉÏÊÖ¿ª·¢ÍøÕ¾µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡