Nginx做HTTP代理、邮件服务器代理、数据库代理
一、背景
最近公司在做网络安全整改,对大数据环境的网络安全要求更高,要求所有的大数据环境的机器不能访问外网,但是可以访问公司的非大数据环境的其他服务器。我们组有四台单独的机器在大数据环境中,相关的python脚本都部署在这4台机器上,这些python脚本都是做大数据相关分析的,同时很多都有访问外网的需求,比如访问天气api查询历史天气、访问百度距离api查询、根据统计数据定时发邮件等,这些都是访问外网的。
大数据环境的机器不能访问外网,但是可以访问公司非大数据环境的机器,非大数据环境的机器可以访问外网。在考虑整改带来的代码入侵性和公司网络现状的情况下,准备申请一台在公司非大数据环境的机器做外网出口的正向代理,将所有的外网访问地址统一转到这台机器上,然后由这一台机器统一访问外网;这样及可以满足安全需求,也可以较少的修改代码,只需要替换现有外网地址对应的ip和端口即可。
二、整改过程
2.1.现状分析
统计现在四台机器的外网访问情况。发现主要有外网访问:一是调用外部的天气API和百度距离API,另一种是定时发邮件到相关同事的邮箱中;所以需要做两类代理:http代理、邮件服务器代理。
2.2.申请机器,搭建Nginx
需要申请一台不在大数据环境但是在公司网络环境的机器,并且大数据机器可以访问这台机器,同时这台机器可以访问外网。(目前申请一台机器,未考虑高可用)。
申请的机器地址:101.147.192.179。 作用:安装nginx,做为代理,后续不管是访问http外部接口还是发邮件,都使用这个ip。
公司真实的邮件服务器地址:mail.xxxxxx.cn
搭建nginx:
[ops@dis-algo data]$
[ops@dis-algo data]$ mkdir nginx
[ops@dis-algo data]$
[ops@dis-algo data]$ cd nginx/
[ops@dis-algo nginx]$
# 下载nginx
[ops@dis-algo nginx]$ wget http://nginx.org/download/nginx-1.20.1.tar.gz
[ops@dis-algo nginx]$ ls
nginx-1.20.1.tar.gz
[ops@dis-algo nginx]$
# 解压
[ops@dis-algo nginx]$ tar -zxvf nginx-1.20.1.tar.gz
[ops@dis-algo nginx]$ ll
total 1040
drwxr-xr-x 8 ops ops 158 May 25 20:35 nginx-1.20.1
-rw-rw-r-- 1 ops ops 1061461 May 25 23:34 nginx-1.20.1.tar.gz
[ops@dis-algo nginx]$
[ops@dis-algo nginx]$ cd nginx-1.20.1/
[ops@dis-algo nginx-1.20.1]$ ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$
# 配置。由于是使用stream方式代理邮件服务器,所以需要--with-stream模块,不要--with-mail模块
# 同时需要http代理相关的模块
# 安装的路径在当前目录的上一级 --prefix=.. 直接在当前目录安装由于文件冲突会报错
[ops@dis-algo nginx-1.20.1]$ ./configure --prefix=.. --with-http_stub_status_module --with-http_ssl_module --with-stream
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$
# 编译、安装
[ops@dis-algo nginx-1.20.1]$ make
[ops@dis-algo nginx-1.20.1]$ make install
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$ ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$
# 回到上一级的安装目录
[ops@dis-algo nginx-1.20.1]$ cd ..
[ops@dis-algo nginx]$ ls
conf html logs nginx-1.20.1 nginx-1.20.1.tar.gz sbin
[ops@dis-algo nginx]$
[ops@dis-algo nginx]$
2.3.配置http代理和邮件代理、数据库代理
数据库代理的配置通邮件服务器地址的配置。
(base) [ops@dis-algo85 nginx]$
(base) [ops@dis-algo85 nginx]$ cd conf/
# 找到配置文件 nginx.conf
(base) [ops@dis-algo85 conf]$ ls
fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
(base) [ops@dis-algo85 conf]$
(base) [ops@dis-algo85 conf]$
# 根据具体情况修改配置文件 完成http代理和邮件代理的配置
(base) [ops@dis-algo85 conf]$ vim nginx.conf
(base) [ops@dis-algo85 conf]$
(base) [ops@dis-algo85 conf]$ cat nginx.conf
worker_processes 8;
error_log /data/nginx/logs/error.log;
pid /data/nginx/logs/nginx.pid;events {worker_connections 1024;
}# http代理
http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;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 /data/nginx/logs/access.log main;# DNS服务器resolver 8.8.8.8;server {# 后续外部http请求都使用101.147.192.179:18080访问server_name 101.147.192.179;listen 18080; keepalive_requests 120;# 对每个http接口做代理location ^~ /caiyunapp/ {proxy_pass https://api.caiyunapp.com/;}# get传参使用$request_urilocation = /routematrix/v2/riding {proxy_pass http://api.map.baidu.com$request_uri;}location ^~ /tianqihoubao {proxy_pass http://www.tianqihoubao.com/;}location ^~ /weather {proxy_pass http://www.weather.com.cn/;}}
}# 需要使用--with-stream模块
stream {# 邮件服务器代理server {listen 25; # 这里必须使用25 其他的端口号我没有测通;使用25端口要求启动使用root权限proxy_pass mail.xxxxxx.cn:25; # 原来的公司邮件服务器 }# 数据库代理server {listen 13307;proxy_pass xx.xx.xx.xx:3306;}
}(base) [ops@dis-algo85 conf]$
# 测试配置文件是否正确
(base) [ops@dis-algo85 conf]$ ../sbin/nginx -t
nginx: the configuration file ../conf/nginx.conf syntax is ok
nginx: configuration file ../conf/nginx.conf test is successful
(base) [ops@dis-algo85 conf]$
# 启动 报错
# 需要执行nginx -c nginx.conf命令指定配置文件
(base) [ops@dis-algo85 conf]$ ../sbin/nginx -s reload
nginx: [error] invalid PID number "" in "/data/nginx/logs/nginx.pid"
(base) [ops@dis-algo85 conf]$
# 指定配置文件。 要求使用sudo,因为邮件代理使用的是25端口,这个端口要求root
(base) [ops@dis-algo85 conf]$ sudo ../sbin/nginx -c /data/nginx/conf/nginx.conf
(base) [ops@dis-algo85 conf]$
# 切换root用户前,先关闭nginx
(base) [ops@dis-algo85 conf]$
(base) [ops@dis-algo85 conf]$ ../sbin/nginx -s stop
(base) [ops@dis-algo85 conf]$
# 以root用户重启 要求先以root用户身份指定配置文件(sudo nginx -c )
(base) [ops@dis-algo85 conf]$ sudo ../sbin/nginx -s reload
(base) [ops@dis-algo85 conf]$
# 查看是否重启成功
(base) [ops@dis-algo85 conf]$ netstat -nltp | grep 25
(Not all processes could be identified, non-owned process infowill not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN -
(base) [ops@dis-algo85 conf]$
2.4.测试
在大数据环境的四台机器上测试。
2.4.1.Http代理测试
1.测试api.caiyunapp.com
原来的地址:
curl https://api.caiyunapp.com/v2.5/yourAppKey/119.292776,26.075473/realtime.json
代理的地址:
curl http://101.147.192.179:18080/caiyunapp/v2.5/yourAppKey/119.292776,26.075473/realtime.json2.测试api.map.baidu.com
原来的地址:
curl http://api.map.baidu.com/routematrix/v2/riding?output=json\&riding_type=1\&origins=30.903624,104.259426\&destinations=30.892165,104.254266\&ak=yourAppKey
代理的地址:
curl http://101.147.192.179:18080/routematrix/v2/riding?output=json\&riding_type=1\&origins=30.903624,104.259426\&destinations=30.892165,104.254266\&ak=yourAppKey3.测试www.tianqihoubao.com
原来的地址:
curl http://www.tianqihoubao.com/lishi/wuxi/month/201101.html
代理的地址:
curl http://101.147.192.179:18080/tianqihoubao/lishi/wuxi/month/201101.html4.测试www.weather.com.cn
原来的地址:
curl http://www.weather.com.cn/weather15d/101230101.shtml
代理的地址:
curl http://101.147.192.179:18080/weather/weather15d/101230101.shtml
2.4.2.邮件代理测试
1.引入jar包<!--发邮件--><dependency><groupId>com.sun.mail</groupId><artifactId>javax.mail</artifactId><version>1.5.6</version></dependency>2.java测试
package com.wuxiaolong;import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;public class TestEmail {private final static String TIMEOUT_MS = "20000";public static void main(String[] args) throws Exception{// 这是公司原来的邮件服务器 代理之前使用这个// String host = "mail.xxxxxx.cn";// 这是代理服务器(nginx) 代理之后使用这个String host = "101.147.192.179";String port = "25";// SMTP邮件服务器默认端口// 公司的邮箱发邮件String user = "wuxl3@abc.cn";String password = "XXXXXX";// 163邮箱收邮件String recipients = "wuxiaolongah@163.com";String subject = "邮件发送测试";String content = "邮件正文:<br>你好 proxy!";Properties props = new Properties();props.put("mail.transport.protocol", "smtp");props.put("mail.smtp.auth", "true");props.put("mail.smtp.port", port);props.put("mail.smtp.host", host);props.put("mail.smtp.timeout", TIMEOUT_MS);Authenticator auth = new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(user, password);}};Session session = Session.getInstance(props, auth);MimeMessage msg = new MimeMessage(session);msg.setFrom(new InternetAddress(user));msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));msg.setSubject(subject);// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件Multipart multipart = new MimeMultipart();// 添加邮件正文BodyPart contentPart = new MimeBodyPart();contentPart.setContent(content, "text/html;charset=UTF-8");multipart.addBodyPart(contentPart);// 将multipart对象放到message中msg.setContent(multipart);// 保存邮件msg.saveChanges();Transport.send(msg, msg.getAllRecipients());}}
测试结果:
2.5.修改代码
相关的python代码按照上面2.4的测试的代理地址进行修改。
2.6.后续注意事项
- 后续这四台机器上有新的外网地址访问,需要提前加入到这台代理机器上。
Nginx做HTTP代理、邮件服务器代理、数据库代理相关推荐
- 代理查询 mysql_查询数据库代理设置
示例 请求示例 http(s)://rds.aliyuncs.com/?DBInstanceId=rm-uf6wjk5xxxxxxxxxx &DBInstanceId=rm-uf6wjk5xx ...
- Discuz!无法连接阿里云RDS数据库代理问题的处理方案
很多站长在使用RDS开启了数据库代理之后,使用数据库代理连接发现一直是无法连接的(1045 notconnect). 经过仔细阅读阿里云数据库代理文档:https://help.aliyun.com/ ...
- 邮件服务器 之 基于FreeBSD和Postfix的邮件系统与邮件列表的web mail安装
作者: 杨廷勇(scyzxp at toping.net) 来自: LinuxSir.Org 版权:杨廷勇 Copyright © 2004.2005.2006 摘要: 本文介绍使用FreeBSD + ...
- 邮件服务器一般运行哪些协议,总结电子邮件支持的相关协议:SMTP、POP和IMAP
电子邮件托管是 ISP 提供的主要服务之一. 电子邮件因其简易性和通信速度使人们的通信方式发生了变革. 但是,如果要在计算机或其他终端设备上运行电子邮件,仍然需要多种应用程序和服务. 电子邮件是通过网 ...
- 邮件服务器从0到100
前段时间刚做了个邮件服务器,经过不段的查资料,现在终于基本实现功能,可以做邮件对内外网的收发并带附件(暂时只能带一个- -主要是前台没做好,没有传多个附件到后台,所以也就没测试过能不能同时发多个附件了 ...
- nginx做反向代理和后端web服务器之间的交互
1.Nginx是什么? Nginx就是反向代理服务器. 首先我们先来看看什么是代理服务器,代理服务器一般是指局域网内部的机器通过代理服务发送请求到互联网上的服务器,代理服务器一般作用于客户端.比如Go ...
- nginx做缓存服务器、代理、文件服务器等笔记
nginx笔记 nginx 做缓存服务器 一.安装nginx yum install -y nginx 二.修改nginx配置 1.主配置/etc/nginx/nginx.conf worker_pr ...
- Nginx高性能反向代理web服务器
基础 Nginx简介 背景介绍 Nginx("engine x")一个具有高性能的[HTTP]和[反向代理]的[WEB服务器],同时也是一个[POP3/SMTP/IMAP代理服务器 ...
- nginx做为反向代理实现负载均衡的例子 .
我们介绍了nginx这个轻量级的高性能server主要可以干的两件事情: >直接作为http server(代替apache,对PHP需要FastCGI处理器支持,这个我们之后介绍): > ...
最新文章
- 资料分享:推荐一本《简单粗暴TensorFlow 2.0》开源电子书!
- NodeAsp——像开发NodeJS应用一样玩转ASP
- IOS上传代码到CocoaPods并通过Pod下载
- XVIII Open Cup named after E.V. Pankratiev. GP of Urals
- JAVA进阶day07JNI(java调用c)A部分
- 炒了8年的概念,到底该如何理解DevOps这个词?
- JAVA零碎要点015---java BigDecimal常见操作_加减乘除操作_比较_取几位小数四舍五入_随时更新
- 2016陕西省赛 Rui and her functions
- springboot动态切换数据源_Springboot整合Mybatis注解实现动态数据源切换
- ScaleAnimation缩放动画Demo
- mysql的瓶颈_MySQL 瓶颈分析及优化
- 编译原理完整学习笔记(四):语法分析
- 用C++计算文件的MD5值
- [渝粤教育] 淄博职业学院 市场营销 参考 资料
- 软件测试性能测试报告完整版,性能测试报告模板
- 【深度学习框架-torch】torch.norm函数详解用法
- lcms质谱仪_岛津LCMS-8045三重四极杆质谱仪
- 跳步游戏2--返回最小跳步数
- excel高级筛选怎么用_Excel教程:用高级筛选做超级拆分器
- python os创建txt文件_Python新建动态命名txt文件