{"id":3334,"date":"2020-03-29T20:45:15","date_gmt":"2020-03-29T11:45:15","guid":{"rendered":"https:\/\/www.sambuichi.jp\/?p=3334"},"modified":"2020-03-30T11:32:23","modified_gmt":"2020-03-30T02:32:23","slug":"node-js%e7%92%b0%e5%a2%83%e8%a8%ad%e5%ae%9a","status":"publish","type":"post","link":"https:\/\/www.sambuichi.jp\/?p=3334","title":{"rendered":"NGINX\u3092WebSocket Proxy\u306b\u4f7f\u3046\uff08Amazon EC2\uff09"},"content":{"rendered":"<p>Views: 74<\/p><link rel=\"stylesheet\" href=\"https:\/\/use.fontawesome.com\/releases\/v5.6.3\/css\/all.css\"\n      integrity=\"sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s\/\"\n      crossorigin=\"anonymous\">\n<div class=\"index\" style=\"width: 90%; margin: auto; padding-left: 8px; border: solid 1px #d0d0d0;\">\n<div style=\"text-align: center;\">\u76ee\u6b21<\/div>\n<p style=\"margin: 0;\">\n<a href=\"#S1\">NGINX\u3092WebSocket Proxy\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b<\/a><br \/>\n<a href=\"#S2\">node.js\u3092Amazon EC2\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/a><br \/>\n<a href=\"#S3\">ws\u3092WebSocket\u30b5\u30fc\u30d0\u30fc\u7528\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b<\/a><br \/>\n<a href=\"#S4\">wscat\u3092WebSocket\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b<\/a><br \/>\n<a href=\"#S5\">Nginx\u8a2d\u5b9a\u3068\u78ba\u8a8d<\/a>\n<\/p>\n<\/div>\n<p><span id=\"S1\"><\/span><\/p>\n<h4>NGINX\u3092WebSocket Proxy\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b<\/h4>\n<p><a href=\"https:\/\/www.nginx.com\/blog\/websocket-nginx\/\">NGINX as a WebSocket Proxy<\/a>\u306b\u5f93\u3063\u3066\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3057\u3066\u78ba\u8a8d\u3059\u308b\u3002<br \/>\n1. node.js\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<br \/>\n\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u3067\u6b21\u306e\u3088\u3046\u306b\u5165\u529b\u3057\u3066\u3001yum\u3067nodejs\u3068npm\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true \" >$ sudo yum install nodejs npm<\/pre>\n<pre><code>\r\n\u8aad\u307f\u8fbc\u3093\u3060\u30d7\u30e9\u30b0\u30a4\u30f3:priorities, update-motd, upgrade-helper\r\namzn-main                                                                                   | 2.1 kB  00:00:00     \r\namzn-updates                                                                                | 2.5 kB  00:00:00     \r\n1143 packages excluded due to repository priority protections\r\n...<\/code><\/pre>\n<p>2. ws\u3068wscat\u3092\u4f7f\u7528\u3057\u3066\u8a2d\u5b9a\u78ba\u8a8d<br \/>\n \u307e\u305a\u3001\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u3067\u6b21\u306e\u3088\u3046\u306b\u5165\u529b\u3057\u3066\u3001ws\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true \" >$ sudo npm install ws<\/pre>\n<pre><code>npm http GET https:\/\/registry.npmjs.org\/ws\r\nnpm http GET https:\/\/registry.npmjs.org\/ws\r\nnpm http GET https:\/\/registry.npmjs.org\/ws\r\nnpm ERR! Error: UNABLE_TO_GET_ISSUER_CERT_LOCALLY\r\n...<\/code><\/pre>\n<p>\u30a8\u30e9\u30fc\u304c\u51fa\u305f\u306e\u3067\u3001NGINX\u306e\u30da\u30fc\u30b8\u306b\u3042\u308b\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u518d\u5b9f\u884c\u3057\u3066\u307f\u305f\u3002<\/p>\n<pre class=\"lang:default highlight:0 decode:true \" >$ sudo npm config set registry http:\/\/registry.npmjs.org\/\r\n$ sudo npm install ws\r\n<\/pre>\n<pre><code>\r\nnpm http GET http:\/\/registry.npmjs.org\/ws\r\nnpm http 200 http:\/\/registry.npmjs.org\/ws\r\nnpm http GET http:\/\/registry.npmjs.org\/ws\/-\/ws-7.2.3.tgz\r\nnpm http 200 http:\/\/registry.npmjs.org\/ws\/-\/ws-7.2.3.tgz\r\nnpm WARN engine ws@7.2.3: wanted: {\"node\":\">=8.3.0\"} (current: {\"node\":\"v0.10.48\",\"npm\":\"1.3.6\"})\r\nnpm http GET http:\/\/registry.npmjs.org\/bufferutil\r\nnpm http GET http:\/\/registry.npmjs.org\/utf-8-validate\r\nnpm http 200 http:\/\/registry.npmjs.org\/bufferutil\r\nnpm http GET http:\/\/registry.npmjs.org\/bufferutil\/-\/bufferutil-4.0.1.tgz\r\nnpm http 200 http:\/\/registry.npmjs.org\/utf-8-validate\r\nnpm http GET http:\/\/registry.npmjs.org\/utf-8-validate\/-\/utf-8-validate-5.0.2.tgz\r\nnpm http 200 http:\/\/registry.npmjs.org\/bufferutil\/-\/bufferutil-4.0.1.tgz\r\nnpm http 200 http:\/\/registry.npmjs.org\/utf-8-validate\/-\/utf-8-validate-5.0.2.tgz\r\nnpm http GET http:\/\/registry.npmjs.org\/node-gyp-build\r\nnpm http 200 http:\/\/registry.npmjs.org\/node-gyp-build\r\nnpm http GET http:\/\/registry.npmjs.org\/node-gyp-build\/-\/node-gyp-build-3.7.0.tgz\r\nnpm http 200 http:\/\/registry.npmjs.org\/node-gyp-build\/-\/node-gyp-build-3.7.0.tgz\r\n\r\n> utf-8-validate@5.0.2 install \/etc\/nginx\/sites-available\/node_modules\/utf-8-validate\r\n> node-gyp-build\r\n\r\n> bufferutil@4.0.1 install \/etc\/nginx\/sites-available\/node_modules\/bufferutil\r\n> node-gyp-build\r\n\r\nmake: \u30c7\u30a3\u30ec\u30af\u30c8\u30ea `\/etc\/nginx\/sites-available\/node_modules\/bufferutil\/build' \u306b\u5165\u308a\u307e\u3059\r\nmake: \u30c7\u30a3\u30ec\u30af\u30c8\u30ea `\/etc\/nginx\/sites-available\/node_modules\/utf-8-validate\/build' \u306b\u5165\u308a\u307e\u3059\r\n  CC(target) Release\/obj.target\/bufferutil\/src\/bufferutil.o\r\n  CC(target) Release\/obj.target\/validation\/src\/validation.o\r\n..\/src\/bufferutil.c:3:22: fatal error: node_api.h: \u305d\u306e\u3088\u3046\u306a\u30d5\u30a1\u30a4\u30eb\u3084\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306f\u3042\u308a\u307e\u305b\u3093\r\n #include <node_api.h><\/code><\/pre>\n<p>\u3053\u3053\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u4f5c\u696d\u3092\u4e2d\u65ad\u3057\u3001\u30b5\u30fc\u30d0\u30fc\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true \" >[ec2-user@ip-*-*-*-* ~]$ mkdir websocket\r\n[ec2-user@ip-*-*-*-* ~]$ cd websocket\r\n[ec2-user@ip-*-*-*-* websocket]$ vi server.js<\/pre>\n<p>server.js<\/p>\n<pre><code>console.log(\"Server started\");\r\nvar Msg = '';\r\nvar WebSocketServer = require('ws').Server,\r\n    wss = new WebSocketServer({port: 8010});\r\n    wss.on('connection', function(ws) {\r\n        ws.on('message', function(message) {\r\n        console.log('Received from client: %s', message);\r\n        ws.send('Server received from client: ' + message);\r\n    });\r\n});<\/code><\/pre>\n<pre class=\"lang:default decode:true \" >$ node server.js<\/pre>\n<pre><code>Server started\r\n\r\nmodule.js:340\r\n    throw err;\r\n          ^\r\nError: Cannot find module 'ws'\r\n    at Function.Module._resolveFilename (module.js:338:15)\r\n    at Function.Module._load (module.js:280:25)\r\n    at Module.require (module.js:364:17)\r\n    at require (module.js:380:17)\r\n    at Object.<anonymous> (\/home\/ec2-user\/websocket\/server.js:3:23)\r\n    at Module._compile (module.js:456:26)\r\n    at Object.Module._extensions..js (module.js:474:10)\r\n    at Module.load (module.js:356:32)\r\n    at Function.Module._load (module.js:312:12)\r\n    at Function.Module.runMain (module.js:497:10)<\/code><\/pre>\n<p>ws\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u304d\u3066\u3044\u306a\u3044\u306e\u3067\u30a8\u30e9\u30fc\u3068\u306a\u3063\u3066\u3044\u308b\u3002<br \/>\nnpm\u3067\u306a\u304f\u3001nvm\u3067\u518d\u5ea6Amazon\u306e\u624b\u9806\u306b\u5f93\u3063\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/p>\n<p><span id=\"S2\"><\/span><\/p>\n<h4>node.js\u3092Amazon EC2\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h4>\n<p>1. Amazon Elastic Compute Cloud (Amazon EC2) \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306bSSH\u3092\u4f7f\u7528\u3057\u3066ec2-user\u3067\u63a5\u7d9a\u3059\u308b\u3002<\/p>\n<p>2. \u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u3067\u6b21\u306e\u3088\u3046\u306b\u5165\u529b\u3057\u3066\u3001\u30ce\u30fc\u30c9\u30d0\u30fc\u30b8\u30e7\u30f3\u30de\u30cd\u30fc\u30b8\u30e3\uff08nvm\uff09\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<br \/>\nnvm\u3067\u306f\u3001\u8907\u6570\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306enodejs\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u51fa\u6765\u308b\u3057\u30d0\u30fc\u30b8\u30e7\u30f3\u9593\u5fa1\u5207\u308a\u66ff\u3048\u3082\u51fa\u6765\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true \" >curl -o- https:\/\/raw.githubusercontent.com\/nvm-sh\/nvm\/v0.34.0\/install.sh | bash\r\n<\/pre>\n<pre><code>$ curl -o- https:\/\/raw.githubusercontent.com\/nvm-sh\/nvm\/v0.34.0\/install.sh | bash\r\n  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\r\n                                 Dload  Upload   Total   Spent    Left  Speed\r\n100 13226  100 13226    0     0  66462      0 --:--:-- --:--:-- --:--:-- 66462\r\n=> Downloading nvm from git to '\/home\/ec2-user\/.nvm'\r\n=> Cloning into '\/home\/ec2-user\/.nvm'...\r\nremote: Enumerating objects: 278, done.\r\nremote: Counting objects: 100% (278\/278), done.\r\nremote: Compressing objects: 100% (245\/245), done.\r\nremote: Total 278 (delta 32), reused 95 (delta 20), pack-reused 0\r\nReceiving objects: 100% (278\/278), 142.15 KiB | 443.00 KiB\/s, done.\r\nResolving deltas: 100% (32\/32), done.\r\n=> Compressing and cleaning up git repository\r\n\r\n=> Appending nvm source string to \/home\/ec2-user\/.bashrc\r\n=> Appending bash_completion source string to \/home\/ec2-user\/.bashrc\r\n=> You currently have modules installed globally with npm. These will no\r\n=> longer be linked to the active version of Node when you install a new node\r\n=> with nvm; and they may (depending on how you construct your $PATH)\r\n=> override the binaries of modules installed with nvm:\r\n=> If you wish to uninstall them at a later point (or re-install them under your\r\n=> nvm Nodes), you can remove them from the system Node as follows:\r\n\r\n     $ nvm use system\r\n     $ npm uninstall -g a_module\r\n\r\n=> Close and reopen your terminal to start using nvm or run the following to use it now:\r\n\r\nexport NVM_DIR=\"$HOME\/.nvm\"\r\n[ -s \"$NVM_DIR\/nvm.sh\" ] &amp;&amp; \\. \"$NVM_DIR\/nvm.sh\"  # This loads nvm\r\n[ -s \"$NVM_DIR\/bash_completion\" ] &amp;&amp; \\. \"$NVM_DIR\/bash_completion\"  # This loads nvm bash_completion\r\n<\/code><\/pre>\n<p>3. \u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u3067\u6b21\u306e\u3088\u3046\u306b\u5165\u529b\u3057\u3066\u3001nvm\u3092\u6709\u52b9\u306b\u3059\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true \" >. ~\/.nvm\/nvm.sh<\/pre>\n<p>4. \u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u3067\u6b21\u306e\u3088\u3046\u306b\u5165\u529b\u3057\u3066\u3001\u6700\u65b0\u306enode.js\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true \" >nvm install node<\/pre>\n<pre><code>\r\nDownloading and installing node v13.12.0...\r\nDownloading https:\/\/nodejs.org\/dist\/v13.12.0\/node-v13.12.0-linux-x64.tar.xz...\r\n######################################################################### 100.0%\r\nComputing checksum with sha256sum\r\nChecksums matched!\r\nNow using node v13.12.0 (npm v6.14.4)\r\nCreating default alias: default -> node (-> v13.12.0)\r\n<\/code><\/pre>\n<p>node.js\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3068npm(Node Package Manager)\u3082\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u308b\u306e\u3067\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u8ffd\u52a0\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u53ef\u80fd\u3002<\/p>\n<p>5. \u6b63\u5e38\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u305f\u304b\u3001\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u3067\u6b21\u306e\u3088\u3046\u306b\u5165\u529b\u3057\u3066\u78ba\u8a8d\u3059\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true \" >node -e \"console.log('Running Node.js ' + process.version)\"<\/pre>\n<p>\u7d44\u307f\u8fbc\u307e\u308c\u305fnode.js\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u6b21\u306e\u3088\u3046\u306a\u30e1\u30c3\u30bb\u30fc\u30b8\u3067\u8868\u793a\u3055\u308c\u308b\u3002 <\/p>\n<pre><code>Running Node.js v13.12.0<\/code><\/pre>\n<p>\u53c2\u8003\uff1a<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/WebSockets_API\/Writing_WebSocket_servers\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/WebSockets_API\/Writing_WebSocket_servers<\/a><\/p>\n<p><span id=\"S3\"><\/span><\/p>\n<h4>ws\u3092WebSocket\u30b5\u30fc\u30d0\u30fc\u7528\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b<\/h4>\n<p> \u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u3067\u6b21\u306e\u3088\u3046\u306b\u5165\u529b\u3057\u3066ws\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true \" >$ sudo npm install ws<\/pre>\n<pre><code>npm WARN package.json websockets@1.0.0 No description\r\nnpm WARN package.json websockets@1.0.0 No repository field.\r\nnpm WARN package.json websockets@1.0.0 No README data\r\nnpm http GET http:\/\/registry.npmjs.org\/ws\/-\/ws-1.1.5.tgz\r\nnpm http 200 http:\/\/registry.npmjs.org\/ws\/-\/ws-1.1.5.tgz\r\nnpm http GET http:\/\/registry.npmjs.org\/ultron\r\nnpm http GET http:\/\/registry.npmjs.org\/options\r\nnpm http 200 http:\/\/registry.npmjs.org\/options\r\nnpm http 200 http:\/\/registry.npmjs.org\/ultron\r\nnpm http GET http:\/\/registry.npmjs.org\/options\/-\/options-0.0.6.tgz\r\nnpm http GET http:\/\/registry.npmjs.org\/ultron\/-\/ultron-1.0.2.tgz\r\nnpm http 200 http:\/\/registry.npmjs.org\/ultron\/-\/ultron-1.0.2.tgz\r\nnpm http 200 http:\/\/registry.npmjs.org\/options\/-\/options-0.0.6.tgz\r\nws@1.1.5 node_modules\/ws\r\n\u251c\u2500\u2500 options@0.0.6\r\n\u2514\u2500\u2500 ultron@1.0.2<\/code><\/pre>\n<p><span id=\"S4\"><\/span><\/p>\n<h4>wscat\u3092WebSocket\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b<\/h4>\n<p> \u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u3067\u6b21\u306e\u3088\u3046\u306b\u5165\u529b\u3057\u3066wscat\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true \" >$ npm install -g wscat<\/pre>\n<pre><code>\/home\/ec2-user\/.nvm\/versions\/node\/v13.12.0\/bin\/wscat -> \/home\/ec2-user\/.nvm\/versions\/node\/v13.12.0\/lib\/node_modules\/wscat\/bin\/wscat\r\n+ wscat@4.0.0\r\nadded 11 packages from 8 contributors in 1.243s<\/code><\/pre>\n<p><span id=\"S5\"><\/span><\/p>\n<h4>Nginx\u8a2d\u5b9a\u3068\u78ba\u8a8d<\/h4>\n<p><i class=\"fas fa-quote-left fa-2x\"><\/i><span>\u63a5\u7d9a\u3092HTTP\u304b\u3089WebSocket\u306bUpgrade\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u308bHTTP\u306f\u3001Upgrade\u304a\u3088\u3073Connection\u30d8\u30c3\u30c0\u30fc\u3092\u4f7f\u7528\u3059\u308b\u3002Nginx\u304c\u30ea\u30d0\u30fc\u30b9\u30d7\u30ed\u30ad\u30b7\u30fc\u30b5\u30fc\u30d0\u30fc\u3068\u3057\u3066WebSocket\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u3068\u304d\u3001\u6b21\u306e\u8ab2\u984c\u304c\u3042\u308b\u3002a)WebSocket\u304chop\u2011by\u2011hop\u30d7\u30ed\u30c8\u30b3\u30eb\u3067\u3042\u308b\u305f\u3081\u3001\u30d7\u30ed\u30ad\u30b7\u30b5\u30fc\u30d0\u30fc\u304c\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u306eUpgrade\u8981\u6c42\u3092\u4ef2\u4ecb\u3059\u308b\u3068\u304d\u3001\u9069\u5207\u306a\u30d8\u30c3\u30c0\u30fc\u3092\u542b\u3081\u3066\u3001\u72ec\u81ea\u306eUpgrade\u8981\u6c42\u3092\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u30b5\u30fc\u30d0\u30fc\u306b\u9001\u4fe1\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002b)HTTP\u3067\u4f7f\u7528\u3055\u308c\u308b\u4e00\u822c\u7684\u306a\u77ed\u3044\u63a5\u7d9a\u3068\u306f\u7570\u306a\u308a\u3001WebSocket\u306e\u63a5\u7d9a\u306f\u7d99\u7d9a\u3057\u305f\u63a5\u7d9a\u3067\u3042\u308b\u305f\u3081\u3001\u30ea\u30d0\u30fc\u30b9\u30d7\u30ed\u30ad\u30b7\u306f\u3053\u308c\u3089\u306e\u63a5\u7d9a\u3092\u958b\u3044\u305f\u307e\u307e\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\nNGINX\u306f\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3068\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u30b5\u30fc\u30d0\u30fc\u306e\u9593\u306b\u30c8\u30f3\u30cd\u30eb\u3092\u8a2d\u5b9a\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3067\u3001WebSocket\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u308b\u3002 NGINX\u304c\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u30b5\u30fc\u30d0\u30fc\u306b\u30a2\u30c3\u30d7\u30b0\u30ec\u30fc\u30c9\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3059\u308b\u306b\u306f\u3001\u6b21\u306e\u4f8b\u306e\u3088\u3046\u306b\u3001Upgrade\u30d8\u30c3\u30c0\u30fc\u3068Connection\u30d8\u30c3\u30c0\u30fc\u3092\u660e\u793a\u7684\u306b\u8a2d\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u30ea\u30af\u30a8\u30b9\u30c8\u306eUpgrade\u30d8\u30c3\u30c0\u30fc\u304c &#8221;\u306b\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306bConnection\u30d8\u30c3\u30c0\u30fc\u304cclose\u8a2d\u5b9a\u3055\u308c\u308b\u3088\u3046\u306b\u3001map\u30d6\u30ed\u30c3\u30af\u3092\u8ffd\u52a0\u3057\u3066\u3044\u308b\u3002<\/span><i class=\"fas fa-quote-right\"><\/i><\/p>\n<pre class=\"lang:default decode:true \" >http {\r\n    map $http_upgrade $connection_upgrade {\r\n        default upgrade;\r\n        '' close;\r\n    }\r\n \r\n    upstream websocket {\r\n        server \uff11\uff12\uff17.0.0.1:8010;\r\n    }\r\n \r\n    server {\r\n\r\n        location \/ {\r\n            proxy_pass http:\/\/websocket;\r\n            proxy_http_version 1.1;\r\n            proxy_set_header Upgrade $http_upgrade;\r\n            proxy_set_header Connection $connection_upgrade;\r\n            proxy_set_header Host $host;\r\n        }\r\n    }\r\n}<\/pre>\n<p>\u30b5\u30fc\u30d0\u30fc\u306eserver.js\u3068\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306ewscat\u9593\u306e\u901a\u4fe1\u3092\u78ba\u8a8d\u3059\u308b\u3002<\/p>\n<table>\n<tr>\n<th width=\"50%\">\u30b5\u30fc\u30d0\u30fc<\/th>\n<th width=\"50%\">\u30af\u30e9\u30a4\u30a2\u30f3\u30c8<\/th>\n<\/tr>\n<tr>\n<td>$ node server.js<br \/>\nServer started<br \/>\nReceived from client: Hello<br \/>\nReceived from client: How are you<\/td>\n<td>$ wscat &#8211;connect ws:\/\/127.0.0.1:8010<br \/>\nConnected (press CTRL+C to quit)<br \/>\n> Hello<br \/>\n< Server received from client: Hello\n> How are you<br \/>\n< Server received from client: How are you<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Views: 74 \u76ee\u6b21 NGINX\u3092WebSocket Proxy\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b node.js\u3092Amazon EC2\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb ws\u3092WebSocket\u30b5\u30fc\u30d0\u30fc\u7528\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b wscat\u3092Web [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":3345,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=\/wp\/v2\/posts\/3334"}],"collection":[{"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3334"}],"version-history":[{"count":39,"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=\/wp\/v2\/posts\/3334\/revisions"}],"predecessor-version":[{"id":3389,"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=\/wp\/v2\/posts\/3334\/revisions\/3389"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=\/wp\/v2\/media\/3345"}],"wp:attachment":[{"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}