首页 > 解决方案 > 使用 apache 将特定 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>

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

标签: apacheredirecthttps

解决方案


你自己找到的解决方案对我来说看起来很奇怪。您打开 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>

推荐阅读