Nginx´î½¨Ð§ÀÍÆ÷µÄ×°ÖúÍÉèÖÃÖ¸ÄÏÏêϸ½â¶Á
nginx´î½¨Ð§ÀÍÆ÷µÄ×°ÖúÍÉèÖÃÖ¸ÄÏÏêϸ½â¶Á
NginxÊÇÒ»¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷Èí¼þ£¬Í¨¹ýÆä·´ÏòÊðÀí¡¢¸ºÔØƽºâÒÔ¼°¾²Ì¬×ÊÔ´»º´æµÈ¹¦Ð§£¬¿ÉÒÔÏÔÖøÌáÉýÍøÕ¾µÄ²¢·¢´¦ÀíÄÜÁ¦ºÍ»á¼ûËÙÂÊ¡£±¾ÎĽ«Ïêϸ½â¶ÁÔõÑù×°ÖúÍÉèÖÃNginxЧÀÍÆ÷£¬²¢¸½´øÏìÓ¦µÄ´úÂëʾÀý×ÊÖú¶ÁÕß¿ìËÙÉÏÊÖ¡£
×°ÖÃNginx
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚЧÀÍÆ÷ÉÏ×°ÖÃNginxÈí¼þ¡£ÏÂÃæÊÇÔÚUbuntuϵͳÉÏ×°ÖÃNginxµÄÏÂÁ
sudo apt-get update sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
ÔÚ×°ÖÃÍê³Éºó£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÑéÖ¤NginxÊÇ·ñ×°ÖÃÀֳɣº
nginx -v
µÇ¼ºó¸´ÖÆ
ÉèÖÃNginxЧÀÍÆ÷
2.1. »ù±¾ÉèÖÃ
NginxµÄÖ÷ÒªÉèÖÃÎļþλÓÚ/etc/nginx/nginx.conf£¬ÎÒÃÇ¿ÉÒÔͨ¹ý±à¼¸ÃÎļþÀ´ÉèÖÃЧÀÍÆ÷µÄ»ù±¾ÉèÖá£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄNginxÉèÖÃʾÀý£º
user www-data; worker_processes auto; worker_rlimit_nofile 65535; pid /var/run/nginx.pid; events { worker_connections 16384; } http { include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; server { listen 80; server_name example.com; root /var/www/html; location / { try_files $uri $uri/ =404; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃÇÖ¸¶¨ÁËNginxʹÓõÄÓû§ºÍÀú³ÌÊý£¬²¢ÉèÖÃÁËһЩÓëÐÔÄܺÍÅþÁ¬Ïà¹ØµÄ²ÎÊý¡£worker_connectionsÌåÏÖÿ¸öworkerÀú³ÌÓë¿Í»§¶ËµÄ×î´óÅþÁ¬Êý¡£access_logºÍerror_log»®·ÖÉèÖÃÁË»á¼ûÈÕÖ¾ºÍ¹ýʧÈÕÖ¾µÄ´æ´¢Â·¾¶ºÍÎļþÃû¡£
2.2. ÐéÄâÖ÷»úÉèÖÃ
ÐéÄâÖ÷»úÊÇÖ¸ÔÚһ̨ÎïÀíÖ÷»úÉÏͨ¹ý²î±ðµÄÉèÖÃÀ´ÖÎÀí¶à¸öÍøÕ¾¡£Nginxͨ¹ýÔÚÉèÖÃÎļþÖнç˵¶à¸öserver¿éµÄ·½·¨À´ÊµÏÖÐéÄâÖ÷»úÉèÖá£ÏÂÃæÊÇÒ»¸öÐéÄâÖ÷»úÉèÖÃʾÀý£º
server { listen 80; server_name example.com; root /var/www/example.com; location / { try_files $uri $uri/ =404; } } server { listen 80; server_name another-example.com; root /var/www/another-example.com; location / { try_files $uri $uri/ =404; } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǽç˵ÁËÁ½¸öÐéÄâÖ÷»ú£¬»®·Ö¶ÔÓ¦²î±ðµÄÓòÃû¡£Ã¿¸öÐéÄâÖ÷»úµÄÉèÖÃÏ໥×ÔÁ¦£¬Í¨¹ýserver_nameÖ¸¶¨Á˶ÔÓ¦µÄÓòÃû£¬rootÖ¸¶¨ÁËÍøÕ¾ÎļþµÄ¸ùĿ¼¡£
Nginx³£ÓÃÏÂÁîºÍ²Ù×÷
×°ÖúÍÉèÖÃNginxºó£¬ÎÒÃÇ»¹ÐèÒªÏàʶһЩ³£ÓõÄÏÂÁîºÍ²Ù×÷À´ÖÎÀíºÍ²Ù×÷NginxЧÀÍÆ÷¡£
Æô¶¯Nginx£ºsudo service nginx start
×èÖ¹Nginx£ºsudo service nginx stop
ÖØÆôNginx£ºsudo service nginx restart
ÖØмÓÔØÉèÖÃÎļþ£ºsudo service nginx reload
Éó²éNginx״̬£ºsudo service nginx status
¼ì²éÉèÖÃÎļþµÄÓï·¨¹ýʧ£ºsudo nginx -t
³ýÁ˳£ÓõÄÏÂÁîÍ⣬Nginx»¹ÌṩÁËһЩÆäËûµÄ¹¦Ð§ºÍÌØÕ÷£¬ÈçURLÖØд¡¢SSLÖ¤ÊéÉèÖá¢GzipѹËõµÈ¡£¶ÁÕß¿ÉÒÔƾ֤×Ô¼ºµÄÐèҪȥ½øÒ»²½Ñо¿ºÍʹÓá£
×ܽ᣺
±¾ÎÄÏêϸ½â¶ÁÁËÔõÑù×°ÖúÍÉèÖÃNginxЧÀÍÆ÷£¬²¢¸½´øÁËÏìÓ¦µÄ´úÂëʾÀý¡£Í¨¹ýѧϰºÍʵ¼ù£¬¶ÁÕß¿ÉÒÔ¿ìËÙÉÏÊÖʹÓÃNginx£¬²¢Æ¾Ö¤×Ô¼ºµÄÐèÇó¾ÙÐнøÒ»²½µÄÉèÖúÍÓÅ»¯¡£Ï£Íû±¾ÎÄÄܹ»¶Ô¶ÁÕßÔڴºÍÉèÖÃNginxЧÀÍÆ÷ʱÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇNginx´î½¨Ð§ÀÍÆ÷µÄ×°ÖúÍÉèÖÃÖ¸ÄÏÏêϸ½â¶ÁµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡