需求背景:当前有个网页版的XShell项目(webSocket程序同理),需要使用到nginx做反向代理
XShell项目是Java开发的,端口为:9017 ,与nginx部署在同一台Linux中
检查nginx是否支持SSH
在sbin文件夹中,执行命令:
./nginx -V
查看是否出现 configure arguments: --with-stream
存在上述参数,证明支持SSH,如:

不存在上述参数,证明不支持SSH,需要配置nginx支持SSH功能
配置nginx支持SSH
找到nginx的configure文件,执行命令,安装stream功能(SSH):
./configure --with-stream
执行成功后,继续执行如下命令:
make
注意:
这里最好使用make,不能使用make install,后者会重置nginx!
这个文件是nginx压缩包解压后的,不在nginx安装路径中,若configure文件找不到了,证明nginx安装完毕后,你把解压后的nginx文件给删除了
至此,SSH功能安装完毕
重复1中的操作,检验SSH功能是否安装
配置SSH(webSocket)代理
SSH代理与http代理不同,需要使用到stream块
stream块与http块的位置是平级的,注意代码书写的位置
stream块代码构成如下:
stream {
upstream ssh-proxy { #ssh-proxy 代理ssh名称
server IP地址:端口; #需要代理的地址
}
server {
listen 端口; #nginx监控的端口
proxy_pass ssh-proxy; #对应upstream中的ssh名称
}
}
由于我需要代理的XShell项目,端口为9017,且与nginx部署在同一台Linux中
因此,我的配置如下:
#ssh代理配置
stream {
upstream ssh-proxy {
server localhost:9017; #代理的web程序与nginx在同一台服务器,故使用localhost即可
}
server {
listen 80; #监听80端口
proxy_pass ssh-proxy;
}
}
配置config后,重启nginx后生效
在sbin中,执行:
./nginx -s reload
若出现错误:nginx: [emerg] unknown directive "stream"........
证明SSH功能不支持,按照步骤2配置"SSH功能"即可
混淆点:
ssh端口为22,因此在server中写成错误的写成localhost:22,而我的web程序端口为9017,因此nginx代理的只是22端口,我们的web程序根本就没有被代理,从而访问代理地址后,报404
stream块(ssh代理)具备http代理功能,因此,在http块中,不需要再配置http代理
4. 附上完整的nginx.conf
只配置了ssh代理
#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;
}
#ssh代理
stream {
upstream ssh-proxy {
server localhost:9017;
}
server {
listen 80;
proxy_pass ssh-proxy;
}
}
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;
#注意:这里不能与SSH代理冲突,配置80的SSH代理,就不需要配置该web程序的反向代理,因为SSH包含了反向代理功能
server {
listen 8081;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
index.html;
}
#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 localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.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;
# }
#}
}
nginx官网也有相关案列,地址如下:文章来源:https://www.toymoban.com/news/detail-650651.html
https://nginx.p2hp.com/en/docs/stream/stream_processing.html文章来源地址https://www.toymoban.com/news/detail-650651.html
到了这里,关于nginx反向代理webSocket程序并配置SSH端口的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!