Ò»ÕÅСͼ¿´¾¡ Nginx
nginx×°ÖÃ
Nginx×°ÖÃÍê³É£¬Ã»ÓÐsbinĿ¼
cd½øÈëµ½nginx-1.18.0Ŀ¼ÄÚÖ´ÐÐ
[root@centos7 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx[root@centos7 nginx-1.18.0]# make[root@centos7 nginx-1.18.0]# make install
µÇ¼ºó¸´ÖÆ
ps£º¨Cprefix=path ½ç˵һ¸öĿ¼£¬´æ·ÅЧÀÍÆ÷ÉϵÄÎļþ £¬Ò²¾ÍÊÇnginxµÄ×°ÖÃĿ¼£¬Ä¬ÈÏʹÓà /usr/local/nginx
Õâ²½Äã»á¿´µ½localĿ¼ÏÂÓÐÒ»¸ö¸úÄã×°ÖõÄnginx1.12.2ͬ¼¶µÄnginxĿ¼£¬ÄÚÀï¾ÍÓÐsbinĿ¼ÁË¡£
»ù´¡Ö¸Áî
listen£º¸ÃÖ¸ÁîÓÃÓÚÉèÖÃÍøÂç¼àÌý¡£
listen *:80 | *:8080 #¼àÌýËùÓÐ80¶Ë¿ÚºÍ8080¶Ë¿Úlisten IP_address:port #¼àÌýÖ¸¶¨µÄµØµãºÍ¶Ë¿ÚºÅlisten IP_address #¼àÌýÖ¸¶¨ipµØµãËùÓж˿Úlisten port #¼àÌý¸Ã¶Ë¿ÚµÄËùÓÐIPÅþÁ¬
µÇ¼ºó¸´ÖÆ
server_name£º¸ÃÖ¸ÁîÓÃÓÚÐéÄâÖ÷»úµÄÉèÖá£
a. »ùÓÚÃû³ÆµÄÐéÄâÖ÷»úÉèÖÃ
server_name name ...;
µÇ¼ºó¸´ÖÆ
b. »ùÓÚ IP µØµãµÄÐéÄâÖ÷»úÉèÖÃ
server_name 192.168.1.1
µÇ¼ºó¸´ÖÆ
location£º¸ÃÖ¸ÁîÓÃÓÚÆ¥Åä URL¡£
locationÖ¸ÁîµÄ×÷ÓÃÊÇƾ֤Óû§ÇëÇóµÄURIÀ´Ö´Ðвî±ðµÄÓ¦Óã¬Ò²¾ÍÊÇƾ֤Óû§ÇëÇóµÄÍøÕ¾URL¾ÙÐÐÆ¥Å䣬ƥÅäÀֳɼ´¾ÙÐÐÏà¹ØµÄ²Ù×÷¡£
Å£±Æ°¡£¡½Ó˽»î±Ø±¸µÄ N ¸ö¿ªÔ´ÏîÄ¿£¡¸ÏæÕä²Ø°É
µÇ¼ºó¸´ÖÆ
locationµÄÓï·¨
=¿ªÍ·ÌåÏÖ׼ȷƥÅä
Èç A ÖÐֻƥÅä¸ùĿ¼×îºóµÄÇëÇ󣬺óÃæ²»¿É´øÈκÎ×Ö·û´®¡£
^~ ¿ªÍ·ÌåÏÖuriÒÔij¸öͨÀý×Ö·û´®¿ªÍ·£¬²»ÊÇÕýÔòÆ¥Åä
~ ¿ªÍ·ÌåÏÖÇø·Ö¾ÞϸдµÄÕýÔòÆ¥Åä;
~* ¿ªÍ·ÌåÏÖ²»Çø·Ö¾ÞϸдµÄÕýÔòÆ¥Åä
/ ͨÓÃÆ¥Åä, ÈôÊÇûÓÐÆäËüÆ¥Åä,ÈκÎÇëÇ󶼻áÆ¥Åäµ½
LocationÕýÔò°¸Àý
#׼ȷƥÅä,/ºóÃæ²»¿É´øÈκÎ×Ö·û server { listen 80; server_name www.itmayiedu.com; #׼ȷƥÅä,×¢½âºóÃæ²»¿É´øÈκÎ×Ö·û location =/ { proxy_pass http://127.0.0.1:8080; index index.html index.htm; } } #Æ¥ÅäËùÓÐÒÔ/¿ªÍ·ÇëÇó server { listen 80; server_name www.itmayiedu.com; #Æ¥ÅäËùÓÐÒÔ/¿ªÍ·ÇëÇó location / { proxy_pass http://127.0.0.1:8080; index index.html index.htm; } } ### ÒÔ¿ªÍ·/itmayiedu_8080×èµ² ĬÈÏ¿ªÆô²»Çø·Ö¾Þϸд server { listen 80; server_name www.itmayiedu.com; ### ÒÔ¿ªÍ·/itmayiedu_8080 ×îÖÕÌøתµ½http://127.0.0.1:8080/; location /itmayiedu_8080/ { proxy_pass http://127.0.0.1:8080/; index index.html index.htm; } ### ÒÔ¿ªÍ·/itmayiedu_8080 ×îÖÕÌøתµ½http://127.0.0.1:8081/; location /itmayiedu_8081/ { proxy_pass http://127.0.0.1:8081/; index index.html index.htm; } } ### ¿ªÍ·Çø·Ö¾Þϸд
µÇ¼ºó¸´ÖÆ
proxy_pass£º¸ÃÖ¸ÁîÓÃÓÚÉèÖñ»ÊðÀíЧÀÍÆ÷µÄµØµã¡£¿ÉÒÔÊÇÖ÷»úÃû³Æ¡¢IPµØµã¼Ó¶Ë¿ÚºÅµÄÐÎʽ¡£
Óï·¨½á¹¹ÈçÏ£º
proxy_pass URL;
µÇ¼ºó¸´ÖÆ
index£º¸ÃÖ¸ÁîÓÃÓÚÉèÖÃÍøÕ¾µÄĬÈÏÊ×Ò³¡£
ÓòÃûÖض¨Ïò
server { listen 80 ; server_name mxiaoqi.top aaa.com; if ( $host = mxiaoqi.top ) #ÔöÌíÅжÏÌõ¼þ£¬µ±»á¼ûÓòÃûÊÇmxiaoqi.topµÄʱ¼ä { rewrite /(.*) http://aaa.com/$1 permanent; #°Ñmxiaoqi.top/ºóÃæµÄÄÚÈÝÖØÐÂдµ½aaa.com/ºóÃæÈôÊǺóÃæÓжà¶ÎÔòʹÓÃ$2¡¢$3ÒÔ´ËÀàÍÆ #permanentÊÇת·¢×´Ì¬Âë } index index.html index.htm index.php; root /data/wwwroot/mxiaoqi.top; }
µÇ¼ºó¸´ÖÆ
·´ÏòÊðÀí
ʹÓà nginx ·´ÏòÊðÀí www.123.com Ö±½ÓÌøתµ½127.0.0.1:8080
server { listen 80; server_name www.123.com; location / { proxy_pass http://127.0.0.1:8080; # ½Ó´ýÒ³Ã棬ƾ֤´Ó×óµ½ÓÒµÄ˳Ðò²éÕÒÒ³Ãæ index index.html index.htm index.jsp; } }
µÇ¼ºó¸´ÖÆ
¼àÌý80¶Ë¿Ú£¬»á¼ûÓòÃûΪwww.123.com£¬²»¼Ó¶Ë¿ÚºÅʱĬÒÔΪ80¶Ë¿Ú£¬¹Ê»á¼û¸ÃÓòÃûʱ»áÌøתµ½127.0.0.1:8080·¾¶ÉÏ¡£
ÏÞÁ÷ÉèÖÃ
©ͰËã·¨ÓëÁîÅÆÍ°Ëã·¨Çø±ð£ºÖ÷ÒªÇø±ðÔÚÓÚ¡°Â©Í°Ëã·¨¡±Äܹ»Ç¿ÐÐÏÞÖÆÊý¾ÝµÄ´«ÊäËÙÂÊ£¬
¶ø¡°ÁîÅÆÍ°Ëã·¨¡±ÔÚÄܹ»ÏÞÖÆÊý¾ÝµÄƽ¾ù´«ÊäËÙÂÊÍ⣬»¹ÔÊÐíijÖÖˮƽµÄÍ»·¢´«Êä¡£ÔÚ¡°ÁîÅÆÍ°Ëã·¨¡±ÖУ¬Ö»ÒªÁîÅÆÍ°Öб£´æÁîÅÆ£¬ÄÇô¾ÍÔÊÐíÍ»·¢µØ´«ÊäÊý¾ÝÖ±µ½µÖ´ïÓû§ÉèÖõÄÃÅÏÞ£¬Òò´ËËüÊʺÏÓÚ¾ßÓÐÍ»·¢ÌØÕ÷µÄÁ÷Á¿¡£
Nginx°´ÇëÇóËÙÂÊÏÞËÙÄ£¿éʹÓõÄÊÇ©ͰËã·¨£¬¼´Äܹ»Ç¿Ðаü¹ÜÇëÇóµÄʵʱ´¦Öóͷ£ËÙÂʲ»»áÁè¼ÝÉèÖõÄãÐÖµ¡£
limit_req_zone ÓÃÀ´ÏÞÖƵ¥Î»Ê±¼äÄÚµÄÇëÇóÊý£¬¼´ËÙÂÊÏÞÖÆ,½ÓÄɵÄ©ͰËã·¨ “leaky bucket”¡£
limit_req_conn ÓÃÀ´ÏÞÖÆͳһʱ¼äÅþÁ¬Êý£¬¼´²¢·¢ÏÞÖÆ¡£
limit_req_zone ²ÎÊýÉèÖÃ
Syntax: limit_req zone=name [burst=number] [nodelay]; Default: ¡ª Context: http, server, location
µÇ¼ºó¸´ÖÆ
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
µÚÒ»¸ö²ÎÊý£º$binary_remote_addrÌåÏÖͨ¹ýremote_addrÕâ¸ö±êʶÀ´×öÏÞÖÆ£¬¡°binary_¡±µÄÄ¿µÄÊÇËõдÄÚ´æÕ¼ÓÃÁ¿£¬ÊÇÏÞÖÆͳһ¿Í»§¶ËipµØµã¡£
µÚ¶þ¸ö²ÎÊý£ºzone=one:10mÌåÏÖÌìÉúÒ»¸ö¾ÞϸΪ10M£¬Ãû×ÖΪoneµÄÄÚ´æÇøÓò£¬ÓÃÀ´´æ´¢»á¼ûµÄƵ´ÎÐÅÏ¢¡£
µÚÈý¸ö²ÎÊý£ºrate=1r/sÌåÏÖÔÊÐíÏàͬ±êʶµÄ¿Í»§¶ËµÄ»á¼ûƵ´Î£¬ÕâÀïÏÞÖƵÄÊÇÿÃë1´Î£¬»¹¿ÉÒÔÓкñÈ30r/mµÄ¡£
limit_req zone=one burst=5 nodelay;
µÚÒ»¸ö²ÎÊý£ºzone=one ÉèÖÃʹÓÃÄĸöÉèÖÃÇøÓòÀ´×öÏÞÖÆ£¬ÓëÉÏÃælimit_req_zone ÀïµÄname¶ÔÓ¦¡£
µÚ¶þ¸ö²ÎÊý£ºburst=5£¬Öصã˵Ã÷Ò»ÏÂÕâ¸öÉèÖã¬burst±¬·¢µÄÒâ˼£¬Õâ¸öÉèÖõÄÒâ˼ÊÇÉèÖÃÒ»¸ö¾ÞϸΪ5µÄ»º³åÇøµ±Óдó×ÚÇëÇ󣨱¬·¢£©¹ýÀ´Ê±£¬Áè¼ÝÁË»á¼ûƵ´ÎÏÞÖƵÄÇëÇó¿ÉÒÔÏȷŵ½Õâ¸ö»º³åÇøÄÚ¡£
µÚÈý¸ö²ÎÊý£ºnodelay£¬ÈôÊÇÉèÖã¬Áè¼Ý»á¼ûƵ´Î²¢ÇÒ»º³åÇøÒ²ÂúÁ˵Äʱ¼ä¾Í»áÖ±½Ó·µ»Ø503£¬ÈôÊÇûÓÐÉèÖã¬ÔòËùÓÐÇëÇó»áÆÚ´ýÅŶӡ£
ngx_http_limit_conn_module ²ÎÊýÉèÖÃ
Õâ¸öÄ£¿éÓÃÀ´ÏÞÖƵ¥¸öIPµÄÇëÇóÊý¡£²¢·ÇËùÓеÄÅþÁ¬¶¼±»¼ÆÊý¡£Ö»ÓÐÔÚЧÀÍÆ÷´¦Öóͷ£ÁËÇëÇó²¢ÇÒÒѾ¶ÁÈ¡ÁËÕû¸öÇëÇóͷʱ£¬ÅþÁ¬²Å±»¼ÆÊý¡£
Syntax: limit_conn zone number; Default: ¡ª Context: http, server, location limit_conn_zone $binary_remote_addr zone=addr:10m; server { location /download/ { limit_conn addr 1; }
µÇ¼ºó¸´ÖÆ
Ò»´ÎÖ»ÔÊÐíÿ¸öIPµØµãÒ»¸öÅþÁ¬¡£
¸ºÔØƽºâ
#user nobody; worker_processes 1; error_log logs/error.log;# ¿ªÆôÈÕÖ¾ pid logs/nginx.pid; ... upstream lyf { server 192.168.37.220:8001; # 3¸ötomcatЧÀÍ server 192.168.37.220:8002; server 192.168.37.220:8003; } server { listen 80; server_name 192.168.37.220;# ¼àÌýip location / { proxy_pass http://lyf; # ÉèÖÃÊðÀí index index.html index.htm; } }
µÇ¼ºó¸´ÖÆ
keepalive ³¤ÅþÁ¬Ìá¸ßÍÌÍÂÁ¿
keepalived £ºÉèÖó¤ÅþÁ¬´¦Öóͷ£µÄÊýÄ¿
proxy_http_version £ºÉèÖó¤ÅþÁ¬http°æ±¾Îª1.1
proxy_set_header £ºÉ¨³ýconnection header ÐÅÏ¢
upstream tomcats { # server 192.168.1.173:8080 max_fails=2 fail_timeout=1s; server 192.168.1.190:8080; # server 192.168.1.174:8080 weight=1; # server 192.168.1.175:8080 weight=1; keepalive 32; } server { listen 80; server_name www.tomcats.com; location / { proxy_pass http://tomcats; proxy_http_version 1.1; proxy_set_header Connection ""; } }
µÇ¼ºó¸´ÖÆ
ÊÂÇé·½·¨
ÂÖѯ·½·¨ÊÇNginx¸ºÔØĬÈϵķ½·¨
ȨÖØ·½·¨ Ö¸¶¨Ã¿¸öЧÀ͵ÄȨÖرÈÀý£¬weightºÍ»á¼û±ÈÂʳÉÕý±È
upstream dalaoyang-server { server localhost:10001 weight=1; server localhost:10002 weight=2; }
µÇ¼ºó¸´ÖÆ
iphash
ÿ¸öÇëÇó¶¼Æ¾Ö¤»á¼ûipµÄhashЧ¹û·ÖÅÉ£¬¾ÓÉÕâÑùµÄ´¦Öóͷ££¬Ã¿¸ö·Ã¿ÍÀο¿»á¼ûÒ»¸öºó¶ËЧÀÍ£¬ÈçÏÂÉèÖã¨ip_hash¿ÉÒÔºÍweightÅäºÏʹÓã©¡£
upstream dalaoyang-server { ip_hash; server localhost:10001 weight=1; server localhost:10002 weight=2; }
µÇ¼ºó¸´ÖÆ
×îÉÙÅþÁ¬
½«ÇëÇó·ÖÅɵ½ÅþÁ¬Êý×îÉÙµÄЧÀÍÉÏ¡£
upstream dalaoyang-server { least_conn; server localhost:10001 weight=1; server localhost:10002 weight=2; }
µÇ¼ºó¸´ÖÆ
fair
°´ºó¶ËЧÀÍÆ÷µÄÏìӦʱ¼äÀ´·ÖÅÉÇëÇó£¬ÏìӦʱ¼ä¶ÌµÄÓÅÏÈ·ÖÅÉ¡£
upstream dalaoyang-server { server localhost:10001 weight=1; server localhost:10002 weight=2; fair; }
µÇ¼ºó¸´ÖÆ
Consul+upsync+Nginx ʵÏÖÎÞÐèraload¶¯Ì¬¸ºÔØƽºâ https://www.cnblogs.com/a1304908180/p/10697278.html
¹Å°åµÄ¸ºÔØƽºâ£¬ÈôÊÇUpstream²ÎÊý±¬·¢×ª±ä£¬Ã¿´Î¶¼ÐèÒªÖØмÓÔØnginx.confÎļþ,
Òò´ËÀ©Õ¹ÐÔ²»ÊǺܸߣ¬ÒÔÊÇÎÒÃÇ¿ÉÒÔ½ÓÄɶ¯Ì¬¸ºÔØƽºâ£¬ÊµÏÖUpstream¿ÉÉèÖû¯¡¢¶¯Ì¬»¯£¬ÎÞÐèÈ˹¤ÖØмÓÔØnginx.conf¡£
LVS+Keepalived+Nginx+Tomcat´î½¨¸ß¿ÉÓÃË«»úÖ÷´ÓÈȱ¸¼¯Èº
https://blog.csdn.net/dsen726/article/details/89519013
ÐèÒªÃ÷È·µÄÊÇ£º
NginxÁ½Ì¨ÊÇÖ÷±¸¹Øϵ£¬Ö»ÓÐһ̨ÔÚÊÂÇé¡£ºóÃæµÄtomcatÊǼ¯Èº£¬Í¬Ê±ÊÆÇéµÄ¡£
keepalivedÊÇͬʱװÖÃÔÚÁ½Ì¨NginxÉϵģ¬²»¹ýÎļþÉèÖ÷×ÆçÑù
ÕâÀïµÄË«»úÈȱ¸ÊÇÖ¸LVS£¬NginxÔòÊǼ¯Èº
keepalived
¿µ½¡¼ì²éºÍʧ°ÜÇл»ÊÇkeepalivedµÄÁ½´ó½¹µã¹¦Ð§¡£ËùνµÄ¿µ½¡¼ì²é£¬¾ÍÊǽÓÄÉtcpÈý´ÎÎÕÊÖ£¬icmpÇëÇó£¬httpÇëÇó£¬udp echoÇëÇóµÈ·½·¨¶Ô¸ºÔØƽºâÆ÷ºóÃæµÄÏÖʵµÄЧÀÍÆ÷(ͨ³£ÊdzÐÔØÕæʵӪҵµÄЧÀÍÆ÷)¾ÙÐб£»î£»¶øʧ°ÜÇл»Ö÷ÒªÊÇÓ¦ÓÃÓÚÉèÖÃÁËÖ÷±¸Ä£Ê½µÄ¸ºÔØƽºâÆ÷£¬Ê¹ÓÃVRRPά³ÖÖ÷±¸¸ºÔØƽºâÆ÷µÄÐÄÌø£¬µ±Ö÷¸ºÔØƽºâÆ÷·ºÆðÎÊÌâʱ£¬Óɱ¸¸ºÔØƽºâÆ÷³ÐÔضÔÓ¦µÄÓªÒµ£¬´Ó¶øÔÚ×î´óÏÞ¶ÈÉÏïÔÌÁ÷Á¿Ëðʧ£¬²¢ÌṩЧÀ͵ÄÎȹÌÐÔ¡£ÁíÍ⣬ËÑË÷¹«ÖÚºÅLinux¾Í¸ÃÕâÑùѧºǫ́»Ø¸´¡°ºï×Ó¡±£¬»ñÈ¡Ò»·Ý¾ªÏ²Àñ°ü¡£
LVSÊÇLinux Virtual ServerµÄ¼òд£¬Òâ¼´LinuxÐéÄâЧÀÍÆ÷£¬ÊÇÒ»¸öÐéÄâµÄЧÀÍÆ÷¼¯ÈºÏµÍ³¡£lvsÏÖÔÚÊǼ¯³ÉÔÚLinuxÄڵġ£
ΪʲôҪLVS+Nginx£¿
Ò»¡¢ngix£¨Ó¦Óòã ÍøÂçÆ߲㸺ÔØƽºâ£©
1¡¢Ò첽ת·¢£¬ÇëÇóÊý¾ÝºÍÏìÓ¦Êý¾Ý¶¼Òª¾ÓÉngix£¬ngixºÍ¿Í»§¶Ë½¨ÉèÅþÁ¬
2¡¢ÂÖѯËùÓеÄtomcatЧÀÍÆ÷£¬°ü¹ÜÇëÇóÀֳɻòÕß×îºóһ̨tomcatЧÀÍÆ÷Ò²ÇëÇóʧ°Ü
¶þ¡¢lvs(ÍøÂç²ã ÍøÂçËIJ㸺ÔØƽºâ)
1¡¢Í¬²½×ª·¢½ÓÊÜÇëÇóÊý¾Ý£¬lvsת·¢µ½Ð§ÀÍÆ÷£¬Ð§ÀÍÆ÷Ö±½ÓºÍ¿Í»§¶Ë½¨ÉèÅþÁ¬
nginxÒªÔâÊÜËùÓеÄÁ÷Á¿£¬µ±Ò»Ì¨nigxÔâÊܲ»ÁË£¬¾ÍÐèÒª´î½¨ngix¼¯Èº¡£ngix+ngix Íâ²ãµÄngixÕÕ¾ÉÒªÔâÊÜËùÓÐÁ÷Á¿¡£
lvs+ngix:lvsͬ²½×ª·¢²»»á½ÓÊÜÏìÓ¦Êý¾Ý£¬LVS½ÓÄÉDRģʽʱ²»±ØÏìӦЧÀÍÆ÷·µ»ØµÄÄÚÈÝ¡££¨Í¨³£ÇëÇóÊý¾ÝÊǽÏÁ¿Ð¡µÄ£¬ÏìÓ¦Êý¾Ý»á½ÏÁ¿´ó£©
¾²Ì¬×ÊÔ´ÉèÖÃ
location ~ .*.(jpg|gif|png)$ { gzip on; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript image/jpeg image/gif image/png; root /usr/share/nginx/images; } location ~ .*.(txt|xml)$ { gzip on; gzip_http_version 1.1; gzip_comp_level 1; gzip_types text/plain application/javascript image/jpeg image/gif image/png; root /usr/share/nginx/code; } location ~ ^/download { gzip_static on; tcp_nopush on; root /opt/app/code; }
µÇ¼ºó¸´ÖÆ
sendfile on | off£¬Îļþ¶ÁÈ¡ÉèÖÃ
ĬÈÏsendfileÊǹرյģ¬¿ÉÒÔÉèÖÃÔÚhttp,server,location,if in locationÖÐ
tcp_nopush on | off,¶à¸ö°üÕûºÏ
ĬÈÏÊǹرÕ״̬£¬¿ÉÒÔÔÚhttp,server,locationÖÐÉèÖã¬ËüµÄ×÷ÓÃÊÇÔÚsendfile¿ªÆôµÄÇéÐÎÏ£¬Ìá¸ßÍøÂç°üµÄ´«ÊäЧÂÊ¡£Ê²Ã´Òâ˼ÄØ£¬¼ÙÉèЧÀͶËÊÕµ½ÇëÇó£¬ÐèÒªÍÆËÍ10¸ö°ü£¬ÎªÁËÌá¸ß´«ÊäЧÂÊ£¬Õâ10¸ö°ü²»»áÒ»¸öÒ»¸ö·µ»Ø¸ø¿Í»§¶Ë£¬¶øÊǽ«10¸ö°üÔܹ»Á˺óÒ»Æ𷵻ػØÈ¥¡£
tcp_nodelay on | off,ÍøÂç°üµÄʵʱÐÔ´«Êä
ĬÈÏ¿ªÆô£¬¿ÉÒÔÔÚhttp,server,locationÖÐÉèÖã¬ËüµÄ×÷ÓÃÊÇÔÚkeepaliveÁ´½ÓÏ£¬Ìá¸ßÍøÂç°üµÄ´«ÊäʵʱÐÔ¡£
gzip on | off£¬Ñ¹Ëõ
ĬÈÏÊǹرÕ״̬£¬¿ÉÒÔÔÚhttp,server,location,if in locationÖÐÉèÖã¬×÷ÓÃÊÇѹËõ´«Êä¡£Ò»Ñùƽ³£À´Ëµä¯ÀÀÆ÷ÊÇ¿ÉÒÔ¶ÔѹËõºóµÄÄÚÈݾÙÐнâѹµÄ¡£
gzip_comp_level level;ѹËõ¼¶±ð
ĬÈϵÄѹËõ¼¶±ðÊÇ1£¬¿ÉÒÔÔÚhttp,server,locationÖÐÉèÖ㬼¶±ðÉèÖõÄÔ½¸ß£¬Ñ¹ËõµÄÔ½ºÃ£¬¿ÉÊÇѹËõ»áãýÃðЧÀͶ˵ÄÅÌËã×ÊÔ´£¬ÒÔÊÇÒª¿ØÖƺÃѹËõ¼¶±ð
gzip_http_version 1.0 | 1.1£¬Ñ¹Ëõ¶ÔhttpÐÒéµÄÖ§³Ö
ĬÈ϶ÔHTTP/1.1ÐÒéµÄÇëÇó²Å»á¾ÙÐÐgzipѹËõ,¿ÉÒÔÉèÖÃÔÚhttp,server,locationÖÐÉèÖᣵ±Óû§ÏëÒª¶ÁÈ¡Ò»¸ö1.htmlÎļþ£¬Ê×ÏÈ»áÔÚĿ¼ÖÐÕÒÑ°1.html.gzÊÇ·ñ±£´æ£¬ÒÔÊÇÕâ¾Íµ¼ÖÂÁË´ÅÅÌ×ÊÔ´µÄÆÌÕÅ£¬±ØÐèÒª´æ´¢Á½·ÝÎļþ¡£
###¾²Ì¬×ÊÔ´»á¼û server { listen 80; server_name static.itmayiedu.com; location /static/imgs { root F:/; index index.html index.htm; } } ###¶¯Ì¬×ÊÔ´»á¼û server { listen 80; server_name www.itmayiedu.com; location / { proxy_pass http://127.0.0.1:8080; index index.html index.htm; } }
µÇ¼ºó¸´ÖÆ
¿çÓòÉèÖÃ
¿çÓò¾ÍÊÇÔÚÔÕ¾µã»á¼ûÓòÃû²î±ðµÄÆäËûÕ¾µã£¬Í¬Ô´Õ½ÂÔ»á×èÖ¹Ò»¸öÓòµÄjavascript¾ç±¾ºÍÁíÍâÒ»¸öÓòµÄÄÚÈݾÙÐн»»¥¡£ËùνͬԴ£¨¼´Ö¸ÔÚͳһ¸öÓò£©¾ÍÊÇÁ½¸öÒ³Ãæ¾ßÓÐÏàͬµÄÐÒ飨protocol£©£¬Ö÷»ú£¨host£©ºÍ¶Ë¿ÚºÅ£¨port£©¡£
CORS ÊÇ¿çÓò×ÊÔ´·ÖÏí£¨Cross-Origin Resource Sharing£©µÄËõд¡£ËüÊÇ W3C ±ê×¼£¬ÊôÓÚ¿çÔ´ AJAX ÇëÇóµÄ»ù´¡½â¾öÒªÁì¡£
1¡¢Í¨Ë׿çÓòÇëÇó£ºÖ»ÐèЧÀÍÆ÷¶ËÉèÖÃAccess-Control-Allow-Origin
2¡¢´øcookie¿çÓòÇëÇó£ºÇ°ºó¶Ë¶¼ÐèÒª¾ÙÐÐÉèÖÃ
#ÔÊÐí¿çÓòÇëÇóµÄÓò£¬*´ú±íËùÓÐ add_header 'Access-Control-Allow-Origin' *; #ÔÊÐí´øÉÏcookieÇëÇó add_header 'Access-Control-Allow-Credentials' 'true'; #ÔÊÐíÇëÇóµÄÒªÁ죬ºÃ±È GET/POST/PUT/DELETE add_header 'Access-Control-Allow-Methods' *; #ÔÊÐíÇëÇóµÄheader add_header 'Access-Control-Allow-Headers' *;
µÇ¼ºó¸´ÖÆ
·ÀµÁÁ´
#¶ÔÔ´Õ¾µãÑéÖ¤ valid_referers *.imooc.com; #²»·¨ÒýÈë»á½øÈëÏ·½ÅÐ¶Ï if ($invalid_referer) { return 404; }
µÇ¼ºó¸´ÖÆ
source: https://www.yuque.com/molizhuzhu/thrgrk/rtslmc
ÒÔÉϾÍÊÇÒ»ÕÅСͼ¿´¾¡ NginxµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡