Apache 中用于 rest API 的 HTTPS 代理

Apache 中用于 rest API 的 HTTPS 代理

听起来像是一个基本问题,但我不了解 Apache 配置。任何帮助都很好。

自从打开 TSDB不支持 https 发送数据(通过 REST API),我们想在 apache 或“Apache Tomcat”中创建一个 https 代理。例如,本地计算机同时具有 TSDB 和 Apahce。apache 应该接受 http 和 https,如果我将任何内容发送到https://<PUBLIC IP>/api/input,它应该转发到TSDB同一台计算机(或TSDB可能在不同的计算机)中http

总体情况

MY Code<----> https://<PUBLIC IP>/api/input<----代理至---->http://localhost/api/input

上面我提到Apache Tomcat,因为我们主要将 Apache Tomcat 用于其他目的,所以首要任务是 Tomcat(使用 Tomcat 可以吗?)。

笔记:Apache我知道和之间的区别Apache Tomcat:Apache 是 http web 服务器,仅处理 http 流量,tomcat 是用于处理请求的 servelet 容器(仅适用于 Java)。在构建 Tomcat 时有 Apache。(如果我错了,请纠正我)

答案1

在 Apache HTTPD 中基本上是这样的:

<VirtualHost *:80>
ServerName publicname.example.com
Redirect / https://publicname.example.com/
</VirtualHost>

<VirtualHost *:443>
ServerName publicname.example.com
SSLEngine on
SSLCertificateFile /path/to/cert.pem
SSLCertificateKeyFile /path/to/key.pem
ErrorLog /path/to/logs/publicaname.example.com-ssl-error.log
CustomLog /path/to/logs/publicaname.example.com-ssl.log combined

ProxyPass /api/input http://127.0.0.1:8080/api/input
ProxyPassReverse /api/input http://127.0.0.1:8080/api/input
</VirtualHost>

注意:你需要mod_proxymod_proxy_http首先加载的模块。

答案2

感谢@ezra-s,我能够发送数据。但在发送过程中,我遇到了一些困难,所以我只想分享一些信息。

1.sudo apt-get install -y libapache2-mod-proxy-html libxml2-dev apache2-prefork-dev libxml2-dev

2.启用模块

sudo a2enmod proxy proxy_ajp proxy_http rewrite deflate headers proxy_balancer proxy_connect proxy_htm ssl

3.生成自签名证书这是指南

4.添加配置/etc/apache2/sites-enabled/000-default.conf

5.我尝试重启sudo service apache2 restart。但出现错误

 * Starting web server apache2                                                                                                                  * 
 * The apache2 configtest failed.
Output of config test was:
[Mon Feb 13 02:31:06.772053 2017] [proxy_html:notice] [pid 8060] AH01425: I18n support in mod_proxy_html requires mod_xml2enc. Without it, non-ASCII characters in proxied pages are likely to display incorrectly.
AH00526: Syntax error on line 39 of /etc/apache2/sites-enabled/000-default.conf:
ProxyPass Unable to parse URL
Action 'configtest' failed.
The Apache error log may have more information.

经过长时间的互联网搜索,我发现mod_xml2enc 不可用错误。因此,我从源代码构建了这个模块

sudo apt-get install apache2-prefork-dev libxml2 libxml2-dev
mkdir ~/modbuild/ && cd ~/modbuild/
wget http://apache.webthing.com/svn/apache/filters/mod_xml2enc.c
wget http://apache.webthing.com/svn/apache/filters/mod_xml2enc.h
sudo apxs2 -aic -I/usr/include/libxml2 ./mod_xml2enc.c
cd ~
rm -rfd ~/modbuild/
sudo service apache2 restart
 * Restarting web server apache2                                                                                                               AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
                                                                                                                                        [ OK ]

谢谢您的帮助!

答案3

有很多方法可以实现您想要的结果。

让您的 Tomcat 监听 127.0.0.1:8080,并在其前面设置一些前端服务器。您的前端服务器将监听 *:80 和 *:443,并将所有请求转发到隐藏的 Tomcat。

有很多前端服务器。可以是 Apache(如您所述)、nginx(占用较少内存)或 haproxy(提供出色的统计数据,同时几乎不占用任何内存)。

nginx 的一个可能的配置片段可以是:

server {
    listen 443 ssl;

    server_name your.hostname.com; 
    access_log  .../access_log main;
    error_log .../error_log;

    ssl_certificate      /.../fullchain.pem;
    ssl_certificate_key  /.../key.pem;

    # some TLS config should be here

    # forward all requests to Tomcat 8080
    location / {
      proxy_pass      http://127.0.0.1:8080/;
      client_max_body_size    128m;  # limit POST size
    }
}

server {
    listen 80;
    server_name your.hostname.com; 

    access_log  /.../80-access_log main;
    error_log /.../80-error_log;

    location / {
        # redirect everython to HTTPS
        return 301 https://$host$request_uri;
    }
}

对于生成 TLS 配置,我建议Mozilla SSL 配置生成器

相关内容