apache - 使用 apache 将特定 HTTPS 请求重定向到特定端口
问题描述
我在将某些请求重定向到其他端口时遇到问题。这是我的配置:
- 我有一个像 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>
如果有人可以帮助我实现它...谢谢!
解决方案
你自己找到的解决方案对我来说看起来很奇怪。您打开 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>
推荐阅读
- python - 代码无法使用排序列表和用户输入找到正确的最小值或最大值?
- php - 本地与远程服务器中的 PHP CURL
- php - 使用 PHP 合并一些取决于范围的数组数据
- .net - 如何将 Windows 驱动程序工具包 (WDK) 安装到 Visual Studio 2017 Pro 中以便对 WinUSB 进行编程?
- macos - 在杯子中正确使用选项“-o CustomPrintSize=”
- scheme - 使用 Foldr/Foldl 交替求和(球拍)
- php - 删除父或“包装”数组
- angular - Angular Material 中 Mat 对话框中的计时器
- php - 使用 mysql 查询连接表?
- excel - 与数组一起使用时 Excel Range.AutoFilter 出错(Powershell)