如何玩转nginx正反向代理
新钛云服已累计为您分享763篇技术干货
Ngnix大家都比较熟悉,最常见的Web服务器,但是它还有一个常见的用户代理服务器,它支持正向代理,也可以反向代理,用的比较多的,可能还是发现代理;本篇将通过一个实际的场景来介绍一下Nginx的正反向代理分别怎么用。
1、在客户的场景中,有两台测试服务windows server系统,无法访问外网。
2、测试服务器中运行的业务程序,需要访问有几个公网域名。
3、场景中还有一台代理服务器 ,它可以访问外网,也可以通两台测试服务器;
系统架构图如下。
两台不能访问外网的测试服务器里面的程序,需要访问外网的几个固定域名。
# cat default.conf
server {
listen80;
server_name _;
location / {
resolver114.114.114.114;
set$backend_host$host; #将原始域名存储到变量中
proxy_pass http://$backend_host$request_uri; #使用变量保持原始域名
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
2.2.1 nginx的ssl_preread
nginx的ssl_preread介绍
1、Nginx 开始支持 SSL 的 ssl_preread 功能是在版本 1.9.0 中引入的。
2、ssl_preread 是 Nginx 的 Stream 模块中的一个指令,用于在 SSL 握手之前读取客户端发送的数据,通常用于提前获取客户端发送的信息,比如域名,从而根据这些信息做出代理或路由决策。这在反向代理和负载均衡的场景中特别有用。
以下是关于 ssl_preread 的详细描述:
1. 功能介绍: ssl_preread 允许 Nginx 在 SSL/TLS 握手的早期阶段读取客户端发送的数据,包括 ClientHello 消息。这样做的目的是为了从握手的预读取数据中获取一些信息,以便在接下来的代理和路由过程中做出更明智的决策。
2. 使用场景: ssl_preread 常用于以下场景:
- 根据客户端发送的域名(SNI)将流量代理到不同的后端服务器。
- 根据不同的协议或应用层协议(如 HTTP、SMTP、POP3 等)将流量路由到不同的后端服务器。
2.2.2、nginx配置
安装nginx配置
nginx编译安装需要添加特定模块
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module
nginx配置文件
这里需要业务系统中使用了几个域名就要配置几个upstream
#user nobody;
worker_processes1;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events{
worker_connections1024;
}
http{
includemime.types;
default_typeapplication/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;
sendfileon;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout65;
includeconf.d/*.conf;
}
#配置https反向代理
stream{
map$ssl_preread_server_name $backend_hosthttps {
tyun;
tyun2;
}
upstreamtyun {
serverwww.tyun.cn:443;
}
upstreamtyun2 {
serverwww.tyun2.cn:443;
}
server{
listen443;
ssl_prereadon;
resolver114.114.114.114;
proxy_pass$backend_hosthttps;
proxy_connect_timeout5s;
proxy_timeout15s;
}
}
2.2.3、无外网win server服务器配置
1、需要配置hosts文件将域名指向代理服务器中
C:\Windows\System32\drivers\etc\hosts
192.168.1.100www.tyun.cn
192.168.1.100www.tyun2.cn
wgethttps://github.com/chobits/ngx_http_proxy_connect_module/archive/refs/heads/master.zip
unzipmaster.zip
yuminstall -y patch
#在nginx源码目录执行
cd/root/nginx-1.23.3
patch-p1 < /root/ngx_http_proxy_connect_module-master/patch/proxy_connect_rewrite_102101.patch
#nginx编译
\
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gzip_static_module \
--with-threads \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_realip_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--add-module=/root/ngx_http_proxy_connect_module-master
make&& make install
server{
listen8080;
resolver114.114.114.114;
proxy_connect;
proxy_connect_allow80 443;
proxy_connect_connect_timeout10s;
proxy_connect_data_timeout10s;
location/ {
proxy_passhttp://$host;
proxy_set_headerHost $host;
proxy_set_headerX-Real-IP $remote_addr;
}
}
import os
import requests
import winreg
# 获取 Windows 系统代理配置
internet_settings = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Internet Settings')
proxy_enabled, _ = winreg.QueryValueEx(internet_settings, 'ProxyEnable')
proxy_server, _ = winreg.QueryValueEx(internet_settings, 'ProxyServer')
if proxy_enabled and proxy_server:
proxies = {
'http': 'http://' + proxy_server,
'https': 'https://' + proxy_server,
}
os.environ['http_proxy'] = 'http://' + proxy_server
os.environ['https_proxy'] = 'https://' + proxy_server
else:
proxies = None
# 使用代理请求
response = requests.get('http://www.tyun.cn', proxies=proxies)
print(response.text)
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。