ÔõÑùʹÓÃNginxʵÏÖ»ùÓÚÓòÃûµÄÐéÄâÖ÷»úÉèÖÃ
ÔõÑùʹÓÃnginxʵÏÖ»ùÓÚÓòÃûµÄÐéÄâÖ÷»úÉèÖÃ
¸ÅÊö£º
ÔÚWebЧÀÍÆ÷ÖУ¬ÐéÄâÖ÷»úÊÇָһ̨ÎïÀíЧÀÍÆ÷ÉÏÔËÐжà¸öÍйÜÓòÃûµÄWebÍøÕ¾¡£Í¨¹ýʹÓÃÐéÄâÖ÷»úÉèÖ㬿ÉÒÔʵÏÖ¶à¸öÓòÃû¹²Ïíͳһ̨ЧÀÍÆ÷£¬²¢ÇÒÿ¸öÓòÃû¶¼¿ÉÒÔÓÐ×ÔÁ¦µÄÉèÖúÍÄÚÈÝ¡£NginxÊÇÒ»¸ö¿ªÔ´µÄ¸ßÐÔÄÜWebЧÀÍÆ÷£¬Ò²¿ÉÒÔÓÃ×÷·´ÏòÊðÀíЧÀÍÆ÷ºÍ¸ºÔØƽºâÆ÷¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃnginxʵÏÖ»ùÓÚÓòÃûµÄÐéÄâÖ÷»úÉèÖá£
°ì·¨£º
×°ÖÃNginx£º
Ê×ÏÈ£¬ÐèÒªÔÚЧÀÍÆ÷ÉÏ×°ÖÃNginx¡£ÏêϸװÖ÷½·¨¿ÉÒԲο¼Nginx¹Ù·½Îĵµ»òÕ߸÷¸ö²Ù×÷ϵͳµÄÖ¸ÄÏ¡£
½¨ÉèÓòÃû¶ÔÓ¦µÄÉèÖÃÎļþ£º
ÔÚNginxµÄÉèÖÃÎļþ¼ÐÖУ¬Í¨³£ÊÇ/etc/nginx/£¬¿ÉÒÔΪÿ¸öÓòÃû½¨ÉèÒ»¸öµ¥¶ÀµÄÉèÖÃÎļþ¡£Ê¹ÓÃÎı¾±à¼Æ÷£¬½¨ÉèÒ»¸öÒÔÓòÃûÃüÃûµÄÉèÖÃÎļþ£¬ºÃ±Èexample.com.conf¡£
ÉèÖÃÐéÄâÖ÷»ú£º
ÔÚ½¨ÉèµÄÉèÖÃÎļþÖУ¬ÉèÖÃÐéÄâÖ÷»úµÄÏà¹ØÐÅÏ¢¡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖãº
server { listen 80; server_name example.com; root /path/to/website; index index.html; location / { try_files $uri $uri/ =404; } }
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬listenÖ¸¶¨Ð§ÀÍÆ÷¼àÌýµÄ¶Ë¿ÚºÅ£¨80ÊÇHTTPµÄĬÈ϶˿ڣ©£¬server_nameÖ¸¶¨ÓòÃû£¬rootÖ¸¶¨ÍøÕ¾µÄ¸ùĿ¼£¬indexÖ¸¶¨Ä¬ÈϵÄË÷ÒýÎļþ¡£location²¿·ÖÓÃÓÚ´¦ÀíURLÇëÇóµÄÂß¼£¬Ê¾ÀýÖеÄÉèÖÃÊǼòÆÓµØʵÑé²éÕÒÎļþ£¬ÈôÊÇÕÒ²»µ½Ôò·µ»Ø404¹ýʧ¡£
¼¤»îÐéÄâÖ÷»úÉèÖãº
ÔÚNginxµÄÖ÷ÉèÖÃÎļþ£¨Í¨³£ÊÇ/etc/nginx/nginx.conf£©ÖÐÒýÈ뽨ÉèµÄÉèÖÃÎļþ¡£·¿ªnginx.confÎļþ£¬ÕÒµ½http²¿·Ö£¬½«ÒÔÏÂÄÚÈÝÌí¼Óµ½ÆäÖУº
include /etc/nginx/*.conf;
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖý«»áÈÃNginx¼ÓÔØ/etc/nginxĿ¼ÏµÄËùÓÐÒÔ.confΪºó׺µÄÉèÖÃÎļþ¡£
ÖØÆôNginxЧÀÍ£º
Íê³ÉÉèÖúó£¬ÐèÒªÖØÐÂÆô¶¯NginxЧÀÍ£¬Ê¹µÃÉèÖÃÉúЧ¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÖØÆôNginx£º
sudo service nginx restart
µÇ¼ºó¸´ÖÆ
²âÊÔÉèÖãº
ͨ¹ý»á¼ûÄãµÄÓòÃûÀ´²âÊÔÉèÖÃÊÇ·ñÉúЧ¡£ÔÚä¯ÀÀÆ÷ÖÐÊäÈëhttp://example.com£¬²¢È·ÈÏÊÇ·ñÏÔʾÁË׼ȷµÄÄÚÈÝ¡£
ÉèÖÃÆäËûÓòÃû£º
ÈôÊÇÄãÓжà¸öÓòÃûÐèÒªÉèÖ㬿ÉÒÔÖظ´ÒÔÉÏ°ì·¨¡£Ã¿¸öÓòÃûÓ¦¸ÃÓÐÒ»¸öµ¥¶ÀµÄÉèÖÃÎļþ£¬²¢ÇÒ¼àÌý²î±ðµÄ¶Ë¿Ú£¬»òÕßʹÓÃSSLÖ¤Êé¾ÙÐÐHTTPSÉèÖá£
×ܽ᣺
ͨ¹ýÉÏÊö°ì·¨£¬Äã¿ÉÒÔÀֳɵØʹÓÃNginxʵÏÖ»ùÓÚÓòÃûµÄÐéÄâÖ÷»úÉèÖá£ÐéÄâÖ÷»úÉèÖÿÉÒÔ×ÊÖúÄãÔÚµ¥¸öЧÀÍÆ÷ÉÏÍйܶà¸öÓòÃû£¬²¢Ìṩ×ÔÁ¦µÄÉèÖúÍÄÚÈÝ¡£Nginx×÷Ϊһ¸ö¸ßÐÔÄܵÄWebЧÀÍÆ÷£¬ÊǴÐéÄâÖ÷»úµÄÀíÏëÑ¡Ôñ¡£Ï£Íû±¾ÎĶÔÄãÃ÷È·ÔõÑùÉèÖÃNginxµÄÐéÄâÖ÷»úÓÐËù×ÊÖú¡£
²Î¿¼´úÂ룺
ÒÔÏÂÊÇNginxÉèÖÃÎļþʾÀý£º
server { listen 80; server_name example.com; root /path/to/website; index index.html; location / { try_files $uri $uri/ =404; } }
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
½«ÉÏÊö´úÂëÉúÑÄΪexample.com.confÎļþ£¬²¢ÇÒ½«ÆäÒýÈëµ½NginxµÄÖ÷ÉèÖÃÎļþÖС£
ÒÔÉϾÍÊÇÔõÑùʹÓÃNginxʵÏÖ»ùÓÚÓòÃûµÄÐéÄâÖ÷»úÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡