记录日常工作关于系统运维,虚拟化云计算,数据库,网络安全等各方面问题。

   nginx使用 TCP代理sftp,实现访问目录文件

本文章向大家介绍nginx代理sftp,主要包括nginx代理sftp使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

最近需要使用一个sftp协议的代理服务器,查了一下nginx1.9之后已经支持了,尝试一下:

The ngx_stream_core_module module is available since version 1.9.0. This module is not built by default, it should be enabled with the --with-stream configuration parameter.

nginx从1.9.0版本开始,新增了ngx_stream_core_module模块。默认编译的时候该模块并未编译进去,需要编译的时候添加--with-stream,使其支持stream代理。

[root@baseline opt]# / http:---- ::--  http:正在解析主机 nginx.org (nginx.org)... :1af8::a004:::e3, ., .|:1af8::a004:::e3|: (890K) [application/octet--..%[================================================================================================================================>] ,      357KB/s 用时 -- :: ( KB/s) - 已保存 “nginx-...gz” [/ -xvf nginx-...gz
......

  [root@baseline nginx]# cd nginx-1.10.3/
  [root@baseline nginx-1.10.3]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module

编译检查,我这没有缺失(如果有缺,却失什么装什么)

 

  [root@baseline nginx-1.10.3]#make

  ......

    make[1]: 离开目录“/opt/nginx/nginx-1.10.3”

  #安装

  [root@baseline nginx-1.10.3]#make install

  [root@baseline nginx-1.10.3]# cd /usr/local/nginx/

  ##启动nginx

  [root@baseline nginx]# ./sbin/nginx

  我在我的window装了一个freeSSHd  作为sftp服务器。配置如下:


然后启动


 配置好sftp之后,测试一下:

[root@baseline nginx]# sftp -P 21  mysftp@192.168.1.180 The authenticity of host '[192.168.1.180]:21 ([192.168.1.180]:21)' can't be established.RSA key fingerprint is SHA256:iM1dwfz+JzZrvmiYbmH3tS3F8ad1wutYxFWtnv8BWu8.
RSA key fingerprint is MD5:5b:1f:b4:99:1c:b4:4d:24:05:a5:16:79:4d:68:3b:7f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.1.180]:21' (RSA) to the list of known hosts.
mysftp@192.168.1.180's password: Permission denied, please try again.
mysftp@192.168.1.180's password: Connected to 192.168.1.180.
sftp> ls

之后修改nginx配置文件 nginx.conf:

在http节点上添加紫色代码:

events {
    worker_connections  1024;
}stream { 
    upstream sftp { 
    hash $remote_addr consistent; 
    server 192.168.1.180:22 max_fails=3 fail_timeout=60s; 
    }
    server { 
        listen 90; #端口可以自己定义
        proxy_connect_timeout 60s; 
        proxy_timeout  30s; 
        proxy_pass sftp; 
    } 
}http {。。。。。。。。。

上述代码意思:nginx 在90端口监听 tcp 并转发请求到upstream 为sftp的主机即 192.168.1.180:22

主机互信,实现免密码登陆

修改之后  nginx -s reload 一下

再试:

[root@baseline nginx]# sftp -P 90  mysftp@192.168.1.19The authenticity of host '[192.168.1.19]:90 ([192.168.1.19]:90)' can't be established.RSA key fingerprint is SHA256:iM1dwfz+JzZrvmiYbmH3tS3F8ad1wutYxFWtnv8BWu8.
RSA key fingerprint is MD5:5b:1f:b4:99:1c:b4:4d:24:05:a5:16:79:4d:68:3b:7f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.1.19]:90' (RSA) to the list of known hosts.
mysftp@192.168.1.19's password: Connected to 192.168.1.19.
sftp> ls
ccc.txt          ccd.txt          confirmPlat.py   file

ok




转载请标明出处【nginx使用 TCP代理sftp,实现访问目录文件】。

《www.micoder.cc》 虚拟化云计算,系统运维,安全技术服务.

网站已经关闭评论