nginx 에서 socket 연결하기

Mingtorr·2021년 4월 13일
1
post-thumbnail

server 파일에서는 아래처럼 입력해야 한다.

const express = require("express"); 
const app = express(); 
const socketIo = require("socket.io");
var http = require("http").createServer(app);
const io = socketIo(server);

client 파일에서는 아래처럼 입력해야 한다.

import io from "socket.io-client";
const socket = io();

nginx 파일에서는 location ^~ /socket 부분이 필요하다.
경로는 aws ec2 기준 /etc/nginx/site-available/your-file 이다.

server {
  listen 80;
  location / {
    root   /home/ubuntu/your_reposity/build;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
  }location /api {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:3001;
        proxy_set_header X-NginX-Proxy ture;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
        }
location ^~ /socket {
            proxy_pass http://localhost:3001;
            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;
}
}
profile
츄르 값 벌기 위해 코딩하는 아키텍쳐

0개의 댓글