programing tip

NGINX는 프록시 웹 소켓을 역전시키고 SSL을 활성화합니까 (wss : //)?

itbloger 2020. 7. 21. 08:05
반응형

NGINX는 프록시 웹 소켓을 역전시키고 SSL을 활성화합니까 (wss : //)?


나는 스스로 길을 잃어 버렸고 NGINX를 스스로 만들지 않았지만 추가 계층을 사용하지 않고도 안전한 웹 소켓을 사용할 수 있기를 원합니다.

웹 소켓 서버 자체에서 SSL을 활성화하고 싶지 않지만 대신 NGINX를 사용하여 SSL 레이어를 전체에 추가하고 싶습니다.

거기에있는 모든 웹 페이지는 할 수 없다고 말하지만 할 수는 있습니다! 누구 (나 자신) 덕분에 나에게 방법을 보여줄 수 있습니다!


nginx는 이제 1.3.13 릴리스에서 Websocket을 지원합니다. 사용 예 :

location /websocket/ {

    proxy_pass ​http://backend_host;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 86400;

}

nginx 변경 로그WebSocket 프록시 문서를 확인할 수도 있습니다.


용감한 Ops 프로그래머 그룹이 새로운 nginx_tcp_proxy_module을 사용하는 브랜드로 상황을 해결 했으므로 걱정하지 마십시오.

2012 년 8 월에 작성되었으므로 미래에 온다면 숙제를해야합니다.

전제 조건

CentOS를 사용한다고 가정합니다.

  • NGINX의 현재 인스턴스를 제거하십시오 (dev 서버를 사용하는 것이 가장 좋습니다)
  • 가능하면 이전 NGINX 구성 파일을 저장하여 다시 사용할 수 있도록하십시오 ( init.d/nginx스크립트 포함 )
  • yum install pcre pcre-devel openssl openssl-devel NGINX 빌드에 필요한 다른 라이브러리들
  • 가져 오기 nginx_tcp_proxy_module을 여기 GitHub의에서 https://github.com/yaoweibin/nginx_tcp_proxy_module 당신이 그것을 배치 곳 (make가 확인이 압축되지 않습니다) 폴더를 기억

새로운 NGINX 구축

다시 CentOS를 가정합니다.

  • cd /usr/local/
  • wget 'http://nginx.org/download/nginx-1.2.1.tar.gz'
  • tar -xzvf nginx-1.2.1.tar.gz
  • cd nginx-1.2.1/
  • patch -p1 < /path/to/nginx_tcp_proxy_module/tcp.patch
  • ./configure --add-module=/path/to/nginx_tcp_proxy_module --with-http_ssl_module (필요한 경우 더 많은 모듈을 추가 할 수 있습니다)
  • make
  • make install

선택 과목:

  • sudo /sbin/chkconfig nginx on

Nginx 설정

다시 사용하려면 이전 구성 파일을 먼저 복사하십시오.

중요 :tcp {} conf에서 최상위 레벨에 지시문 을 작성해야합니다 . 지시문 안에 있지 않은지 확인하십시오 http {}.

아래 구성 예는 단일 업스트림 웹 소켓 서버와 SSL 및 비 SSL에 대한 두 개의 프록시를 보여줍니다.

tcp {
    upstream websockets {
        ## webbit websocket server in background
        server 127.0.0.1:5501;

        ## server 127.0.0.1:5502; ## add another server if you like!

        check interval=3000 rise=2 fall=5 timeout=1000;
    }   

    server {
        server_name _;
        listen 7070;

        timeout 43200000;
        websocket_connect_timeout 43200000;
        proxy_connect_timeout 43200000;

        so_keepalive on;
        tcp_nodelay on;

        websocket_pass websockets;
        websocket_buffer 1k;
    }

    server {
        server_name _;
        listen 7080;

        ssl on;
        ssl_certificate      /path/to/cert.pem;
        ssl_certificate_key  /path/to/key.key;

        timeout 43200000;
        websocket_connect_timeout 43200000;
        proxy_connect_timeout 43200000;

        so_keepalive on;
        tcp_nodelay on;

        websocket_pass websockets;
        websocket_buffer 1k;
    }
}

이것은 나를 위해 일했다 :

location / {
    # redirect all HTTP traffic to localhost:8080
    proxy_pass http://localhost:8080;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    # WebSocket support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

-빌린 곳 : https://github.com/nicokaiser/nginx-websocket-proxy/blob/df67cd92f71bfcb513b343beaa89cb33ab09fb05/simple-wss.conf


SSL을 사용하는 .net 코어 2.0 Nginx 용

location / {
    # redirect all HTTP traffic to localhost:8080
    proxy_pass http://localhost:8080;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    # WebSocket support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $http_connection;
}

이것은 나를 위해 일했다


나를 위해 그것은 proxy_pass위치 설정으로 내려 왔습니다 . 을 변경할 필요에 http://nodeserverhttps://nodeserver, 사물의 노드 서버 측에서 유효한 SSL 인증서 설치가 있습니다. 그렇게하면 외부 노드 서버를 도입 할 때 IP 만 변경하면 나머지는 모두 동일한 구성으로 유지됩니다.

I hope this helps someone along the way... I was staring at the problem the whole time... sigh...

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}
upstream nodeserver {
        server 127.0.0.1:8080;
}
server {
        listen 443 default_server ssl http2;
        listen [::]:443 default_server ssl http2 ipv6only=on;
        server_name mysite.com;
        ssl_certificate ssl/site.crt;
        ssl_certificate_key ssl/site.key;
        location /horizon {
                proxy_pass https://nodeserver;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection $connection_upgrade;
                proxy_http_version 1.1;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_intercept_errors on;
                proxy_redirect off;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-NginX-Proxy true;
                proxy_ssl_session_reuse off;
            }
}

A good, concise article by Pankaj Malhotra discusses how to do this with NGINX and is available here.

The basic NGINX configuration is reproduced below:

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

upstream appserver {
    server 192.168.100.10:9222; # appserver_ip:ws_port
}

server {
    listen 8888; // client_wss_port

    ssl on;
    ssl_certificate /path/to/crt;
    ssl_certificate_key /path/to/key;


    location / {
        proxy_pass http://appserver;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

Using nginx/1.14.0

i have a websocket-server running on port 8097 and users connect from to wss on port 8098, nginx just decrypts the content and forwards it to the websocket server

So i have this config file (in my case /etc/nginx/conf.d/default.conf)

server {
    listen   8098;
        ssl on;
        ssl_certificate      /etc/ssl/certs/combined.pem;
        ssl_certificate_key  /root/domain.key;
    location / {

        proxy_pass http://hostname:8097;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400;

    }
}

참고URL : https://stackoverflow.com/questions/12102110/nginx-to-reverse-proxy-websockets-and-enable-ssl-wss

반응형