濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Nginx代理axios請(qǐng)求以及注意事項(xiàng)詳解

Nginx代理axios請(qǐng)求以及注意事項(xiàng)詳解

熱門(mén)標(biāo)簽:虛假地圖標(biāo)注 地圖標(biāo)注如何改成微信號(hào) 山東企業(yè)外呼系統(tǒng)公司 承德地圖標(biāo)注公司 靈圖uu電子寵物店地圖標(biāo)注 百度地圖標(biāo)注公司位置要多少錢(qián) 400電話(huà)號(hào)碼辦理多少錢(qián) 濮陽(yáng)好的聯(lián)通400電話(huà)申請(qǐng) 地圖標(biāo)注黃河的位置

前言

近期寫(xiě)個(gè)小demo,因?yàn)橛玫侥炒髲S的在線數(shù)據(jù),接口做了跨域限制,所以利用Nginx代理來(lái)解決這些問(wèn)題。

1. nginx.conf 配置信息

由于nginx.conf配置信息較多,本篇只關(guān)注跟axios和靜態(tài)資源請(qǐng)求設(shè)置,順便也將常見(jiàn)的一些配置項(xiàng)備注一下。具體設(shè)置如下:

# 設(shè)定http服務(wù)器,利用它的反向代理功能提供負(fù)載均衡支持
http {
 #連接超時(shí)時(shí)間
 keepalive_timeout 120;
 
 #gzip壓縮開(kāi)關(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)聽(tīng)80端口
 listen 80
 
 #定義服務(wù)名稱(chēng)
 server_name localthost;
 
 #首頁(yè)
 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文件無(wú)法加載無(wú)法訪問(wèn)的問(wèn)題,注意末尾不能有 /
  location ~ .*\.(js|css|jpg|png)$ {
  proxy_pass http://zp_server;
  } 
 }
}

2. proxy_pass的斜杠問(wèn)題

Nginx的官網(wǎng)將proxy_pass分為兩種類(lèi)型:

  • 一種是只包含IP和端口號(hào)的(連端口之后的/也沒(méi)有,這里要特別注意),比如proxy_pass http://localhost:8080,這種方式稱(chēng)為不帶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;
}

在訪問(wèn)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)訪問(wèn)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之間沒(méi)有斜杠,分析一下原因。

 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)注意這里的雙斜杠。
}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

標(biāo)簽:上海 福州 德宏 泰安 樂(lè)山 鷹潭 安康 淮安

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Nginx代理axios請(qǐng)求以及注意事項(xiàng)詳解》,本文關(guān)鍵詞  Nginx,代理,axios,請(qǐng)求,以及,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Nginx代理axios請(qǐng)求以及注意事項(xiàng)詳解》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Nginx代理axios請(qǐng)求以及注意事項(xiàng)詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    天峻县| 西充县| 靖宇县| 剑川县| 鹤壁市| 密山市| 佛坪县| 衡山县| 游戏| 惠州市| 青田县| 闻喜县| 定州市| 即墨市| 新河县| 栾川县| 盐池县| 顺平县| 松原市| 浙江省| 府谷县| 苏尼特右旗| 瑞丽市| 马尔康县| 白河县| 玛纳斯县| 香港 | 阳新县| 精河县| 尖扎县| 家居| 大足县| 铁岭县| 西城区| 离岛区| 阳春市| 宜黄县| 斗六市| 铁力市| 黑水县| 阳原县|