這篇文章將為大家詳細(xì)講解有關(guān)Nginx代理axios請(qǐng)求以及注意事項(xiàng)的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)建站 - 西部信息中心,四川服務(wù)器租用,成都服務(wù)器租用,四川網(wǎng)通托管,綿陽服務(wù)器托管,德陽服務(wù)器托管,遂寧服務(wù)器托管,綿陽服務(wù)器托管,四川云主機(jī),成都云主機(jī),西南云主機(jī),西部信息中心,西南服務(wù)器托管,四川/成都大帶寬,成都機(jī)柜租用,四川老牌IDC服務(wù)商1. nginx.conf 配置信息
由于nginx.conf配置信息較多,本篇只關(guān)注跟axios和靜態(tài)資源請(qǐng)求設(shè)置,順便也將常見的一些配置項(xiàng)備注一下。具體設(shè)置如下:
# 設(shè)定http服務(wù)器,利用它的反向代理功能提供負(fù)載均衡支持 http { #連接超時(shí)時(shí)間 keepalive_timeout 120; #gzip壓縮開關(guān)及相關(guān)配置 gzip on; gzip_min_length 1k; gzip_buffers 4 32k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; gzip_disable "MSIE [1-6]."; #設(shè)定實(shí)際的服務(wù)器列表 upstream zp_server{ server 127.0.0.1:8089; } #HTTP服務(wù)器 server { #監(jiān)聽80端口 listen 80 #定義服務(wù)名稱 server_name localthost; #首頁 index index.html #指向項(xiàng)目根目錄 root D:\project\src\main\webapp; #編碼格式 charset utf-8; #代理的路徑(和upstream綁定),location 后面設(shè)置映射的路徑 location / { #代理配置參數(shù) proxy_connect_timeout 180; proxy_send_timeout 180; proxy_read_timeout 180; proxy_set_header Host $host; proxy_set_header X-Forwarder-For $remote_addr; proxy_pass http://zp_server/; #跨域相關(guān)設(shè)置 add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept' always; } #配置靜態(tài)資源 解決js css文件無法加載無法訪問的問題,注意末尾不能有 / location ~ .*\.(js|css|jpg|png)$ { proxy_pass http://zp_server; } } }
2. proxy_pass的斜杠問題
Nginx的官網(wǎng)將proxy_pass分為兩種類型:
一種是只包含IP和端口號(hào)的(連端口之后的/也沒有,這里要特別注意),比如proxy_pass http://localhost:8080,這種方式稱為不帶URI方式;
另一種是在端口號(hào)之后有其他路徑的,包含了只有單個(gè)/的,如proxy_pass http://localhost:8080/,以及其他路徑,比如proxy_pass http://localhost:8080/abc。
2.1 對(duì)于不帶URI方式
對(duì)于不帶URI方式,Nginx將會(huì)保留location中路徑部分,比如:
location /api1/ { proxy_pass http://localhost:8080; }
在訪問http://localhost/api1/xxx時(shí),會(huì)代理到http://localhost:8080/api1/xxx
2.2 對(duì)于帶URI方式
對(duì)于帶URI方式,nginx將使用諸如alias的替換方式對(duì)URL進(jìn)行替換,并且這種替換只是字面上的替換,比如:
location /api2/ { proxy_pass http://localhost:8080/; }
當(dāng)訪問http://localhost/api2/xxx時(shí),http://localhost/api2/(注意最后的/)被替換成了http://localhost:8080/,然后再加上剩下的xxx,于是變成了http://localhost:8080/xxx。
2.3 總結(jié)一下
server { listen 80; server_name localhost; location /api1/ { proxy_pass http://localhost:8080; } # http://localhost/api1/xxx -> http://localhost:8080/api1/xxx location /api2/ { proxy_pass http://localhost:8080/; } # http://localhost/api2/xxx -> http://localhost:8080/xxx location /api3 { proxy_pass http://localhost:8080; } # http://localhost/api3/xxx -> http://localhost:8080/api3/xxx location /api4 { proxy_pass http://localhost:8080/; } # http://localhost/api4/xxx -> http://localhost:8080//xxx,請(qǐng)注意這里的雙斜線,好好分析一下。 location /api5/ { proxy_pass http://localhost:8080/haha; } # http://localhost/api5/xxx -> http://localhost:8080/hahaxxx,請(qǐng)注意這里的haha和xxx之間沒有斜杠,分析一下原因。 location /api6/ { proxy_pass http://localhost:8080/haha/; } # http://localhost/api6/xxx -> http://localhost:8080/haha/xxx location /api7 { proxy_pass http://localhost:8080/haha; } # http://localhost/api7/xxx -> http://localhost:8080/haha/xxx location /api8 { proxy_pass http://localhost:8080/haha/; } # http://localhost/api8/xxx -> http://localhost:8080/haha//xxx,請(qǐng)注意這里的雙斜杠。 }
關(guān)于“Nginx代理axios請(qǐng)求以及注意事項(xiàng)的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
新聞標(biāo)題:Nginx代理axios請(qǐng)求以及注意事項(xiàng)的示例分析-創(chuàng)新互聯(lián)
URL分享:http://www.rwnh.cn/article2/geeic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、建站公司、營銷型網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、面包屑導(dǎo)航、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容