成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

一段萬(wàn)能的nginx接口反向代理配置

miqt / 1681人閱讀

摘要:作為前端開(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

重要提示:以上3個(gè)的順序不要顛倒,否則代理失敗,我也不知道為什么。

如何在手機(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

相關(guān)文章

  • 一段萬(wàn)能nginx接口反向代理配置

    摘要:作為前端開(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...

    z2xy 評(píng)論0 收藏0
  • Nginx入門(mén)及如何反向代理解決生產(chǎn)環(huán)境跨域問(wèn)題

    摘要:還是回到萬(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/...

    Moxmi 評(píng)論0 收藏0
  • nginx反向代理與微信測(cè)試全攻略

    摘要:傳給微信的參數(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 封...

    endless_road 評(píng)論0 收藏0
  • 通過(guò)nginx反向代理解決前端訪問(wèn)跨域問(wèn)題

    摘要:三反向代理解決的原理將安裝在本地,然后將項(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)題...

    dongxiawu 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<