当子位置上的代理nginx时,套接字的问题有效。
从官方文档部署示例应用程序 https://github.com/chrismccord/phoenix_chat_example
进一步配置Nginx
server {
listen 4200;
server_name phoenix_chat_example;
access_log /var/log/nginx/phoenix_chat_example.access.log;
error_log /var/log/nginx/phoenix_chat_example.error.log;
charset utf8;
location ^~ / {
proxy_pass http://localhost:4000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
然后在配置phoenix_chat_example / config / dev.exs中添加一个选项(check_origin:false)
config :chat, Chat.Endpoint,
http: [port: System.get_env("PORT") || 4000],
debug_errors: true,
cache_static_lookup: false,
check_origin: false,
code_reloader: true,
watchers: [node: ["node_modules/brunch/bin/brunch", "watch"]]
我们转到http://phoenix_chat_example:4200/,看看一切正常
现在更改配置nginx中的位置
位置^〜/ chat_application {.....
并更改静态资源的路径
" /css/app.css" - > " /chat_application/css/app.css" " /js/app.js" - > " /chat_application/js/app.js"
在phoenix_chat_example / web / templates / layout / app.html.eex
中并在phoenix_chat_example / web / static / js / app.js中更改套接字网址
new Socket(" / socket" - > new Socket(" / chat_application / socket"
我们看到错误
phoenix.js:644 WebSocket连接到 ' WS:// phoenix_chat_example:4200 / chat_application /插座/ WebSocket的USER_ID = 123' 失败:WebSocket握手期间出错:意外的响应代码: 404
在客户端
和
[info] GET // socket / websocket [warn] **(Phoenix.Router.NoRouteError) 找不到GET / socket / websocket的路由(Chat.Router) (chat)web / router.ex:1:Chat.Router.match_route / 4 (chat)web / router.ex:1:Chat.Router.do_call / 2 (聊天)lib / chat / endpoint.ex:1:Chat.Endpoint.phoenix_pipeline / 1 (聊天)lib / plug / debugger.ex:93:Chat.Endpoint。" call(overridable 3)" / 2 (聊天)lib / phoenix / endpoint / render_errors.ex:34:Chat.Endpoint.call/2 (插件)lib / plug / adapters / cowboy / handler.ex:15:Plug.Adapters.Cowboy.Handler.upgrade / 4 (牛仔)src / cowboy_protocol.erl:442 :: cowboy_protocol.execute / 4
在后端