Nginx 配置 ttyd 实现 Web 终端访问
Nginx 配置 ttyd 实现 Web 终端访问
本文将带您了解如何使用 Nginx 配置 ttyd 实现 Web 终端访问,并提供一个完整的配置示例,帮助您解决 Nginx 跳转到 ttyd 失败的问题。
问题描述:
您可能遇到过在配置 Nginx 的 ttyd 配置后,通过 Nginx 跳转到 ttyd 时出现 404 错误,无法连接上 ttyd 的情况。
解决方法:
下面是一个正确的 Nginx 的 ttyd 配置示例,包含了必要的配置选项,以确保 Nginx 正确地代理到 ttyd 服务器:
location ~ ^/ugreen/v1/ttyd {
proxy_pass http://localhost:7681;
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_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 86400;
proxy_redirect off;
}
配置说明:
proxy_pass http://localhost:7681;: 将请求代理到运行在本地主机 7681 端口的 ttyd 服务器。proxy_http_version 1.1;: 设置代理协议版本为 1.1,以便支持 WebSocket 连接。proxy_set_header Upgrade $http_upgrade;: 将 Upgrade 头部信息传递到 ttyd 服务器。proxy_set_header Connection 'upgrade';: 将 Connection 头部信息设置为 'upgrade',以指示这是一个 WebSocket 连接。proxy_set_header Host $host;: 将 Host 头部信息传递到 ttyd 服务器。proxy_set_header X-Real-IP $remote_addr;: 将客户端真实 IP 地址传递到 ttyd 服务器。proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;: 将代理信息传递到 ttyd 服务器。proxy_read_timeout 86400;: 设置代理读取超时时间为 86400 秒。proxy_redirect off;: 关闭代理重定向功能。
注意事项:
- 确保 ttyd 服务器已成功启动并监听在指定的端口号 (在本例中为 7681)。
- 调整
proxy_pass指令中的地址和端口号以匹配您的 ttyd 服务器地址和端口号。 - 使用
proxy_read_timeout指令来调整代理读取超时时间,以避免连接断开。
其他:
您还可以根据需要添加其他 Nginx 配置指令,例如 location 块中可以添加其他匹配规则,以实现更复杂的路由规则。
通过以上配置,您可以通过 Web 浏览器访问 Nginx 的代理地址,并实现对 ttyd 的 Web 终端访问。
原文地址: https://www.cveoy.top/t/topic/fNCm 著作权归作者所有。请勿转载和采集!