按照此链接配置Nginx click here 尝试通过HTTPS连接WebSocket(socketo.me),但未成功,则报错为
WebSocket打开握手超时
由于我的托管服务器的ngnix(版本:1.13.8)已配置为在前端以反向代理模式工作。这是Nginx的配置
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server xx.xxx.xxx.x:8282; #External IP address
}
server {
location / {
proxy_pass http://xx.xxx.xxx.x:8080; #External IP address
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 120s;
}
}
}
chatroom.php
<script type="text/javascript">
$(document).ready(function(){
var conn = new WebSocket('ws://xx.xxx.xxx.x:8282');
conn.onopen = function(e) {
console.log("Connection established!");
};
conn.onmessage = function(e) {
console.log(e.data);
...
};
conn.onclose = function(e) {
console.log("Connection Closed!");
}
})
</script>
server.php
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8282
);
$server->run();
在应用SSL之前,
如下所示,从终端通过端口#8282建立WebSocket连接
root@user:/var/www/vhosts/somedomain.xy/httpdocs/chatroom-php-mysql/bin# php server.php
Server Started.
New connection! (84)
Connection 84 has disconnected
基本上在浏览器中打开网站URL时,会发生以下情况:
客户端请求到达前端Nginx,要求提供一些资源(.html页面,.php页面,图像,javascript等)。我们托管服务器中的Nginx可在以下TCP端口上运行:80-http,443-https。
Nginx检查其缓存中是否已经有资源。
如果已缓存资源,则Nginx返回已缓存的内容。
如果未缓存资源或请求了动态页面(例如index.php),则Nginx将请求转发(转发)到后端服务器-Apache。我们的托管服务器中的Apache可在以下TCP端口上运行:7080-http,7081-https。然后Nginx缓存静态内容-HTML,图像,js,css。
符号链接已在中创建
/etc/nginx/plesk.conf.d/vhosts
中的somedomain.xy.conf
#ATTENTION!
#
#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,
#SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.
server {
listen xx.xxx.xxx.x:443 ssl http2;
server_name somedomain.xy;
server_name www.somedomain.xy;
server_name ipv4.somedomain.xy;
ssl_certificate /opt/psa/var/certificates/scfPsMGvJ;
ssl_certificate_key /opt/psa/var/certificates/scfPsMGvJ;
ssl_client_certificate /opt/psa/var/certificates/scfSdpTzN;
client_max_body_size 128m;
root "/var/www/vhosts/somedomain.xy/httpdocs";
access_log "/var/www/vhosts/system/somedomain.xy/logs/proxy_access_ssl_log";
error_log "/var/www/vhosts/system/somedomain.xy/logs/proxy_error_log";
#extension letsencrypt begin
location /.well-known/acme-challenge/ {
root /var/www/vhosts/default/htdocs;
types { }
default_type text/plain;
satisfy any;
auth_basic off;
allow all;
location ~ ^/\.well-known/acme-challenge.*/\. {
deny all;
}
}
#extension letsencrypt end
location / {
proxy_pass https://xx.xxx.xxx.x:7081;
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 X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location /internal-nginx-static-location/ {
alias /var/www/vhosts/somedomain.xy/httpdocs/;
internal;
}
location ~ ^/(plesk-stat|awstats-icon|webstat|webstat-ssl|ftpstat|anon_ftpstat) {
proxy_pass https://xx.xxx.xxx.x:7081;
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 X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location ~ ^/proj_ci/ {
proxy_pass https://xx.xxx.xxx.x:7081;
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 X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location ~ "^/files/" {
proxy_pass https://xx.xxx.xxx.x:7081;
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 X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
add_header X-Powered-By PleskLin;
}
server {
listen xx.xxx.xxx.x:80;
server_name somedomain.xy;
server_name www.somedomain.xy;
server_name ipv4.somedomain.xy;
client_max_body_size 128m;
root "/var/www/vhosts/somedomain.xy/httpdocs";
access_log "/var/www/vhosts/system/somedomain.xy/logs/proxy_access_log";
error_log "/var/www/vhosts/system/somedomain.xy/logs/proxy_error_log";
#extension letsencrypt begin
location /.well-known/acme-challenge/ {
root /var/www/vhosts/default/htdocs;
types { }
default_type text/plain;
satisfy any;
auth_basic off;
allow all;
location ~ ^/\.well-known/acme-challenge.*/\. {
deny all;
}
}
#extension letsencrypt end
location / {
proxy_pass http://xx.xxx.xxx.x:7080;
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 X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location /internal-nginx-static-location/ {
alias /var/www/vhosts/somedomain.xy/httpdocs/;
internal;
}
location ~ ^/(plesk-stat|awstats-icon|webstat|webstat-ssl|ftpstat|anon_ftpstat) {
proxy_pass http://xx.xxx.xxx.x:7080;
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 X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location ~ ^/proj_ci/ {
proxy_pass http://xx.xxx.xxx.x:7080;
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 X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location ~ "^/files/" {
proxy_pass http://xx.xxx.xxx.x:7080;
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 X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
add_header X-Powered-By PleskLin;
}
我曾尝试在/etc/nginx/conf.d
中创建文件名app_name.conf
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server xx.xxx.xxx.x:8282;
}
server {
# listen xx.xxx.xxx.x:80;
# listen 443 default_server ssl;
listen 443 ssl http2;
server_name somedomain.xy;
location / {
proxy_pass http://xx.xxx.xxx.x:8282;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
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_read_timeout 120s;
proxy_read_timeout 86400;
# proxy_redirect default;
# proxy_redirect http://xx.xxx.xxx.x:8282/ /;
# proxy_redirect http://www.somedomain.xy/ /;
}
location /chat/ {
proxy_pass http://xx.xxx.xxx.x:8282;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 120s;
}
location /test {
rewrite ^/test(.*) $1 break;
proxy_pass http://127.0.0.1:8282;
}
location /wss {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Proxy "";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://xx.xxx.xxx.x:8282;
proxy_read_timeout 120s;
}
location /websocket {
proxy_pass http://xx.xxx.xxx.x:8282; ## WSPHP listening port
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 86400;
}
}
此外,在Nginx中,我们看不到它们是/etc/nginx/sites-available/*
和/etc/nginx/sites-enabled/*
这样的目录,
我们将在/etc/apache2