山东大学创新实训第十五周周报------端口转发实现前后端交互

首先,联系服务器管理员创建了一个新的docker容器开放端口443、80等(端口号不限定)

这里整理了服务器常见端口:

21端口:FTP 文件传输服务
22端口:SSH协议、SCP(文件传输)、端口号重定向
23/tcp端口:TELNET 终端仿真服务
25端口:SMTP 简单邮件传输服务
53端口:DNS 域名解析服务
69/udp:TFTP
80/8080/3128/8081/9098端口:HTTP协议代理服务器
110/tcp端口:POP3(E-mail)
119端口:Network 
123端口:NTP(网络时间协议)
135、137、138、139端口: 局域网相关默认端口,应关闭
161端口:SNMP(简单网络管理协议)
389端口:LDAP(轻量级目录访问协议)、ILS(定位服务)
443/tcp 443/udp:HTTPS服务器
465端口:SMTP(简单邮件传输协议)
873端口:rsync
1080端口:SOCKS代理协议服务器常用端口号、QQ
1158端口:ORACLE EMCTL
1433/tcp/udp端口:MS SQL*SERVER数据库server、MS SQL*SERVER数据库monitor
1521端口:Oracle 数据库
2100端口:Oracle XDB FTP服务
3389端口:WIN2003远程登录
3306端口:MYSQL数据库端口
5432端口:postgresql数据库端口
5601端口:kibana
6379端口:Redis数据库端口
8080端口:TCP服务端默认端口、JBOSS、TOMCAT、Oracle XDB(XML 数据库)
8081端口:Symantec AV/Filter for MSE
8888端口:Nginx服务器的端口
9000端口:php-fpm
9080端口:Webshpere应用程序
9090端口:webshpere管理工具
9200端口:Elasticsearch服务器端口
10050端口:zabbix_server 10050
10051端口:zabbix_agent
11211端口:memcache(高速缓存系统)
27017端口:mongoDB数据库默认端口
22122端口:fastdfs服务器默认端口

在这个全新的docker容器内使用nginx配置ssl证书:

#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;
}

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;

    server {
        listen 80;
        server_name www.aiguider666.asia;

        # 将所有HTTP请求重定向到HTTPS
        return 301 https://$server_name$request_uri;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #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   xxx.xxx.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;
        #}
    }

    server {
        listen 443 ssl;
        server_name www.xxx.asia;

        ssl_certificate  /root/xxx/nginx/conf/cert/xxx.xxx_bundle.pem;
        ssl_certificate_key  /root/xxx/nginx/conf/cert/xxx.xxx.key;证书位置

        ssl_session_cache    shared:SSL:1m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_session_timeout  5m;
        ssl_prefer_server_ciphers  on;

        location / {
            proxy_pass http://xxx.xxx.xxx.xxx:端口号;
            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_set_header X-Forwarded-Proto https;
            #root   html;
            #index  index.html index.htm;
        }
    }
}

配置完成后,很容易出现一个问题就是,一定要记得关梯子!!!!!这里我们排查了很久很久才发现原来是梯子的问题!!因为梯子会导致DNS无法正常解析。比如当你 python 版本低于 3.10 时,打开了梯子然后 pip 安装库也会报出 ssl 错误 ,因为Python 版本低于 3.10 的某些版本,特别是 Python 2.x 和 3.x 的早期版本,可能使用了过时的 SSL/TLS 库。这些库可能不支持某些现代的 SSL/TLS 协议或加密算法,导致 SSL 错误。

配置成功如图:

现在面临的问题是,我想要使用在原来的docker容器内的大模型环境,有两种方法。

方法一:

sudo apt-get install autossh 
autossh -M 0 -f -N -L 8443:localhost:8443 -p 20007 root@127.0.0.1

1. 安装 autossh

sudo apt-get install autossh
  • sudo: 以超级用户(管理员)权限运行命令。
  • apt-get install autossh: 使用包管理器 apt-get 安装 autossh 工具。

2. 使用 autossh 创建 SSH 隧道

autossh -M 0 -f -N -L 8443:localhost:8443 -p 20007 root@127.0.0.1
  • autossh: 自动重启 SSH 会话的工具。它会监视并保持 SSH 隧道的连接状态。
  • -M 0: 禁用 autossh 的监控端口。通常 autossh 会使用一个额外的端口来监控 SSH 连接的状态,但通过 -M 0 可以禁用这个功能。
  • -f: 将 autossh 进程放到后台运行。
  • -N: 不执行远程命令。此选项告诉 SSH 客户端只进行端口转发,而不在远程服务器上执行任何命令。
  • -L 8443:localhost:8443: 设置本地端口转发。将本地主机的 8443 端口转发到远程服务器的 8443 端口。具体解释如下:
    • 8443: 本地计算机的端口。
    • localhost: 目标主机地址,在这个上下文中是指远程服务器自身。
    • 8443: 远程服务器的端口。
  • -p 20007: 指定用于连接到远程服务器的 SSH 端口号为 20007。默认情况下,SSH 使用端口 22,但在这里是使用了非标准端口 20007。
  • root@127.0.0.1: 这是用于连接到远程服务器的登录凭证,其中 root 是用户名,127.0.0.1 是远程服务器的主机名或 IP 地址(在这个情况下是本地地址)。

但由于该方法可能会对原来的容器造成不可预估的损坏,故舍弃。

方法二:

使用docker进行请求中转:

client -> 宿主机 -> tmp 容器(nginx 反向代理)-> a100 容器

  • 客户端请求 (client):客户端发送请求到宿主机的端口 80。
  • 宿主机 (host):宿主机通过 Docker 将请求转发到 tmp 容器。
  • tmp 容器 (nginx 反向代理):nginx 接收到请求,并根据配置将请求代理到 a100 容器。
  • a100 容器a100 容器处理请求并返回响应。

最终在a100容器内进行后端请求,前端响应成功如图:

在微信小程序中写一个页面调用该接口:

const app = getApp();

Page({
  data: {
    message: ''
  },

  onLoad: function() {
    this.getStringFromBackend();
  },

  getStringFromBackend: function() {
    const that = this;
    wx.request({
      url: 'https://www.aiguider666.asia/get_string',
      method: 'GET',
      success: function(res) {
        if (res.statusCode === 200) {
          that.setData({
            message: res.data.message
          });
        } else {
          console.error('请求失败:', res);
        }
      },
      fail: function(err) {
        console.error('请求失败:', err);
      }
    });
  }
});

成功如图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值