端口转发
# 端口转发
进入树莓派的宝塔管理面板,添加一个网站,绑定域名。
>i 注意域名后要带上端口号

以上配置完成后,通过浏览器访问http://pi.jidongyun.cn:7601
即可访问树莓派上的网站。
要想通过http://pi.jidongyun.cn这样的方式来访问,必须实现端口转发。
在远程服务器上将7600端口转发到80端口。
在nigix中添加如下配置:
```js
##通过Nginx反向代理进行端口转发
#Demo1负载均衡池
upstream frp_http_1{
server 127.0.0.1:7600;
}
#Demo1端口转发
server {
listen 80;
server_name pi.jidongyun.cn;
access_log /www/wwwlogs/access.log;
#将所有请求转发给frp_http池的应用处理
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://frp_http_1;
}
}
```
保存配置后,重载配置即可。
参考教程:[通过Nginx反向代理进行端口转发](https://lss0555.blog.csdn.net/article/details/81195291)
阿里云服务器端Nginx1.19.8完整配置如下。
其中包括端口转发、缓存处理、跨域处理等。
```
user www www;
worker_processes auto;
error_log /www/wwwlogs/nginx_error.log crit;
pid /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http
{
include mime.types;
#include luawaf.conf;
include proxy.conf;
default_type application/octet-stream;
server_names_hash_bucket_size 512;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
server_tokens off;
access_log off;
##通过Nginx反向代理进行端口转发
#树莓派负载均衡池
upstream frp_http_raspberry{
server 127.0.0.1:7600;
}
#树莓派端口转发
server {
listen 80;
server_name *.goldarts.cn *.feiyunjs.com bpi.jidongyun.cn;
access_log /www/wwwlogs/access.log;
#将所有请求转发给frp_http池的应用处理
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://frp_http_raspberry;
#处理缓存:
#https://blog.csdn.net/zhangjunli/article/details/80436489
#反向代理缓存设置命令
proxy_cache cache_one;
#设置指定状态码的缓存时间
#proxy_cache_valid 200 304 301 302 2h;
#proxy_cache_valid any 2h;
#expires 1d;
add_header X-Cache $upstream_cache_status;
proxy_cache_key $uri$is_args$args;
#处理跨域(折磨了很久):
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 200;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
}
#处理缓存:
location ~ /purge(/.*) {
allow 127.0.0.1;
deny all;
proxy_cache_purge cache_one $1$is_args$args;
}
}
server
{
listen 888;
server_name phpmyadmin;
index index.html index.htm index.php;
root /www/server/phpmyadmin;
location ~ /tmp/ {
return 403;
}
#error_page 404 /404.html;
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /\.
{
deny all;
}
access_log /www/wwwlogs/access.log;
}
include /www/server/panel/vhost/nginx/*.conf;
}
```
## connection refused错误处理
若访问域名http://pi.jidongyun.cn的时候,网页提示“The page you requested was not found”,同时树莓派ssh客户端报connection refused错误,这是提示连接被拒绝。
出现这种问题,百度并没有解决方案。
正确的处理方法,是**检查宝塔的网站域名绑定和端口设置**。
添加域名+端口号,并在宝塔防火墙放行该端口号即可。
>i 通常添加端口号以后,宝塔会自动放行该端口。

# 参考教程
[内网穿透frp服务搭建详细教程与web项目使用实例](https://blog.csdn.net/qq_38011415/article/details/88765168)
[利用 frp 进行内网穿透](https://mritd.com/2017/01/21/use-frp-for-internal-network-wear/)
[树莓派 + frp](https://blog.csdn.net/weixin_40973138/article/details/103222901)
[借助 frp 随时随地访问自己的树莓派](https://www.jianshu.com/p/b31ad0a5e676)
# 商业隧道
[SAKURA FRP](https://www.natfrp.com/),需实名认证
[量子互联](https://www.nsloop.com/)