nginx反向代理webSocket程序并配置SSH端口

需求背景:当前有个网页版的XShell项目(webSocket程序同理),需要使用到nginx做反向代理

XShell项目是Java开发的,端口为:9017 ,与nginx部署在同一台Linux中

  1. 检查nginx是否支持SSH

在sbin文件夹中,执行命令:

./nginx -V

查看是否出现 configure arguments: --with-stream

存在上述参数,证明支持SSH,如:

不存在上述参数,证明不支持SSH,需要配置nginx支持SSH功能

  1. 配置nginx支持SSH

找到nginx的configure文件,执行命令,安装stream功能(SSH):

./configure --with-stream

执行成功后,继续执行如下命令:

make

注意:

  1. 这里最好使用make,不能使用make install,后者会重置nginx!

  1. 这个文件是nginx压缩包解压后的,不在nginx安装路径中,若configure文件找不到了,证明nginx安装完毕后,你把解压后的nginx文件给删除了

至此,SSH功能安装完毕

重复1中的操作,检验SSH功能是否安装

  1. 配置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功能"即可

混淆点:

  1. ssh端口为22,因此在server中写成错误的写成localhost:22,而我的web程序端口为9017,因此nginx代理的只是22端口,我们的web程序根本就没有被代理,从而访问代理地址后,报404

  1. 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://nginx.p2hp.com/en/docs/stream/stream_processing.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

睡竹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值