ÔõÑùÔÚLinuxÉÏÉèÖ÷´ÏòÊðÀí£¨ÈçNginx£©
ÔõÑùÔÚlinuxÉÏÉèÖ÷´ÏòÊðÀí£¨Èçnginx£©
СÐò£º
ÔÚÏÖ´ú»¥ÁªÍøÓ¦ÓÃÖУ¬·´ÏòÊðÀíÊÇÒ»¸ö³£¼ûµÄ×é¼þ£¬ÓÃÓÚÌṩ¸ßÐÔÄܺͿÉÀ©Õ¹ÐÔ¡£NginxÊÇÒ»¿îÊ¢ÐеĿªÔ´Èí¼þ£¬Ëü¿ÉÒÔÓÃ×÷·´ÏòÊðÀíЧÀÍÆ÷¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚLinuxÉÏÉèÖÃNginx×÷Ϊ·´ÏòÊðÀíЧÀÍÆ÷£¬²¢Ìṩ´úÂëʾÀý¡£
Ò»¡¢×°ÖÃNginx
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃNginx¡£ÔÚ´ó´ó¶¼Linux¿¯ÐаæÖУ¬¿ÉÒÔʹÓðü¹ÜÀíÆ÷¿ìËÙ×°ÖÃNginx¡£ÒÔÏÂÊǼ¸¸ö³£¼û°ü¹ÜÀíÆ÷µÄ×°ÖÃÏÂÁîʾÀý£º
¹ØÓÚDebianºÍUbuntu¿¯Ðа棺
sudo apt update
sudo apt install nginx
¹ØÓÚCentOSºÍFedora¿¯Ðа棺
sudo yum install epel-release
sudo yum install nginx
×°ÖÃÍê³Éºó£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´ÑéÖ¤NginxÊÇ·ñÒÑ׼ȷװÖãº
sudo systemctl status nginx
ÈôÊÇ״̬ÏÔʾΪ¡°active (running)¡±£¬ÔòÌåÏÖNginxÒÑÀÖ³É×°Öò¢ÕýÔÚÔËÐС£
¶þ¡¢ÉèÖ÷´ÏòÊðÀí
ÔÚNginxµÄÉèÖÃÎļþÖУ¬ÎÒÃÇ¿ÉÒÔ±à¼Î»ÓÚ¡°/etc/nginx/nginx.conf¡±µÄÖ÷ÉèÖÃÎļþ£¬ÒÔÉèÖ÷´ÏòÊðÀí¡£
·¿ª¸ÃÎļþ²¢ÕÒµ½¡°http {¡±¿ªÍ·µÄ²¿·Ö£¬È»ºóÔڸò¿·ÖµÄĩβÌí¼ÓÒÔÏÂÉèÖôúÂ룺
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://webserver; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr;
µÇ¼ºó¸´ÖÆ
}
}
ÔÚÒÔÉÏʾÀýÖУ¬¡°example.com¡±ÊÇÄúµÄÓòÃû»òIPµØµã£¬¿ÉÒÔ½«ÆäÌ滻ΪÏÖʵֵ¡£ÔÚ¡°proxy_pass¡±Ö¸ÁîÖУ¬ÎÒÃÇʹÓÃÁË¡°http://webserver¡±×÷Ϊºó¶ËЧÀÍÆ÷µÄµØµã¡£Äú¿ÉÒÔÌ滻ΪÄúÏÖʵµÄºó¶ËЧÀÍÆ÷µØµã¡£
ÉúÑÄÉèÖÃÎļþºó£¬ÖØмÓÔØNginxÉèÖãº
sudo systemctl reload nginx
´Ëʱ£¬NginxÒѾÉèÖÃΪ·´ÏòÊðÀíЧÀÍÆ÷¡£
Èý¡¢ÑéÖ¤·´ÏòÊðÀí
ÒªÑéÖ¤NginxµÄ·´ÏòÊðÀíÊÇ·ñÕý³£ÊÂÇ飬¿ÉÒÔͨ¹ýÔÚä¯ÀÀÆ÷Öлá¼ûÉèÖõÄÓòÃû»òIPµØµãÀ´²âÊÔ¡£
ÔÚä¯ÀÀÆ÷ÖÐÊäÈë¡°http://example.com¡±£¬½«»á¿´µ½NginxÀֳɽ«ÇëÇóÊðÀíµ½ºó¶ËЧÀÍÆ÷£¬²¢½«ÆäÏìÓ¦·µ»Ø¸øä¯ÀÀÆ÷¡£
ÔÚ»á¼û·´ÏòÊðÀíʱ´ú£¬Äú¿ÉÒÔÔÚºó¶ËЧÀÍÆ÷µÄÈÕÖ¾ÎļþÖÐÉó²éÇëÇóµÄIPµØµãÊÇ·ñ׼ȷÏÔʾ¡£ÕâÊÇͨ¹ý¡°proxy_set_header X-Real-IP $remote_addr;¡±Ö¸ÁîʵÏֵġ£
½áÂÛ£º
ͨ¹ý±¾ÎĵÄÖ¸ÄÏ£¬ÄúÒѾѧ»áÁËÔõÑùÔÚLinuxÉÏÉèÖÃNginxΪ·´ÏòÊðÀíЧÀÍÆ÷¡£Í¨¹ý³ä·ÖʹÓÃNginxµÄ·´ÏòÊðÀí¹¦Ð§£¬ÄúµÄÓ¦ÓóÌÐò¿ÉÒÔ»ñµÃ¸ü¸ßµÄÐÔÄܺͿÉÀ©Õ¹ÐÔ¡£×£ÄúʹÓÃÓä¿ì£¡
ÒÔÉϾÍÊÇÔõÑùÔÚLinuxÉÏÉèÖ÷´ÏòÊðÀí£¨ÈçNginx£©µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡