【发布时间】:2015-02-27 01:33:09
【问题描述】:
(如果需要,请参阅my last question 了解更多背景信息。)
我正在开发一个使用分离的前端和后端的应用程序:
- 后端是一个主要提供 REST API 的 Rails 应用(在
localhost:3000上提供服务)。 - 前端是一个 AngularJS 应用程序,我正在使用 Gulp 构建它并在
localhost:3001上本地提供服务(使用 BrowserSync)。
为了让两端相互通信,同时尊重same-origin policy,我配置了 nginx 作为两者之间的代理,在localhost:3002 上可用。这是我的 nginx.conf:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 3002;
root /;
# Rails
location ~ \.(json)$ {
proxy_pass http://localhost:3000;
}
# AngularJS
location / {
proxy_pass http://localhost:3001;
}
}
}
基本上,任何对 .json 文件的请求,我都会发送到 Rails 服务器,而任何其他请求(例如,静态资产),我都会发送到 BrowserSync 服务器。
来自我gulpfile.coffee 的 BrowserSync 任务:
gulp.task 'browser-sync', ->
browserSync
server:
baseDir: './dist'
directory: true
port: 3001
browser: 'google chrome'
startPath: './index.html#/foo'
这一切基本上都有效,但有几个我正在尝试解决的警告:
- 当我运行 gulp 任务时,基于上述配置,BrowserSync 会在
http://localhost:3001/index.html#/foo加载一个 Chrome 选项卡。但是,由于我使用的是 nginx 代理,因此我需要端口为 3002。有没有办法告诉 BrowserSync,“在端口 3001 上运行,但在端口 3002 上启动”?我尝试为startPath使用绝对路径,但它只需要相对路径。 - 每次 BrowserSync 启动时,我都会在控制台中收到一个(看似良性的)JavaScript 错误:
WebSocket connection to 'ws://localhost:3002/browser-sync/socket.io/?EIO=3&transport=websocket&sid=m-JFr6algNjpVre3AACY' failed: Error during WebSocket handshake: Unexpected response code: 400。不确定这到底是什么意思,但我的假设是 BrowserSync 被 nginx 代理弄糊涂了。
如何解决这些问题以使其无缝运行?
感谢您的任何意见!
【问题讨论】:
标签: nginx proxy gulp same-origin-policy browser-sync