Eyeeshot BloG

ELB Websocket 설정 문제 본문

Tech

ELB Websocket 설정 문제

eyeeshot 2020. 12. 3. 12:03

ELB 상황에서 Websocket 을 사용하려면 문제가 몇가지 있다 우선 ELB 에 proxy 설정을 통해 websocket 연결이 가능하게 할수 있다. 링크
하지만 이렇게 연결하더라고 STOMP + SockJS 로 사용하려면 during WebSocket handshake: Unexpected response code: 400 에러가 난다.
해당 오류는 Handshake failed due to invalid Upgrade header 오류로 헤더 정보에 upgrade 정보가 없어서 나오는 에러이다.
ELB proxy policy 는 proxy만 가능하고 헤더정보는 여전히 넘어오지 않는다. 따라서 우회하는 방법으로 
ELB -> nginx -> application 처리로 테스트 해보았다.

nginx는 docker-compose 로 아래와 같이 설정하였다.

docker-compose.yml

version: '3'
services:
  proxy:
    image: nginx:latest
    ports:
      - "80:80"
      - "443:443"
      - "9999:9999"
      - "5400:5400"
    volumes:
      - ./proxy/nginx.conf:/etc/nginx/nginx.conf
  web:
    image: nginx:latest
    expose:
      - "8080"
    volumes:
      - ./source:/source
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
  php:
    image: php:7.3-fpm
    expose:
      - "9000"
    volumes:
      - ./source:/source

web, php 는 해당 docker 정상 동작 확인용으로 같이 올렸다.

 

nginx.conf

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
#    include       /etc/nginx/mime.types;
#    default_type  application/octet-stream;
#    upstream docker-nginx {
#        server web:8080;
#    }

    server {
        listen 5400;
        server_name localhost;

        location / {
                proxy_pass http://{설정할 IP}:35400;
        }
    }

    server {
        listen 80;
        server_name localhost;
        location / {
            proxy_pass         http://{설정할 IP}:35100;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location /ws/ {
                proxy_pass http://{설정할 IP}:35100;
                proxy_http_version 1.1;
                proxy_set_header Upgrade "websocket";
                proxy_set_header Connection "Upgrade";
        }
    }

    server {
        listen 443;
        server_name _;
        location /ws/ {
                proxy_pass http://{설정할 IP}:35100;
                proxy_http_version 1.1;
                proxy_set_header Upgrade "websocket";
                proxy_set_header Connection "Upgrade";
        }

        location / {
                proxy_pass http://{설정할 IP}:35100;
                proxy_redirect     off;
                proxy_set_header   Host $host;
                proxy_set_header   X-Real-IP $remote_addr;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }


    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                      'upgrade - "$http_upgrade"'
                      'header - "$upstream_http_custom_header"';
    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}

아래 코드 부분이 프록시로 전달되면서 헤더 정보를 추가 입력해 전달하게된다

proxy_pass http://{설정할 IP}:35100;
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";

이렇게하면  Handshake failed due to invalid Upgrade header 오류가 더이상 안나게된다..
더 좋은방법이 있을꺼 같지만...현재로서는 찾은방법은 여기까지이다.

참고 문서 : https://www.joinc.co.kr/w/man/12/nginx/wsproxy