【问题标题】:Redirect specifc HTTPS request to a specific port with apache使用 apache 将特定 HTTPS 请求重定向到特定端口
【发布时间】:2018-11-10 04:44:37
【问题描述】:

我无法将某些请求重定向到其他端口。这是我的配置:

  • 我有一个像 XXXX.ddns.net 这样的公共域
  • 我有一个带有 apache 的 Rapsbian 服务器,并且我的 /var/www 文件夹中的文件得到了正确的服务(角度网站)
  • 在同一个 Raspbian 服务器上,有一个 REST 服务器在 3000 端口上运行
  • 这是在带有 SSL(letsencrypt) 的 HTTPS 上运行的

我希望所有对 XXXX.ddns.net/api/* 的请求都重定向到 3000 端口。

我更改了 .htaccess 文件,重写规则似乎在本地工作,但我无法在我的互联网站点上工作。 API 请求实现时出现错误 500。

这是我当前的 .htaccess 文件:

RewriteEngine On
RewriteRule ^api/(.*)      https://localhost:3000/api/$1 [QSA]
# not sure if it should be http or https in the rule but nothing works
#RewriteRule ^api/(.*)      http://localhost:3000/api/$1 [QSA]

# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

# If the requested pattern is file and file doesn't exist, send 404
RewriteCond %{REQUEST_URI} ^(\/[a-z_\-\s0-9\.]+)+\.[a-zA-Z]{2,4}$
RewriteRule ^ - [L,R=404]

这是我当前的 000-default-le-ssl.conf 文件(在 /etc/apache2/sites-available 中):

<IfModule mod_ssl.c>
<VirtualHost *:443>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

ServerName XXXX.ddns.net
SSLCertificateFile /etc/letsencrypt/live/XXXX.ddns.net/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/XXXX.ddns.net/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
<Location /api>
        ProxyPass http://127.0.0.1:3000/api
        ProxyPassReverse http://127.0.0.1:3000/api
</Location>

</VirtualHost>
</IfModule>

如果有人可以帮助我实现它... 谢谢!

【问题讨论】:

  • 我不熟悉letsencrypt。日志文件中有任何内容吗?有关详细信息,请参阅“systemctl status apache2.service”和“journalctl -xe”。如果您总是想要 HTTPS,只需在端口 80 处添加第二个虚拟主机,并使用指令 redirect permanent / https://yourserver.url/
  • 我试过了,但没有任何改变。当我的网站联系 api 部分时,出现错误 500。如果我直接在 3000 端口上发出我的 api 请求,它可以工作,但它并不优雅,并且几乎被所有防火墙阻止......还有其他想法吗?
  • 如果它不起作用,您可以使用命令systemctl status apache2.servicejournalctl -xesudoroot 获得有关问题所在的线索。你这样做了吗?

标签: apache redirect https


【解决方案1】:

您自己找到的解决方案对我来说看起来很奇怪。您打开 SSLProxyEngine 并禁用所有安全措施。后端 API 是否同时在 3000 端口的 HTTPS 和 HTTP 下运行?这不可能。

我经常使用这种设置(apache 作为后端应用程序的代理)并建议以下配置:

由于我不理解重写指令的目的,所以我将它们排除在外。端口 80 的 VirtualHost 总是将 HTTP 请求重定向到 HTTPS。如果可行,请将 permanent 添加到指令中(某些浏览器会缓存永久内容,请参阅 VirtualHost *:80 中的注释)。

用于 HTTPS 的 VirtualHost 从位于 /var/www/html 的 DocumentRoot 中提供内容。 Directory 指令注意只提供正确寻址的文件(不可能进行查找)。 VirtualHost 还为同一服务器上 3000 端口上的 /api 提供代理。

如果您的letsencrypt配置正确(填写XXXX),它应该适用于apache 2.4。两种 VirtualHost 配置都可以写入单个文件,通常位于 /etc/apache2/sites-available 中,并带有指向 /etc/apache2/sites-enabled 的符号链接。请在测试此配置之前删除/重命名您的 .htaccess 文件和其他配置。如果您需要通过 apache 进行访问控制,也可以直接在 VirtualHost 配置中进行配置。

<VirtualHost *:80>
    ServerName XXXX.ddns.net
    # Always https
    Redirect / https://XXXX.ddns.net/
#    Redirect permanent / https://XXXX.ddns.net/
</VirtualHost>

<VirtualHost *:443>
    ServerAdmin webmaster@localhost
    ServerName XXXX.ddns.net
    # These are your SSL settings; your responsibility
    SSLCertificateFile /etc/letsencrypt/live/XXXX.ddns.net/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/XXXX.ddns.net/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf

    # Your document root; where the JavaScript application lives
    DocumentRoot /var/www/html
    <Directory /var/www/html/ >
        Options -Indexes +FollowSymLinks -MultiViews
        AllowOverride None
        Order Allow,Deny 
        Allow From All
    </Directory>

    # Reverse proxy settings for api
    ProxyRequests     Off
    ProxyPreserveHost On
    <Location /api >
        ProxyPass        http://127.0.0.1:3000/api
        ProxyPassReverse http://127.0.0.1:3000/api
    </Location>
</VirtualHost>

【讨论】:

    【解决方案2】:

    感谢您的帮助。我真的不知道如何,但它现在有效! 我不记得我做了什么,但最后一个是像这样修改我的 000-default-le-ssl.conf 文件:

    SSLProxyEngine On
    SSLProxyVerify none 
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerName off
    SSLProxyCheckPeerExpire off
    <Location /api>
        ProxyPass http://127.0.0.1:3000/api/
        ProxyPassReverse http://127.0.0.1:3000/api/
        ProxyPass https://127.0.0.1:3000/api/
        ProxyPassReverse https://127.0.0.1:3000/api/
    </Location>
    

    【讨论】:

    • 嗨,你自己找到的解决方案对我来说看起来很奇怪。请检查我的答案。
    猜你喜欢
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    相关资源
    最近更新 更多