ÔÚSwooleÖÐʹÓÃNginxµÄ·´ÏòÊðÀíÓ븺ÔØƽºâµÄ½Ì³Ì
swooleÊÇÒ»ÖÖÕë¶ÔphpÓïÑÔµÄÇáÁ¿¼¶¸ßÐÔÄÜÍøÂçͨѶ¿â£¬ËüµÄ·ºÆð´ó´óÌá¸ßÁËphpÓ¦ÓõÄÐÔÄܺͿÉÀ©Õ¹ÐÔ¡£¶ønginxÊÇÒ»¿îÊ¢ÐеÄwebЧÀÍÆ÷£¬Ò²±»ÆÕ±éÓÃÓÚ·´ÏòÊðÀíºÍ¸ºÔØƽºâ¡£ÔÚswooleÖÐʹÓÃnginxµÄ·´ÏòÊðÀíÓ븺ÔØƽºâ¿ÉÒÔ¸üºÃµØÊ©Õ¹swooleµÄÓÅÊÆ¡£ÏÂÃæÊÇʹÓÃnginxʵÏÖswoole·´ÏòÊðÀíÓ븺ÔØƽºâµÄ½Ì³Ì¡£
×°ÖÃNginx
Ê×ÏÈÐèҪװÖÃNginx£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
sudo apt-get update sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
ÉèÖÃNginx
ÔÚ×°ÖÃÍê³Éºó£¬ÐèÒª¶ÔNginx¾ÙÐÐÉèÖá£ÉèÖÃÎļþλÓÚ/etc/nginx/sites-available/default¡£·¿ª¸ÃÎļþ¾ÙÐб༣¬½«ÆäÐÞ¸ÄΪÈçÏÂÄÚÈÝ£º
upstream swoole { server 127.0.0.1:9501 weight=10 max_fails=3 fail_timeout=30s; server 127.0.0.1:9502 weight=5 max_fails=3 fail_timeout=30s; } server { listen 80; server_name example.com; location / { proxy_pass http://swoole; 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µÄupstream£¬ÄÚÀï°üÀ¨ÁËSwooleЧÀÍËù¼àÌýµÄÁ½¸öµØµãºÍȨÖØ¡£ÎÒÃÇ»¹ÉèÖÃÁËÒ»¸öÐéÄâÖ÷»ú£¬¼àÌý80¶Ë¿Ú£¬½«ÇëÇóͨ¹ý·´ÏòÊðÀíµÄ·½·¨·Ö·¢µ½swoole¼¯ÈºÖС£
±àдSwooleЧÀÍ
½ÓÏÂÀ´ÐèÒª±àдһ¸ö¼òÆÓµÄSwooleЧÀÍ£¬ÓÃÓÚÎüÊÕNginxת·¢µÄÇëÇó¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀý´úÂ룺
<?php $http = new SwooleHttpServer("0.0.0.0", 9501); $http->on('request', function ($request, $response) { $response->header("Content-Type", "text/plain"); $response->end("Hello World "); }); $http->start();
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öÔÚ9501¶Ë¿Ú¼àÌýµÄSwoole HttpЧÀÍ£¬²¢ÔÚÇëÇó»Øµ÷º¯ÊýÖзµ»ØÁËÒ»¸öHello World×Ö·û´®¡£
Æô¶¯SwooleЧÀÍ
ÔËÐÐÒÔÏÂÏÂÁîÆô¶¯SwooleЧÀÍ£º
php server.php
µÇ¼ºó¸´ÖÆ
´Ëʱ£¬SwooleЧÀÍÒѾÆô¶¯Àֳɣ¬²¢¼àÌýÁË9501¶Ë¿Ú¡£
Æô¶¯Nginx
×îºó£¬ÔËÐÐÒÔÏÂÏÂÁîÆô¶¯Nginx£º
sudo service nginx start
µÇ¼ºó¸´ÖÆ
´Ëʱ£¬NginxÒѾÆô¶¯Àֳɣ¬¿ÉÒÔͨ¹ý»á¼ûhttp://example.com/À´²âÊÔSwooleЧÀ͵ÄÏìÓ¦¡£
²âÊÔ¸ºÔØƽºâ
ΪÁ˲âÊÔNginxµÄ¸ºÔØƽºâ¹¦Ð§£¬ÎÒÃÇ¿ÉÒÔÔÙÆô¶¯Ò»¸öSwoole HttpЧÀÍÀ´Ä£ÄâÁíһ̨ЧÀÍÆ÷£¬ÔËÐÐÒÔÏÂÏÂÁîÆô¶¯£º
php server.php --port=9502
µÇ¼ºó¸´ÖÆ
´Ëʱ£¬ÔÚNginxµÄÉèÖÃÎļþÖУ¬ÎÒÃǽç˵ÁËswoole¼¯ÈºÖÐÒ»¸ö¶Ë¿ÚµÄȨÖØΪ10£¬ÁíÒ»¸ö¶Ë¿ÚµÄȨÖØΪ5¡£Ò²¾ÍÊÇ˵£¬Nginx»á½«Ô¼Äª2/3µÄÇëÇóת·¢µ½9501¶Ë¿ÚÉÏ£¬1/3µÄÇëÇóת·¢µ½9502¶Ë¿ÚÉÏ¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÔÚä¯ÀÀÆ÷Öжà´ÎË¢ÐÂhttp://example.com/À´²âÊÔ¸ºÔØƽºâ¹¦Ð§¡£
×ܽá
ͨ¹ýÒÔÉϵIJÙ×÷£¬ÎÒÃÇÒѾÀֳɵØʵÏÖÁËÔÚSwooleÖÐʹÓÃNginxµÄ·´ÏòÊðÀíÓ븺ÔØƽºâ¹¦Ð§¡£NginxµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâ¹¦Ð§¿ÉÒÔ×ÊÖúÎÒÃǸüºÃµØ·Ö·¢SwooleÓ¦ÓÃÖеÄÇëÇó£¬Ìá¸ß¶¯Ì¬ÍøÕ¾µÄ²¢·¢ÄÜÁ¦ºÍÎȹÌÐÔ¡£
ÒÔÉϾÍÊÇÔÚSwooleÖÐʹÓÃNginxµÄ·´ÏòÊðÀíÓ븺ÔØƽºâµÄ½Ì³ÌµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡