nginx stream转发
- 前言
- 一、 centos 安装 nginx
- 二、nginx 配置
- 三、 nginx 启动
- 四、 访问本地 8080 端口的页面
- 五、 遇到的问题
- 4.1 页面不能正常加载
- 4.2 辅助测试
前言
为了文章通过,作如下注释: stream: 流; firewall: 防火墙;
本篇文章 主要是配合 上一篇 opn 穿透内网来使用的
通过 公网服务器 + opn 实现了 内网穿透, 然后我们通过 nginx stream转发 就可以实现 tcp stream转发,这样就可以 做到:
-
外网ip + port
访问到内网的服务(本教程实现) - 进一步的实现就是 多个域名 访问 内网不同项目
一、 centos 安装 nginx
yum install -y nginx
即可完成安装
二、nginx 配置
The ngx_stream_proxy_module module (1.9.0) allows proxying data streams over TCP, UDP (1.9.13), and UNIX-domain sockets.
官网说 ngx 可以代理 TCP,UDP…数据stream
vim /etc/nginx/nginx.conf
编辑nginx 配置文件
配置很简单, 和 ngx负载均衡配置相似, 只需要加上 steam 相关配置即可
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
stream {
server {
listen 10000;
proxy_pass 192.168.131.6:8080;
}
}
需要注意的配置错误
通过 nginx -t
显示一直报错, 后来发现, 这个多了个 http scheme。 这里是stream转发, 各种 scheme 的数据stream都会有。只需要 ip:port
即可。(upstream 负载均衡的时候也需要注意哦)
三、 nginx 启动
nginx -t
测试 nginx 配置文件是否存在语法错误,没有错误则 返回 ok
nginx -s reload
nginx 测试没有错误后, 用来启动或者重启 nginx
四、 访问本地 8080 端口的页面
可以正常访问
五、 遇到的问题
很有可能在访问 本地 8080 服务的时候,页面不能正常加载 出来, cant get any response 之类的信息
4.1 页面不能正常加载
telnet 本地opnip port
出现如下图情况,则有两种可能:
- 本地firewall没有关闭
- 或者 本地 8080 端口没有 tcp 相关服务开启
我遇到的问题就是我的 win10 firewall 开启了,我直接 关闭了firewall,如果是经常用作的服务器,我建议关闭 firewall,开启访问端口。
4.2 辅助测试
ping opnip
如果 ping 不通,则是firewall开启了
关闭了firewall,同时关闭了 8080 端口的服务,测试结果如下。
临时总结下不一定对: telnet 如果出现 Connection refused/failed 应该就是 端口可以正常访问只是服务没有开启而已。
正常访问时候, telnet 结果: