鍍金池/ 問答/PHP  Linux  HTML/ 前后端分離,ajax api接口跨域,node代理和nginx代理

前后端分離,ajax api接口跨域,node代理和nginx代理

前后端分離,ajax api接口跨域,node代理和nginx代理

回答
編輯回答
鹿惑

前后端分離方式,實(shí)現(xiàn)跨域:

1.模擬后端開發(fā),拉后端代碼。(要后端代碼,沒坑,效率低)
2.前端開啟web服務(wù)器,node服務(wù)器或者nginx服務(wù)器。(前端自由,不用管后端)

node服務(wù)器:

proxyTable: {
  '/api': {
    target: 'http://segmentfault.com',
    pathRewrite: {
      '^/api': ''
    }
  }
}
1.比如ajax接口"/getMessage",本地"localhost:8080/getMessage",有跨域的問題。
2.localhost:8080 => http://segmentfault.com,本地開啟服務(wù)器實(shí)現(xiàn)代理。

nginx服務(wù)器:

http {
    server {
        listen  8080; #本地端口        
        server_name localhost;
        location  ~*/get*|post* { #nginx正則匹配(可高度自定義)            
            proxy_pass http://segmentfault.com; #反向代理
        }
    }
}
1.比如ajax接口"/getMessage",本地"localhost:8080/getMessage",有跨域的問題。
2.localhost:8080 => http://segmentfault.com,本地開啟服務(wù)器實(shí)現(xiàn)代理。
3.nginx反向代理更多配置查詢http://www.nginx.cn/115.html。

參考:
1.Vue-cli proxyTable
2.nginx搭建web服務(wù)器

2017年11月21日 06:37