Nginx·´ÏòÊðÀíЧÀÍÆ÷µÄÅþÁ¬ÊýÏÞÖƺÍÇëÇóÐÐÁе÷ÓÅÒªÁì
Nginx·´ÏòÊðÀíЧÀÍÆ÷µÄÅþÁ¬ÊýÏÞÖƺÍÇëÇóÐÐÁе÷ÓÅÒªÁì
ÔÚÔËÐи߲¢·¢µÄÍøÂçÓ¦ÓóÌÐòʱ£¬Nginx·´ÏòÊðÀíЧÀÍÆ÷ÊÇÒ»ÖַǾ³£¼ûÇÒ¿É¿¿µÄÑ¡Ôñ¡£È»¶ø£¬ÈôÊÇûÓÐ׼ȷÉèÖÃÅþÁ¬ÊýÏÞÖƺ͵÷ÓÅÇëÇóÐÐÁУ¬Ð§ÀÍÆ÷¿ÉÄÜ»áÓöµ½ÐÔÄÜÆ¿¾±ºÍ¾Ü¾øЧÀ͵ÄÎÊÌâ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNginxÀ´ÏÞÖÆÅþÁ¬Êý²¢ÓÅ»¯ÇëÇóÐÐÁС£
ÅþÁ¬ÊýÏÞÖÆ
Nginx¿ÉÒÔͨ¹ýÉèÖÃworker_connections²ÎÊýÀ´ÏÞÖÆÅþÁ¬Êý¡£¸Ã²ÎÊýÖ¸¶¨ÁËÿ¸öworkerÀú³ÌÄܹ»Í¬Ê±´¦ÀíµÄ×î´óÅþÁ¬Êý¡£µ±ÅþÁ¬ÊýµÖ´ï¸ÃÏÞÖÆʱ£¬ÐµÄÅþÁ¬½«±»¾Ü¾ø¡£
·¿ªNginxÉèÖÃÎļþ£¬ÕÒµ½http¿é²¢Ìí¼Ó»òÐÞ¸ÄÒÔÏÂÐУº
http { ... worker_processes auto; worker_connections 1024; ... }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬worker_connectionsÉèÖÃΪ1024£¬ÕâÒâζ×Åÿ¸öworkerÀú³Ì¿ÉÒÔͬʱ´¦Àí1024¸öÅþÁ¬¡£Æ¾Ö¤Ð§ÀÍÆ÷µÄÓ²¼þÐÔÄܺÍÓ¦ÓóÌÐòÐèÇó£¬Äú¿ÉÒÔƾ֤ÏÖÕæÏàÐξÙÐе÷½â¡£Çë×¢ÖØ£¬worker_processesµÄÖµÓ¦ÉèÖÃΪCPU½¹µãÊýµÄ±¶Êý£¬ÒÔ³ä·ÖʹÓÃЧÀÍÆ÷×ÊÔ´¡£
ÇëÇóÐÐÁе÷ÓÅ
µ±²¢·¢ÅþÁ¬ÊýÁè¼Ýworker_connectionsÏÞÖÆʱ£¬Nginx½«°ÑÇëÇó·ÅÈëÐÐÁÐÖÐÆÚ´ý´¦Àí¡£Äú¿ÉÒÔµ÷½âÇëÇóÐÐÁеij¤¶ÈºÍ³¬Ê±Ê±¼ä£¬ÒÔ×îºéÁ÷ƽµØïÔܾ̾øЧÀ͵ĿÉÄÜÐÔ¡£
¼ÌÐø±à¼NginxÉèÖÃÎļþ£¬Ìí¼Ó»òÐÞ¸ÄÒÔÏÂÐУº
http { ... events { accept_mutex off; worker_connections 1024; worker_processes auto; multi_accept on; use epoll; ... } ... server { ... location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 600s; proxy_connect_timeout 600s; proxy_send_timeout 600s; proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_buffering on; ... } ... } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃǶÔÇëÇóÐÐö¾ÙÐÐÁ˼¸´¦µ÷ÓÅ¡£Ê×ÏÈ£¬Í¨¹ýÉèÖÃaccept_mutexΪoff£¬ÎÒÃǽûÓÃÁË»¥³âËø£¬ÒÔ±ã¶à¸öworkerÀú³Ì¿ÉÒÔͬʱ½ÓÊÜеÄÅþÁ¬¡£Æä´Î£¬ÉèÖÃmulti_acceptΪon£¬ÒÔ±ãNginx¾¡¿ì´¦ÀíÐÐÁÐÖеÄËùÓÐÇëÇó¡£×îºó£¬ÎÒÃÇƾ֤ÏÖʵÐèÇóÉèÖÃÁ˳¬Ê±Ê±¼ä»ººÍ³åÇø¾Þϸ¡£
´úÂëʾÀý
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄNode.jsЧÀÍÆ÷µÄʾÀý£¬Ä£Äâºó¶ËÓ¦ÓóÌÐò¡£
const http = require('http'); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, World! '); }); server.listen(3000, 'localhost', () => { console.log('Server running at http://localhost:3000/'); });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸ö¼òÆÓµÄHTTPЧÀÍÆ÷£¬¼àÌýÔÚÍâµØµÄ3000¶Ë¿ÚÉÏ¡£ÓÉÓÚ´ËЧÀÍÆ÷½öÓÃÓÚÑÝʾĿµÄ£¬ËüÖ»»á·µ»ØÒ»¸ö¼òÆӵġ°Hello, World!¡±×Ö·û´®¡£
ҪʹÓÃNginx×÷Ϊ·´ÏòÊðÀíЧÀÍÆ÷£¬ÄúÐèÒª½«ÉÏÊöʾÀý´úÂëÉúÑÄΪһ¸öÃûΪserver.jsµÄÎļþ£¬²¢Ö´ÐÐÒÔÏÂÏÂÁîÀ´Æô¶¯Ð§ÀÍÆ÷£º
node server.js
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬½«ÒÔÏÂNginxÉèÖÃÉúÑÄΪһ¸öÃûΪnginx.confµÄÎļþ£º
http { ... server { listen 80; location / { proxy_pass http://localhost:3000; } } }
µÇ¼ºó¸´ÖÆ
ͨ¹ýÖ´ÐÐÒÔÏÂÏÂÁîÀ´Æô¶¯Nginx£º
nginx -c /path/to/nginx.conf
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬Äú¿ÉÒÔͨ¹ý»á¼ûhttp://localhostÀ´»á¼ûÄúµÄÓ¦ÓóÌÐò¡£ËùÓеÄÇëÇ󶼽«Í¨¹ýNginxЧÀÍÆ÷¾ÙÐÐÊðÀíºÍ¸ºÔØƽºâ¡£
×ܽá
ͨ¹ýÏÞÖÆÅþÁ¬ÊýºÍµ÷ÓÅÇëÇóÐÐÁУ¬Äú¿ÉÒÔ¸üºÃµØÖÎÀí¸ß²¢·¢µÄÍøÂçÓ¦ÓóÌÐò¡£ÒÔÉÏÏÈÈÝÁËÔõÑùʹÓÃNginxÀ´ÏÞÖÆÅþÁ¬Êý²¢ÓÅ»¯ÇëÇóÐÐÁеÄÒªÁ죬²¢ÌṩÁËÒ»¸ö¼òÆÓµÄNode.jsЧÀÍÆ÷µÄ´úÂëʾÀý¡£Çëƾ֤ÏÖʵÐèÇó£¬Æ¾Ö¤Ð§ÀÍÆ÷µÄÓ²¼þÐÔÄܾÙÐÐÊʵ±µÄÉèÖõ÷½â¡£
ÒÔÉϾÍÊÇNginx·´ÏòÊðÀíЧÀÍÆ÷µÄÅþÁ¬ÊýÏÞÖƺÍÇëÇóÐÐÁе÷ÓÅÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡