nginx 基于域名的TCP协议反向代理,如何配置
来源:6-12 代理场景下Nginx与上游服务建立连接细节

shuwanghao
2023-11-08
老师,我这边配置 TCP协议 基于域名的反向代理失败了,需要老师您帮忙解答!
nginx的版本是 1.24.0
我陪着的 nginx 配置文件如下:
user nginx;
worker_processes 1;
pid /var/run/nginx.pid;
error_log /var/log/nginx/nginx.error.log warn;
events {
worker_connections 1024;
}
#stream模块和http模块是并列级别的,所以stream要写在http{}外边
stream {
# log_format basic '$remote_addr [$time_local] '
# '$protocol $status $bytes_sent $bytes_received '
# '$session_time';
# access_log /var/log/nginx/stream-access.log basic buffer=32k;
# 为了让这个配置文件简单一些,将配置stream放入到/etc/nginx/conf.d,并以.stream做后缀名。
# 需要为每个端口创建一个.stream做后缀名的配置文件
include /etc/nginx/conf.d/*.stream;
}
http {
include /etc/nginx/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 /dev/null;
#access_log /var/log/dnmp/nginx.access.log main;
# hide verson string
server_tokens off;
sendfile on;
#tcp_nopush on;
client_max_body_size 100M;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
map $ssl_preread_server_name $name {
mysql.wvzc.com backend;
default backend2;
}
upstream backend {
server host.docker.internal:3305;
}
upstream backend2 {
server 192.168.0.3:12345;
server 192.168.0.4:12345;
}
server {
listen 12346;
proxy_pass $name;
ssl_preread on;
}
# 这样配置是对的
server {
listen 12345; # 代理服务器监听的端口
proxy_pass host.docker.internal:3305; # 后端服务器的地址和端口
}
# 这样配置是对的
upstream mysql.wvzc.com.server {
server host.docker.internal:3305;
}
server {
listen 188; # 代理服务器监听的端口
proxy_pass mysql.wvzc.com.server; # 后端服务器的地址和端口
}
直接访问 nginx 的 12345 和 188 端口可以正常连接 mysql 客户端;
但是通过域名( mysql.wvzc.com )访问 nginx 的 12346 端口,无法与 mysql 建立连接!
不知道问题具体出在哪了。。。。
nginx 的编译配置如下:
sh-4.2# nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-perl_modules_path=/usr/lib/perl5/vendor_perl --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-Os -fomit-frame-pointer -g' --with-ld-opt=-Wl,--as-needed,-O1,--sort-common
sh-4.2#
写回答
1回答
-
酷田
2023-12-16
从您配置的域名来看,12346端口的虚拟主机并没有写清楚域名导致的把,另外这个地址需要能被您的PC解析到
00
相似问题