nginx配置同時支援https/wss協議(http/https ws/wss)都可以
阿新 • • 發佈:2018-12-15
在Nginx代理叢集支援SSL。整體架構如下:
SSL
SSL(Secure Socket Layer,安全套接層) 簡單來說是一種加密技術, 通過它, 我們可以在通訊的雙方上建立一個安全的通訊鏈路, 因此資料互動的雙方可以安全地通訊, 而不需要擔心資料被竊取. 關於 SSL 的深入知識, 可以看這篇文章: SSL/TLS協議執行機制的概述
WSS
WSS 是 Web Socket Secure 的簡稱, 它是 WebSocket 的加密版本. 我們知道 WebSocket 中的資料是不加密的, 但是不加密的資料很容易被別有用心的人竊取, 因此為了保護資料安全, 人們將 WebSocket 與 SSL 結合, 實現了安全的 WebSocket 通訊, 即 WebSocket Secure. 所以說 WSS 是使用 SSL 進行加密了的 WebSocket 通訊技術.
HTTPS
其實 HTTPS 和 WSS 類似, HTTP 之於 HTTPS 就像 WebSocket 之於 WebSocket Secure. HTTP 協議本身也是明文傳輸, 因此為了資料的安全性, 人們利用 SSL 作為加密通道, 在 SSL 之上傳遞 HTTP 資料, 因此 SSL 加密通道上執行的 HTTP 協議就被稱為 HTTPS 了.
總結
SSL 是基礎, 在 SSL 上執行 WebSocket 協議就是 WSS; 在 SSL 上執行 HTTP 協議就是 HTTPS.
nginx配置(給出全部配置)
window/linux通用
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; accept_mutex on; #設定網路連線序列化,防止驚群現象發生,預設為on multi_accept on; #設定一個程序是否同時接受多個網路連線,預設為off #use epoll; #事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream runx { server 192.168.51.247; } server { listen 808; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #location / { # root html; # index index.html index.htm; #} location / { proxy_connect_timeout 15s; proxy_send_timeout 15s; proxy_read_timeout 30m; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Connection ""; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://runx; #請求轉向runx 定義的伺服器列表 client_max_body_size 1024m; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # server { listen 443 ssl; server_name socket.square.huoxinghy.com;//域名配置 ssl_certificate C:\Users\ASDFGJA\Desktop\215061298600605\215061298600605.pem;//證書路徑linux中可以放在 /etc/nginx/ssl ssl_certificate_key C:\Users\ASDFGJA\Desktop\215061298600605\215061298600605.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { #root html; #index index.html index.htm; proxy_connect_timeout 15s; proxy_send_timeout 15s; proxy_read_timeout 30m; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Connection ""; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://runx; #請求轉向runx 定義的伺服器列表 client_max_body_size 1024m; } } }