摘要:作為前端開(kāi)發(fā),每次調(diào)試接口,把代碼發(fā)到測(cè)試服務(wù)器,是很費(fèi)時(shí)費(fèi)事的一件事情。為了提高效率,想到了反向代理來(lái)解決這一問(wèn)題。如何在手機(jī)上調(diào)試呢手機(jī)上不可能直接訪問(wèn)可以把手機(jī)和電腦連接到同一個(gè)網(wǎng)段,使用電腦的進(jìn)行訪問(wèn)。
作為前端開(kāi)發(fā),每次調(diào)試接口,把代碼發(fā)到測(cè)試服務(wù)器,是很費(fèi)時(shí)費(fèi)事的一件事情。
為了提高效率,想到了nginx反向代理來(lái)解決這一問(wèn)題。
接口地址:
test.com
訪問(wèn)地址:
localhost
最核心的問(wèn)題就是,登錄時(shí),無(wú)法寫(xiě)入cookie的問(wèn)題,為了解決這個(gè)問(wèn)題,走了不少?gòu)澛贰?/p>
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 10; server { listen 80; server_name localhost; location =/ { add_header X-Frame-Options SAMEORIGIN; root D:/workspace/; index index.html; } location ~* .(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|swf|woff|woff2|ttf|json|svg|cur|vue|otf|eot)$ { charset utf-8; root D:/workspace/; expires 3d; } location = /socket/v2 { proxy_pass http://test.com; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host test.com; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 30; proxy_send_timeout 30; proxy_read_timeout 60; proxy_buffer_size 256k; proxy_buffers 4 256k; } location / { proxy_pass http://test.com; proxy_set_header Cookie $http_cookie; proxy_cookie_domain test.com localhost; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host test.com; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; } } }
核心代碼在三行代碼上:
proxy_set_header Cookie $http_cookie;
proxy_cookie_domain test.com localhost;
proxy_set_header Host test.com;
具體解釋我也是一知半解:
第一個(gè)是攜帶cookie,
第二個(gè)設(shè)置cookie 的 domain
第三個(gè) 設(shè)置真實(shí)的host
如何在手機(jī)上調(diào)試呢?
手機(jī)上不可能直接訪問(wèn)localhost,可以把手機(jī)和電腦連接到同一個(gè)網(wǎng)段,使用電腦的ip進(jìn)行訪問(wèn)。
但是這里只代理了localhost,并沒(méi)有代理電腦的ip
所以,需要把是上面的server{...}拷貝一份,只需要把里面的localhost全部改成你的電腦ip就可以了,最終代碼:
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 10; server { listen 80; server_name localhost; location =/ { add_header X-Frame-Options SAMEORIGIN; root D:/workspace/; index index.html; } location ~* .(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|swf|woff|woff2|ttf|json|svg|cur|vue|otf|eot)$ { charset utf-8; root D:/workspace/; expires 3d; } location = /socket/v2 { proxy_pass http://test.com; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host test.com; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 30; proxy_send_timeout 30; proxy_read_timeout 60; proxy_buffer_size 256k; proxy_buffers 4 256k; } location / { proxy_pass http://test.com; proxy_set_header Cookie $http_cookie; proxy_cookie_domain test.com localhost; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host test.com; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; } } server { listen 8080; server_name xx.xx.xx.xx; location =/ { add_header X-Frame-Options SAMEORIGIN; root D:/workspace/; index index.html; } location ~* .(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|swf|woff|woff2|ttf|json|svg|cur|vue|otf|eot)$ { charset utf-8; root D:/workspace/; expires 3d; } location = /socket/v2 { proxy_pass http://test.com; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host test.com; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 30; proxy_send_timeout 30; proxy_read_timeout 60; proxy_buffer_size 256k; proxy_buffers 4 256k; } location / { proxy_pass http://test.com; proxy_set_header Cookie $http_cookie; proxy_cookie_domain test.com xx.xx.xx.xx; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host test.com; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; } } }
訪問(wèn)方法:
http://xx.xx.xx.xx:8080 即可
如果是打包工具生成增這個(gè)配置的話(huà),可以用nodejs動(dòng)態(tài)獲取你電腦的ip
function? getIPAdress() {?????? var? interfaces? = ?require("os").networkInterfaces();?????? for (var? devName? in ?interfaces) {???????????? var? iface? = ?interfaces[devName];???????????? for (var? i = 0; i < iface.length; i++) {????????????????? var? alias? = ?iface[i];????????????????? if (alias.family? === ?"IPv4"? && ?alias.address? !== ?"127.0.0.1"? && ?!alias.internal) {??????????????????????? return? alias.address; }???????????? }?????? }?? }
所以,這里貼出來(lái)一個(gè)動(dòng)態(tài)生成nginx.config的工具
function buildNginxConfig(config) { function? getIPAdress() {?????? var? interfaces? = ?require("os").networkInterfaces();?????? for (var? devName? in ?interfaces) {???????????? var? iface? = ?interfaces[devName];???????????? for (var? i = 0; i < iface.length; i++) {????????????????? var? alias? = ?iface[i];????????????????? if (alias.family? === ?"IPv4"? && ?alias.address? !== ?"127.0.0.1"? && ?!alias.internal) {??????????????????????? return? alias.address;????????????????? }???????????? }?????? }?? } var cwd = process.cwd().replace(//g, "/") + "/app"; var protocol = /https|443/.test(config.ip) ? "https" : "http"; var servers = [{ browserIp: "localhost", port: 80, root: cwd, serverIp: config.ip, protocol: protocol, }, { browserIp: getIPAdress(), port: 8080, root: cwd, serverIp: config.ip, protocol: protocol, }].map(function(item) { return ` server { listen ${item.port}; server_name ${item.browserIp}; location =/ { add_header X-Frame-Options SAMEORIGIN; root ${item.root}; index index.html; } location ~* .(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|swf|woff|woff2|ttf|json|svg|cur|vue|otf|eot)$ { charset utf-8; root ${item.root}; expires 3d; } location = /socket/v2 { proxy_pass ${item.protocol}://${item.serverIp}; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host ${item.serverIp}; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 30; proxy_send_timeout 30; proxy_read_timeout 60; proxy_buffer_size 256k; proxy_buffers 4 256k; } location / { proxy_pass ${item.protocol}://${item.serverIp}; proxy_set_header Cookie $http_cookie; proxy_cookie_domain ${item.serverIp} ${item.browserIp}; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host ${item.serverIp}; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; } }`; }).join(" "); var str = `worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 10; ${servers} }`; return str; } exports = module.exports = buildNginxConfig;
有了這個(gè)萬(wàn)能反向代理,可以隨心所欲的玩轉(zhuǎn)任何網(wǎng)站接口了
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/98105.html
摘要:作為前端開(kāi)發(fā),每次調(diào)試接口,把代碼發(fā)到測(cè)試服務(wù)器,是很費(fèi)時(shí)費(fèi)事的一件事情。為了提高效率,想到了反向代理來(lái)解決這一問(wèn)題。如何在手機(jī)上調(diào)試呢手機(jī)上不可能直接訪問(wèn)可以把手機(jī)和電腦連接到同一個(gè)網(wǎng)段,使用電腦的進(jìn)行訪問(wèn)。 作為前端開(kāi)發(fā),每次調(diào)試接口,把代碼發(fā)到測(cè)試服務(wù)器,是很費(fèi)時(shí)費(fèi)事的一件事情。為了提高效率,想到了nginx反向代理來(lái)解決這一問(wèn)題。 接口地址:test.com 訪問(wèn)地址:loca...
摘要:還是回到萬(wàn)能的文件,添加匹配規(guī)則實(shí)現(xiàn)代理轉(zhuǎn)發(fā)設(shè)置代理轉(zhuǎn)發(fā)通過(guò)上面的設(shè)置,在重啟服務(wù),可以讓頁(yè)面中所有包含字段的請(qǐng)求都轉(zhuǎn)為由服務(wù)器去向地址發(fā)送請(qǐng)求,從而巧妙的解決了瀏覽器的跨域問(wèn)題。 1.Nginx入門(mén)與基本操作篇 注:由于服務(wù)器是windows系統(tǒng),所以本文主要講解Nginx在windows下的操作。 首先下載Nginx 解壓縮,我們所有的配置基本都在萬(wàn)能的 nginx/conf/...
摘要:傳給微信的參數(shù)進(jìn)行轉(zhuǎn)義其中參數(shù)是可以被微信原樣返回,這樣就可以按你自己的需求完成反向代理了??梢匀サ舸罱y(cè)試環(huán)境另一條運(yùn)維的原則是不要在生產(chǎn)環(huán)境上直接改,在測(cè)試環(huán)境修改并經(jīng)過(guò)測(cè)試,測(cè)試通過(guò)后,再上傳到生產(chǎn)環(huán)境。 前言 在與第三方系統(tǒng)進(jìn)行接口開(kāi)發(fā)時(shí),需要不斷的改進(jìn)和測(cè)試,以常見(jiàn)的微信登錄支付和 Alipay 支付和登錄為例. 相對(duì)來(lái)講 Alipay 做起來(lái)容易一些, 一是接口 SDK 封...
摘要:三反向代理解決的原理將安裝在本地,然后將項(xiàng)目部署于下,這樣訪問(wèn)本地項(xiàng)目時(shí)用本地項(xiàng)目即可訪問(wèn)。這樣瀏覽器之間的請(qǐng)求完全滿(mǎn)足瀏覽器域名協(xié)議端口相同的同源策略,可在不改變后臺(tái)接口的情況下避免跨域問(wèn)題。 一、問(wèn)題背景說(shuō)明: 編寫(xiě)移動(dòng)前端頁(yè)面時(shí)需要訪問(wèn)后臺(tái)系統(tǒng)接口。前端項(xiàng)目在本地(個(gè)人辦公電腦)開(kāi)發(fā),后臺(tái)接口存放后生產(chǎn)的后臺(tái)服務(wù)器,本地的ajax請(qǐng)求無(wú)法直接訪問(wèn)后臺(tái)接口,也就是遇到了跨域問(wèn)題...
閱讀 2439·2021-11-23 09:51
閱讀 2471·2021-11-11 17:21
閱讀 3112·2021-09-04 16:45
閱讀 2397·2021-08-09 13:42
閱讀 2230·2019-08-29 18:39
閱讀 2898·2019-08-29 14:12
閱讀 1299·2019-08-29 13:49
閱讀 3373·2019-08-29 11:17