Nginx做HTTP代理、邮件服务器代理、数据库代理

本文介绍了在大数据环境不能访问外网的背景下,如何通过申请一台非大数据环境的机器,搭建Nginx作为正向代理,实现对外网API和邮件服务器的访问。详细步骤包括现状分析、申请和配置Nginx、配置HTTP代理和邮件代理,以及测试和后续代码修改。此方案减少了代码入侵性,满足了网络安全需求。
摘要由CSDN通过智能技术生成

一、背景

最近公司在做网络安全整改,对大数据环境的网络安全要求更高,要求所有的大数据环境的机器不能访问外网,但是可以访问公司的非大数据环境的其他服务器。我们组有四台单独的机器在大数据环境中,相关的python脚本都部署在这4台机器上,这些python脚本都是做大数据相关分析的,同时很多都有访问外网的需求,比如访问天气api查询历史天气、访问百度距离api查询、根据统计数据定时发邮件等,这些都是访问外网的。

image-20211009151815732

大数据环境的机器不能访问外网,但是可以访问公司非大数据环境的机器,非大数据环境的机器可以访问外网。在考虑整改带来的代码入侵性和公司网络现状的情况下,准备申请一台在公司非大数据环境的机器做外网出口的正向代理,将所有的外网访问地址统一转到这台机器上,然后由这一台机器统一访问外网;这样及可以满足安全需求,也可以较少的修改代码,只需要替换现有外网地址对应的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_uri
        location = /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 info
 will 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.json

2.测试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=yourAppKey

3.测试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.html

4.测试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() {
            @Override
            protected 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());
    }

}

测试结果:

image-20211009194913128

2.5.修改代码

相关的python代码按照上面2.4的测试的代理地址进行修改。

2.6.后续注意事项

  • 后续这四台机器上有新的外网地址访问,需要提前加入到这台代理机器上。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值