SwooleÓëNginx·´ÏòÊðÀíÓ븺ÔØƽºâµÄÍêÉÆÈÚºÏ
swooleÓënginx·´ÏòÊðÀíÓ븺ÔØƽºâµÄÍêÉÆÈÚºÏ
Ëæ×Å»¥ÁªÍøÓ¦ÓõÄÉú³¤£¬Ëæ×Å»á¼ûÁ¿ºÍ²¢·¢Á¿µÄÔöÌí£¬¼òµ¥µÄÓ¦ÓóÌÐòÍùÍùÒѾÎÞ·¨Öª×ãÐèÇó¡£ÎªÏàʶ¾öÕâ¸öÎÊÌ⣬ÈËÃÇ×îÏÈʹÓøºÔØƽºâÊÖÒÕ£¬½«ÇëÇó·Ö·¢µ½¶à¸öЧÀÍÆ÷ÉÏ£¬´Ó¶øÌá¸ßϵͳºÍÓ¦ÓõĿÉÓÃÐÔºÍÎȹÌÐÔ¡£È»¶ø£¬²î±ðµÄ¸ºÔØƽºâÊÖÒÕÓвî±ðµÄÓÅÈõµã¡£µ±½ñ£¬SwooleºÍNginx·´ÏòÊðÀíÊǽÏÁ¿Ê¢ÐеĸºÔØƽºâÊÖÒÕ£¬ÔõÑù½«¶þÕßÍêÉÆÈںϣ¬ÒÔʵÏÖ¸ü¸ßЧ¸üÎȹ̵ÄЧÀÍÄØ£¿
Ê×ÏÈ£¬NginxÊÇÒ»¸ö¸ßЧµÄ·´ÏòÊðÀíЧÀÍÆ÷£¬³£ÓÃÓÚ¸ºÔØƽºâ¡£ÆäÖ÷Òª×÷ÓÃÊǽ«¿Í»§¶ËµÄÇëÇóת·¢µ½ºó¶ËµÄЧÀÍÆ÷ÉÏ£¬²¢½«ºó¶ËЧÀÍÆ÷µÄÏìÓ¦ÐÅÏ¢·µ»Ø¸ø¿Í»§¶Ë¡£¿ÉÊÇ£¬Nginx²¢²»¿É´¦Àí³¤ÅþÁ¬ºÍÒì²½IOµÈÎÊÌ⣬Òò´Ë£¬SwooleÊ©Õ¹ÁËÖ÷ÒªµÄ×÷Óá£
SwooleÊÇÒ»¸ö»ùÓÚPHPÓïÑÔ¿ª·¢µÄÒì²½¡¢ÊÂÎñÇý¶¯µÄÍøÂçͨѶ¿ò¼Ü¡£Æä×î´óµÄÌصã¾ÍÊÇÖ§³ÖÒì²½IOºÍ³¤ÅþÁ¬£¬¿ÉÒÔ³ä·ÖʹÓÃЧÀÍÆ÷µÄÐÔÄܺÍ×ÊÔ´¡£Swoole²»µ«Ö§³ÖHTTPÐÒ飬»¹Ö§³ÖTCP¡¢UDP¡¢WebSocketµÈ¶àÖÖÐÒ飬¿ÉÒÔÖª×ãÖݪֲî±ðµÄÓ¦Óó¡¾°¡£
½«SwooleºÍNginxÍŽáÆðÀ´Ê¹Ó㬿ÉÒÔ³ä·ÖÑéÕ¹ËüÃǸ÷×ÔµÄÓŵ㡣ÀýÈ磬¿ÉÒÔͨ¹ýNginx·´ÏòÊðÀí½«ÇëÇó·Ö·¢µ½¶ą̀ºó¶ËЧÀÍÆ÷ÉÏ£¬´Ó¶øʵÏÖ¸ºÔØƽºâ£»Í¬Ê±£¬ÔÚÿ̨ºó¶ËЧÀÍÆ÷ÉÏ£¬Ê¹ÓÃSwooleÀ´´¦ÀíÇëÇ󣬿ÉÒÔÌá¸ßЧÀÍÆ÷µÄÐÔÄܺÍÏìÓ¦ËÙÂÊ¡£
ÏÂÃ棬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃSwooleºÍNginx·´ÏòÊðÀíÀ´ÊµÏÖ¸ºÔØƽºâºÍ¸ßÐÔÄܵÄWebЧÀÍ¡£
Ê×ÏÈ£¬ÐèҪװÖò¢ÉèÖÃNginx·´ÏòÊðÀíЧÀÍÆ÷¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄNginxÉèÖÃÎļþʾÀý£º
http { upstream backend { server 127.0.0.1:8000; server 127.0.0.1:8001; server 127.0.0.1:8002; } 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; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖÃÖ¸¶¨Á˶ą̀ºó¶ËЧÀÍÆ÷£¬½«ÇëÇó·Ö·¢µ½ÕâЩЧÀÍÆ÷ÉÏ¡£¼ÙÉèSwoole¼àÌýµÄ¶Ë¿ÚΪ8000¡¢8001¡¢8002µÈ£¬¿ÉÒÔÔÚÿ̨ЧÀÍÆ÷ÉÏÆô¶¯SwooleЧÀÍ¡£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄSwoole HTTPЧÀÍÆ÷ʾÀý£º
<?php $http = new swoole_http_server('127.0.0.1', 8000); $http->on('request', function ($request, $response) { $response->header('Content-Type', 'text/plain'); $response->end("Hello world "); }); $http->start();
µÇ¼ºó¸´ÖÆ
µ±Óпͻ§¶ËÇëÇóµÖ´ïʱ£¬Swoole½«Òì²½µØ´¦ÀíÕâ¸öÇëÇ󣬲¢·µ»ØÏìÓ¦¡£ÓÉÓÚSwooleÊÇÒì²½ºÍÊÂÎñÇý¶¯µÄ£¬¿ÉÒÔÖ§³Ö´ó¹æÄ£µÄ²¢·¢ÇëÇ󣬴ӶøÌá¸ßЧÀÍÆ÷µÄÐÔÄܺÍÏìÓ¦ËÙÂÊ¡£
ͨ¹ýÒÔÉϵÄÅäºÏ£¬ÎÒÃÇ¿ÉÒÔʵÏÖÒ»¸ö¸ßÐÔÄÜ¡¢¸ß¿ÉÓúͿÉÉìËõµÄWebЧÀÍ¡£ËäÈ»£¬ÔÚÏÖʵӦÓÃÖУ¬»¹ÐèҪ˼Á¿¸ºÔØƽºâËã·¨¡¢¶¯Ì¬À©ÈݺÍËõÈÝ¡¢Êý¾Ýͬ²½µÈÎÊÌâ¡£ÕâЩ¶¼ÐèҪƾ֤ÏêϸµÄÓ¦Óó¡¾°ºÍÐèÇó¾ÙÐÐÍýÏëºÍÉè¼Æ¡£
×ÜÖ®£¬SwooleºÍNginx·´ÏòÊðÀíÊÇÁ½¸öºÜÊÇÓÅÒìµÄÊÖÒÕ£¬ÍŽáÆðÀ´¿ÉÒÔʵÏÖ¸ü¸ßЧ¸üÎȹ̵ÄЧÀÍ¡£ÐÅÍÐËæ×Å»¥ÁªÍøÓ¦ÓõÄÉú³¤£¬ÕâÁ½¸öÊÖÒÕµÄְλºÍÖ÷ÒªÐÔ½«»áÈÕÒæÌá¸ß¡£
ÒÔÉϾÍÊÇSwooleÓëNginx·´ÏòÊðÀíÓ븺ÔØƽºâµÄÍêÉÆÈںϵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡