2017-09-04 5 views
0

SSL을 통해 socketio를 nginx로 설정하려고합니다. 문제는 내가 연결할 클라이언트를 얻을 수 있지만 그 소켓을 통해 올 것으로 예상되는 다른 이벤트를 볼 수 없다는 것입니다.SocketIO with Nginx with SSL

import openSocket from "socket.io-client"; 
const socket = openSocket(`${SOCKET}`); 

function subscribeToTimer(callBack) { 
    socket.on("timer", timestamp => callBack(null, timestamp)); 
    socket.emit("subscribeToTimer", 1000); 
} 

export class App extends Component { 
    constructor(props) { 
    super(props); 
    this.store = this.configureStore(); 
    subscribeToTimer((err, action) => this.store.dispatch(action)); 
    } 

서버 :

클라이언트 코드는 여기에 있습니다 : (주이 내 프로덕션 서버에서 로컬 작업을 바로하지 않습니다)

: nginx를 /etc/nginx/sites-enabled/default에 의해 관리되는

const port = 8000 
const io = require('socket.io')() 

io.on("connection", (client) => { 
    console.log("a user connected") 
    client.on("subscribeToTimer", (interval) => { 
    console.log("a user is subscribing to timer with interval: ", interval) 
    setInterval(() => { 
     timestamp = new Date() 
     client.emit('timer', { type: 'SET_TIME', payload: timestamp }); 
    }, interval); 
    }); 
}) 

io.listen(port) 
console.log('listening on port ', port) 

server { 
    <snip> 
    location /socket.io { 
    proxy_pass http://localhost:8000; 
    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; 
    } 
} 

서버를 시작하면 다음과 같이 나타납니다.

listening on port 8000 
a user connected 

따라서 클라이언트가 서버에 연결하고 있지만 subscribeToTImer 이벤트가 표시되지 않습니다.

여기에 대한 통찰력이 있으십니까?

+1

개발자 콘솔에있는 항목은 무엇입니까? –

+0

@ TarunLalwani 클라이언트 콘솔에는 아무 것도 없었습니다. 나는 실제로 이것을 알아 냈으므로 대답으로 업데이트 할 것이다. – user341493

답변

1

문제는 아마 때문에 두 가지 이유이다. 하나는 호스트 헤더를 사용하고 하나 대신 127.0.0.1

server { 
    <snip> 
    location /socket.io { 
    proxy_pass http://127.0.0.1:8000; 
    proxy_http_version 1.1; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection 'upgrade'; 
    proxy_cache_bypass $http_upgrade; 
    } 
} 

나는 근본 원인을 잘 100 % 아니라고하지만 소켓 다른 문제에 도움이되었습니다 Host 제거하고 대신 localhost127.0.0.1을 사용하여 본의 로컬 호스트 사용 .io 이전에

0

구성의 proxy_pass 줄에 문제가 있음이 밝혀졌습니다. 서버의 명명 된 그룹으로 upstream 섹션을 만든 다음 proxy_pass (아니 내가 http://localhost... 내가 그것을 가진 방법)에 참조하십시오.

작업 설정 /etc/nginx/sites-enabled/default는 :

upstream socket_nodes { 
    ip_hash; 
    server 127.0.0.1:8000; 
} 

server { 

    <-- snip --> 

    location /socket.io { 
    proxy_pass http://socket_nodes; 
    proxy_http_version 1.1; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection 'upgrade'; 
    proxy_set_header Host $host; 
    } 
} 
+0

이것이 단일 업스트림 서버와 다른 점이 있는지 확실하지 않다. 대신'proxy_pass http : //127.0.0.1 : 8000;'을 사용할 수 있습니까? 'location/socket.io'에 직접 입력하십시오 –

+0

@TarunLalwani는'localhost'와 달리 nginx가'127.0.0.1'을 다루고 있습니까? 그것은 localhost와 함께 일하고 있었으므로 ... 어쨌든, 나는 오늘 밤 시험을 볼 수 있어야합니다. – user341493

+0

@ TarunLalwani 네 말이 맞아. 127.0.0.1:8000이 작동합니다. 감사! – user341493