av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

nginx?反向代理負(fù)載均衡策略配置SSL訪問(wèn)匹配規(guī)則優(yōu)先級(jí)

瀏覽:5日期:2023-08-03 20:13:38
目錄引言解決跨域問(wèn)題Nginx配置文件upstream(負(fù)載均衡服務(wù)器設(shè)置)反向代理實(shí)例---》負(fù)載均衡實(shí)例輪詢策略權(quán)重策略ip hash策略其他負(fù)載均衡策略重定向rewritenginx配置Vue前端發(fā)布history模式訪問(wèn)nginx配置靜態(tài)資源文件映射實(shí)際線上Nginx配置文件參考Nginx調(diào)優(yōu)Nginx匹配規(guī)則說(shuō)明以及匹配的優(yōu)先級(jí)引言

首先Nginx能做反向代理【關(guān)于反向代理和正向代理此處不做說(shuō)明了,感興趣的小伙伴自行谷歌】;比方說(shuō),我想在本地使用 www.google.com 的域名去訪問(wèn)www.taobao.com。那么這個(gè)時(shí)候我們就可以通過(guò)nginx去實(shí)現(xiàn)

再者Nginx能實(shí)現(xiàn)負(fù)載均衡,就是說(shuō)應(yīng)用部署在不同的服務(wù)器上,但是通過(guò)統(tǒng)一的域名進(jìn)入,nginx則對(duì)請(qǐng)求進(jìn)行分發(fā),將請(qǐng)求分發(fā)到不同的服務(wù)器上去處理,這樣就可以有效的減輕了單臺(tái)服務(wù)器的壓力,解決單點(diǎn)故障,在上面這兩種情況下,nginx服務(wù)器的作用都只是作為分發(fā)服務(wù)器,真正的內(nèi)容,我們可以放在其他的服務(wù)器上,這樣來(lái),還能起到一層安全隔壁的作用,nginx可以作為隔離層

解決跨域問(wèn)題

同源:URL由協(xié)議、域名、端口和路徑組成,如果兩個(gè)URL的協(xié)議、域名和端口相同,則表示他們同源

瀏覽器的同源策略:瀏覽器的同源策略,限制了來(lái)自不同源的"document"或腳本,對(duì)當(dāng)前"document"讀取或設(shè)置某些屬性。

從一個(gè)域上加載的腳本不允許訪問(wèn)另外一個(gè)域的文檔屬性(同源表示:協(xié)議、域名和端口相同)

例如:因?yàn)閚ginx和tomcat不能共用同一端口,url一樣,端口不同,這樣就會(huì)有跨域問(wèn)題

Nginx配置文件

配置文件主要由四部分組成:

main(全區(qū)設(shè)置)

server(主機(jī)配置)

http(控制著nginx http處理的所有核心特性)

location(URL匹配特定位置設(shè)置)。

upstream(負(fù)載均衡服務(wù)器設(shè)置)#Nginx的worker進(jìn)程運(yùn)行用戶以及用戶組#user nobody;#Nginx開(kāi)啟的進(jìn)程數(shù)worker_processes 1;#定義全局錯(cuò)誤日志定義類型,[debug|info|notice|warn|crit]#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#指定進(jìn)程ID存儲(chǔ)文件位置#pidlogs/nginx.pid;#事件配置events { #use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; #epoll模型是Linux內(nèi)核中的高性能網(wǎng)絡(luò)I/O模型,如果在mac上面,就用kqueue模型。 use kqueue; #每個(gè)進(jìn)程可以處理的最大連接數(shù),理論上每臺(tái)nginx服務(wù)器的最大連接數(shù)為worker_processes*worker_connections。理論值:worker_rlimit_nofile/worker_processes worker_connections 1024;}#http參數(shù)http { #文件擴(kuò)展名與文件類型映射表 include mime.types; #默認(rèn)文件類型 default_type application/octet-stream; #日志相關(guān)定義 #log_format main '$remote_addr - $remote_user [$time_local] '$request' ' # '$status $body_bytes_sent '$http_referer' ' # ''$http_user_agent' '$http_x_forwarded_for''; #連接日志的路徑,指定的日志格式放在最后。 #access_log logs/access.log main; #開(kāi)啟高效傳輸模式 sendfileon; #防止網(wǎng)絡(luò)阻塞 #tcp_nopush on; #客戶端連接超時(shí)時(shí)間,單位是秒 #keepalive_timeout 0; keepalive_timeout 65; #開(kāi)啟gzip壓縮輸出 #gzip on; #虛擬主機(jī)基本設(shè)置 server {#監(jiān)聽(tīng)的端口號(hào)listen 80;#訪問(wèn)域名server_name localhost;#編碼格式,如果網(wǎng)頁(yè)格式與當(dāng)前配置的不同的話將會(huì)被自動(dòng)轉(zhuǎn)碼#charset koi8-r;#虛擬主機(jī)訪問(wèn)日志定義#access_log logs/host.access.log main;#對(duì)URL進(jìn)行匹配location / { #訪問(wèn)路徑,可相對(duì)也可絕對(duì)路徑 root html; #首頁(yè)文件,匹配順序按照配置順序匹配 index index.html index.htm;}#錯(cuò)誤信息返回頁(yè)面#error_page 404 /404.html;# redirect server error pages to the static page /50x.htmlerror_page 500 502 503 504 /50x.html;location = /50x.html { root html;}#訪問(wèn)URL以.php結(jié)尾則自動(dòng)轉(zhuǎn)交給127.0.0.1# proxy the PHP scripts to Apache listening on 127.0.0.1:80## location ~ \.php$ {# proxy_pass http://127.0.0.1;# }# php腳本請(qǐng)求全部轉(zhuǎn)發(fā)給FastCGI處理# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# includefastcgi_params;#}# 禁止訪問(wèn).ht頁(yè)面# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#} } #第二個(gè)虛擬主機(jī)配置 # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { #root html; #index index.html index.htm; # } #} #HTTPS虛擬主機(jī)定義 # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { #root html; #index index.html index.htm; # } #} include servers/*;}反向代理實(shí)例---》

假設(shè)現(xiàn)在代理 www.baidu.com

server { # 監(jiān)聽(tīng)80端口 listen 80; server_name localhost; # individual nginx logs for this web vhost access_log /tmp/access.log; error_log /tmp/error.log ; # 配置代理 location / {proxy_pass http://www.baidu.com; }負(fù)載均衡實(shí)例

(下面主要驗(yàn)證最常用的三種負(fù)載策略。虛擬主機(jī)配置)

server { #監(jiān)聽(tīng)80端口 listen 80; server_name localhost; # individual nginx logs for this web vhost access_log /tmp/access.log; error_log /tmp/error.log ; location / {# 負(fù)載均衡策略# 輪詢 # proxy_pass http://polling_strategy;# weight權(quán)重# proxy_pass http://weight_strategy;# ip_hash# proxy_pass http://ip_hash_strategy;# fair# proxy_pass http://fair_strategy;# url_hash# proxy_pass http://url_hash_strategy;# 重定向# rewrite ^ http://localhost:8080; }輪詢策略# 1、輪詢(默認(rèn))# 每個(gè)請(qǐng)求按時(shí)間順序逐一分配到不同的后端服務(wù)器,如果后端服務(wù)器down掉,能自動(dòng)剔除。 upstream polling_strategy { server www.net:8080; # 應(yīng)用服務(wù)器1 server www.net:8081; # 應(yīng)用服務(wù)器2} 測(cè)試結(jié)果(通過(guò)端口號(hào)來(lái)區(qū)分當(dāng)前訪問(wèn)):8081:hello8080:hello8081:hello8080:hello權(quán)重策略#2、指定權(quán)重# 指定輪詢幾率,weight和訪問(wèn)比率成正比,用于后端服務(wù)器性能不均的情況。 upstream weight_strategy { server glmapper.net:8080 weight=1; # 應(yīng)用服務(wù)器1 server glmapper.net:8081 weight=9; # 應(yīng)用服務(wù)器2}測(cè)試結(jié)果:總訪問(wèn)次數(shù)15次,根據(jù)上面的權(quán)重配置,兩臺(tái)機(jī)器的訪問(wèn)比重:2:13ip hash策略

iphash 算法: ip是基本的點(diǎn)分十進(jìn)制,將ip的前三個(gè)端作為參數(shù)加入hash函數(shù)。這樣做的目的是保證ip地址前三位相同的用戶經(jīng)過(guò)hash計(jì)算將分配到相同的后端server。作者的這個(gè)考慮是極為可取的,因此ip地址前三位相同通常意味著來(lái)著同一個(gè)局域網(wǎng)或者相鄰區(qū)域,使用相同的后端服務(wù)讓nginx在一定程度上更具有一致性

假設(shè)5臺(tái)機(jī)器均在同一個(gè)局域網(wǎng)內(nèi)【192.168.0.X】測(cè)試時(shí)發(fā)現(xiàn)5臺(tái)機(jī)器每次都路由到了同一個(gè)服務(wù)器上,一開(kāi)始以為是配置問(wèn)題,但是排查之后也排除了這個(gè)可能性。最后考慮到可能是對(duì)于同網(wǎng)段的ip做了特殊處理,驗(yàn)證之后確認(rèn)了猜測(cè)

#3、IP綁定 ip_hash#每個(gè)請(qǐng)求按訪問(wèn)ip的hash結(jié)果分配,這樣每個(gè)訪客固定訪問(wèn)一個(gè)后端服務(wù)器,#可以解決session的問(wèn)題;在不考慮引入分布式session的情況下,#原生HttpSession只對(duì)當(dāng)前servlet容器的上下文環(huán)境有效upstream ip_hash_strategy { ip_hash; server glmapper.net:8080; # 應(yīng)用服務(wù)器1 server glmapper.net:8081; # 應(yīng)用服務(wù)器2} 其他負(fù)載均衡策略#4、fair(第三方)#按后端服務(wù)器的響應(yīng)時(shí)間來(lái)分配請(qǐng)求,響應(yīng)時(shí)間短的優(yōu)先分配。 upstream fair_strategy { server glmapper.net:8080; # 應(yīng)用服務(wù)器1 server glmapper.net:8081; # 應(yīng)用服務(wù)器2 fair; } #5、url_hash(第三方)#按訪問(wèn)url的hash結(jié)果來(lái)分配請(qǐng)求,使每個(gè)url定向到同一個(gè)后端服務(wù)器,#后端服務(wù)器為緩存時(shí)比較有效。 upstream url_hash_strategy { server glmapper.net:8080; # 應(yīng)用服務(wù)器1 server glmapper.net:8081; # 應(yīng)用服務(wù)器2 hash $request_uri; hash_method crc32; } 重定向rewrite

驗(yàn)證思路:本地使用localhost:80端口進(jìn)行訪問(wèn),根據(jù)nginx的配置,如果重定向沒(méi)有生效,則最后會(huì)停留在當(dāng)前l(fā)ocalhost:80這個(gè)路徑,瀏覽器中的地址欄地址不會(huì)發(fā)生改變;如果生效了則地址欄地址變?yōu)閘ocalhost:8080;

通過(guò)驗(yàn)證,滿足預(yù)期!

location / { #重定向 #rewrite ^ http://localhost:8080;}nginx配置Vue前端發(fā)布history模式訪問(wèn)# nginx配置history模式訪問(wèn):server {listen 900;server_name localhost; error_page 500 502 503 504 /50x.html; location / { root C:/Users/lenovo/Desktop/前端代碼/dist; index /index.html; try_files $uri $uri/ /index.html; } }nginx配置靜態(tài)資源文件映射##靜態(tài)資源文件:server{ listen 9998; server_name localhost;location /file { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';# 文件夾路徑 alias C:/Users/lenovo/Desktop/TestRes/; autoindex on; autoindex_exact_size on; autoindex_localtime on; } }實(shí)際線上Nginx配置文件參考user root;#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pidlogs/nginx.pid;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] '$request' ' # '$status $body_bytes_sent '$http_referer' ' # ''$http_user_agent' '$http_x_forwarded_for''; #access_log logs/access.log main; sendfileon; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; client_max_body_size 4096m; server {listen 1000;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;# 配置前端Vue的dist包發(fā)布 location / { root /usr/local/vue/dist; index index.html index.htm; try_files $uri $uri/ /index.html;}# 轉(zhuǎn)發(fā)后端Nginx請(qǐng)求 location /service/ { proxy_pass http://localhost:3389/;}# 配置文件路徑映射 location /files { alias /usr/local/resource/; autoindex on;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html { root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# includefastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#} } #以下屬性中,以ssl開(kāi)頭的屬性表示與證書(shū)配置有關(guān)。 server {listen 6666 ssl;# listen 443 ssl;#配置HTTPS的默認(rèn)訪問(wèn)端口為443。#如果未在此處配置HTTPS的默認(rèn)訪問(wèn)端口,可能會(huì)造成Nginx無(wú)法啟動(dòng)。#如果您使用Nginx 1.15.0及以上版本,請(qǐng)使用listen 443 ssl代替listen 443和ssl onserver_name www.test.cn; #需要將www.test.cn替換成證書(shū)綁定的域名。root html;index index.html index.htm;# cert為當(dāng)前路徑的文件夾ssl_certificate cert/www.test.cn.pem; #需要將www.test.cn.pem替換成已上傳的證書(shū)文件的名稱。ssl_certificate_key cert/www.test.cn.key; #需要將www.test.cn.key替換成已上傳的證書(shū)私鑰文件的名稱。ssl_session_timeout 5m;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;#表示使用的加密套件的類型。ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #表示使用的TLS協(xié)議的類型。ssl_prefer_server_ciphers on;location /base { root html; #站點(diǎn)目錄。 index index.html index.htm; }location / { root /usr/local/vue/dist; index index.html index.htm; try_files $uri $uri/ /index.html;} location /service/ { proxy_pass http://localhost:3389/; } location /files { alias /usr/local/static/; autoindex on; } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { #root html; #index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { #root html; #index index.html index.htm; # } #}}

后臺(tái)服務(wù)訪問(wèn)代理配置

#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pidlogs/nginx.pid;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] '$request' ' # '$status $body_bytes_sent '$http_referer' ' # ''$http_user_agent' '$http_x_forwarded_for''; #access_log logs/access.log main; sendfileon; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server {listen 8988;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {client_max_body_size 100m;proxy_pass http://127.0.0.1/mobile/;proxy_redirect off;proxy_read_timeout 3600;proxy_send_timeout 3600;proxy_buffer_size 128k;proxy_buffers 32 32k;proxy_busy_buffers_size 128k;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Real-IP $remote_addr;proxy_set_header Host $host:$server_port;}location /etc {client_max_body_size 100m;proxy_pass http://127.0.0.1/api/notice;proxy_redirect off;proxy_read_timeout 3600;proxy_send_timeout 3600;proxy_buffer_size 128k;proxy_buffers 32 32k;proxy_busy_buffers_size 128k;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Real-IP $remote_addr;proxy_set_header Host $host:$server_port;}location /fileResult { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; alias /root/fileResult ; autoindex on; autoindex_exact_size on; autoindex_localtime on; } location /fileLocal { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';# 文件路徑 alias /root/local; autoindex on; autoindex_exact_size on; autoindex_localtime on; } #error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html { root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# includefastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { #root html; #index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { #root html; #index index.html index.htm; # } #}}Nginx調(diào)優(yōu)增加工作線程數(shù)和并發(fā)連接數(shù)[root@localhost /]# vi /etc/nginx/nginx.confworker_processes 4; # 一般CPU 是幾核就設(shè)置為幾 也可以設(shè)置成autoevents { worker_connections 10240; # 每個(gè)進(jìn)程打開(kāi)的最大連接數(shù),包含了 Nginx 與客戶端和 Nginx 與 upstream 之間的連接 multi_accept on; # 可以一次建立多個(gè)連接 use epoll; #epoll這種網(wǎng)絡(luò)模型}查看nginx 語(yǔ)法是否正確[root@localhost /]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is ok啟用長(zhǎng)連接[root@localhost /]# vi /etc/nginx/nginx.conf配置反向代理upstream server_pool{ server localhost:8080 weight=1 max_fails=2 fail_timeout=30s; server localhost:8081 weight=1 max_fails=2 fail_timeout=30s; keepalive 300; # 300個(gè)長(zhǎng)連接 提高效率}配置反向代理服務(wù)location / { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_pass http://server_pool; #所有請(qǐng)求都代理給server_pool}配置壓縮gzip on;gzip_http_version 1.1;gzip_disable 'MSIE [1-6].(?!.*SV1)';gzip_proxied any;gzip_types text/plain text/css application/javascript application/x-javascript application/json application/xml application/vnd.ms-fontobject application/x-font-ttf application/svg+xml application/x-icon;gzip_vary on;gzip_static on;操作系統(tǒng)優(yōu)化 配置文件/etc/sysctl.confsysctl -w net.ipv4.tcp_syncookies=1 # 防止一個(gè)套接字在有過(guò)多試圖連接到時(shí)引起過(guò)載sysctl -w net.core.somaxconn=1024 # 默認(rèn)128,操作系統(tǒng)連接隊(duì)列sysctl -w net.ipv4.tcp_fin_timeout=10 # timewait 的超時(shí)時(shí)間sysctl -w net.ipv4.tcp_tw_reuse=1 # os 直接使用 timewait的連接sysctl -w net.ipv4.tcp_tw_recycle=0 # 回收禁用 /etc/security/limits.conf hard nofile 204800 soft nofile 204800 soft core unlimited soft stack 204800其它優(yōu)化sendfile on; # 減少文件在應(yīng)用和內(nèi)核之間拷貝tcp_nopush on; # 當(dāng)數(shù)據(jù)包達(dá)到一定大小再發(fā)送tcp_nodelay off; # 有數(shù)據(jù)隨時(shí)發(fā)送Nginx匹配規(guī)則說(shuō)明以及匹配的優(yōu)先級(jí)location 匹配規(guī)則語(yǔ)法規(guī)則 location [=|~|~*|^~] /uri/ { … }模式 含義location = /uri = 表示精確匹配,只有完全匹配上才能生效location ^~ /uri ^~ 開(kāi)頭對(duì)URL路徑進(jìn)行前綴匹配,并且在正則之前。location ~ pattern 開(kāi)頭表示區(qū)分大小寫(xiě)的正則匹配location ~* pattern 開(kāi)頭表示不區(qū)分大小寫(xiě)的正則匹配location /uri 不帶任何修飾符,也表示前綴匹配,但是在正則匹配之后location / 通用匹配,任何未匹配到其它location的請(qǐng)求都會(huì)匹配到,相當(dāng)于switch中的default前綴匹配時(shí),Nginx 不對(duì) url 做編碼,因此請(qǐng)求為 /static/20%/aa,可以被規(guī)則 ^~ /static/ /aa 匹配到(注意是空格)多個(gè) location 配置的情況下匹配順序?yàn)椋▍⒖假Y料而來(lái)): 首先精確匹配 = 其次前綴匹配 ^~ 其次是按文件中順序的正則匹配 然后匹配不帶任何修飾的前綴匹配。 最后是交給 / 通用匹配 當(dāng)有匹配成功時(shí)候,停止匹配,按當(dāng)前匹配規(guī)則處理請(qǐng)求注意:前綴匹配,如果有包含關(guān)系時(shí),按最大匹配原則進(jìn)行匹配。比如在前綴匹配:location /dir01 與location /dir01/dir02,如有請(qǐng)求 http://localhost/dir01/dir02/file 將最終匹配到 location /dir01/dir02例子,有如下匹配規(guī)則:location = / { echo '規(guī)則A';}location = /login { echo '規(guī)則B';}location ^~ /static/ { echo '規(guī)則C';}location ^~ /static/files { echo '規(guī)則X';}location ~ .(gif|jpg|png|js|css)$ { echo '規(guī)則D';}location ~* .png$ { echo '規(guī)則E';}location /img { echo '規(guī)則Y';}location / { echo '規(guī)則F';}那么產(chǎn)生的效果如下:訪問(wèn)根目錄 /,比如 http://localhost/ 將匹配 規(guī)則A訪問(wèn) http://localhost/login 將匹配 規(guī)則B訪問(wèn) http://localhost/register 則匹配 規(guī)則F訪問(wèn) http://localhost/static/a.html 將匹配 規(guī)則C訪問(wèn) http://localhost/static/files/a.exe 將匹配 規(guī)則X,雖然 規(guī)則C 也能匹配到,但因?yàn)樽畲笃ヅ湓瓌t,最終選中了 規(guī)則X。你可以測(cè)試下,去掉規(guī)則 X ,則當(dāng)前 URL 會(huì)匹配上 規(guī)則C。訪問(wèn) http://localhost/a.gif, http://localhost/b.jpg 將匹配 規(guī)則D 和 規(guī)則 E ,但是 規(guī)則 D 順序優(yōu)先,規(guī)則 E 不起作用,而 http://localhost/static/c.png 則優(yōu)先匹配到 規(guī)則 C訪問(wèn) http://localhost/a.PNG 則匹配 規(guī)則 E ,而不會(huì)匹配 規(guī)則 D ,因?yàn)?規(guī)則 E 不區(qū)分大小寫(xiě)。訪問(wèn) http://localhost/img/a.gif 會(huì)匹配上 規(guī)則D,雖然 規(guī)則Y 也可以匹配上,但是因?yàn)檎齽t匹配優(yōu)先,而忽略了 規(guī)則Y。訪問(wèn) http://localhost/img/a.tiff 會(huì)匹配上 規(guī)則Y。訪問(wèn) http://localhost/category/id/1111 則最終匹配到規(guī)則 F ,因?yàn)橐陨弦?guī)則都不匹配,這個(gè)時(shí)候應(yīng)該是 Nginx 轉(zhuǎn)發(fā)請(qǐng)求給后端應(yīng)用服務(wù)器,比如 FastCGI(php),tomcat(jsp),Nginx 作為反向代理服務(wù)器存在。所以實(shí)際使用中,筆者覺(jué)得至少有三個(gè)匹配規(guī)則定義,如下:# 直接匹配網(wǎng)站根,通過(guò)域名訪問(wèn)網(wǎng)站首頁(yè)比較頻繁,使用這個(gè)會(huì)加速處理,官網(wǎng)如是說(shuō)。# 這里是直接轉(zhuǎn)發(fā)給后端應(yīng)用服務(wù)器了,也可以是一個(gè)靜態(tài)首頁(yè)# 第一個(gè)必選規(guī)則location = / { proxy_pass http://tomcat:8080/index}# 第二個(gè)必選規(guī)則是處理靜態(tài)文件請(qǐng)求,這是 nginx 作為 http 服務(wù)器的強(qiáng)項(xiàng)# 有兩種配置模式,目錄匹配或后綴匹配,任選其一或搭配使用location ^~ /static/ { root /webroot/static/;}location ~* .(gif|jpg|jpeg|png|css|js|ico)$ { root /webroot/res/;}# 第三個(gè)規(guī)則就是通用規(guī)則,用來(lái)轉(zhuǎn)發(fā)動(dòng)態(tài)請(qǐng)求到后端應(yīng)用服務(wù)器# 非靜態(tài)文件請(qǐng)求就默認(rèn)是動(dòng)態(tài)請(qǐng)求,自己根據(jù)實(shí)際把握# 畢竟目前的一些框架的流行,帶.php、.jsp后綴的情況很少了location / { proxy_pass http://tomcat:8080/}rewrite 語(yǔ)法 last – 基本上都用這個(gè) Flag break – 中止 Rewirte,不再繼續(xù)匹配 redirect – 返回臨時(shí)重定向的 HTTP 狀態(tài) 302 permanent – 返回永久重定向的 HTTP 狀態(tài) 3011、下面是可以用來(lái)判斷的表達(dá)式: -f 和 !-f 用來(lái)判斷是否存在文件 -d 和 !-d 用來(lái)判斷是否存在目錄 -e 和 !-e 用來(lái)判斷是否存在文件或目錄 -x 和 !-x 用來(lái)判斷文件是否可執(zhí)行2、下面是可以用作判斷的全局變量 例:http://localhost:88/test1/test2/test.php?k=v $host:localhost $server_port:88 $request_uri:/test1/test2/test.php?k=v $document_uri:/test1/test2/test.php $document_root:D: ginx/html $request_filename:D: ginx/html/test1/test2/test.phpredirect 語(yǔ)法server { listen 80; server_name start.igrow.cn; index index.html index.php; root html; if ($http_host !~ '^star.igrow.cn$') {rewrite ^(.*) http://star.igrow.cn$1 redirect; }}防盜鏈location ~* .(gif|jpg|swf)$ { valid_referers none blocked start.igrow.cn sta.igrow.cn; if ($invalid_referer) { rewrite ^/ http://$host/logo.png; }}根據(jù)文件類型設(shè)置過(guò)期時(shí)間location ~* .(js|css|jpg|jpeg|gif|png|swf)$ { if (-f $request_filename) {expires 1h;break; }}禁止訪問(wèn)某個(gè)目錄location ~* .(txt|doc)${ root /data/www/wwwroot/linuxtone/test; deny all;}---------------------

以上就是nginx 反向代理負(fù)載均衡策略配置SSL訪問(wèn)匹配規(guī)則優(yōu)先級(jí)的詳細(xì)內(nèi)容,更多關(guān)于nginx配置SSL訪問(wèn)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Nginx
主站蜘蛛池模板: 国产精品99久久久久久动医院 | 国产一区欧美一区 | 亚洲视频在线一区 | 在线观看中文字幕av | 精品国产第一区二区三区 | 嫩草视频入口 | 综合网伊人 | 日韩在线一区二区 | 在线观看三级av | 久久99精品国产99久久6男男 | 久久久91精品国产一区二区三区 | 不卡一区二区在线观看 | 亚洲视频欧美视频 | 羞羞视频在线观看网站 | 伊人久久成人 | 欧美看片 | 草久久免费视频 | 视频二区在线观看 | 亚洲一区二区三区在线 | 国产亚洲精品综合一区 | 久久久精品天堂 | 精精精精xxxx免费视频 | 亚洲另类春色偷拍在线观看 | 成人一级黄色毛片 | 久久久国产精品视频 | 精品欧美一区二区三区免费观看 | 日韩精品免费在线观看 | 欧美自拍视频 | 成人午夜电影在线观看 | 精品国产一区二区三区久久 | 日韩欧美在线视频 | 少妇精品久久久久久久久久 | 国产精品亚洲欧美日韩一区在线 | 午夜免费看视频 | 国产一区精品在线 | 成人国产精品入口免费视频 | 日韩亚洲视频 | 国产精品激情小视频 | 激情欧美一区二区三区中文字幕 | 丝袜 亚洲 另类 欧美 综合 | 久久久久亚洲av毛片大全 |