
Ich habe derzeit Probleme beim Versuch, Apache als Reverse-Proxy für das Front-End unserer Anwendung und unsere Back-End-API zu verwenden.
Wenn ein Benutzer beispielsweise die URL anklickt, https://my.app.com/
sollte er zu unserer Frontend-Anwendung weitergeleitet werden, die von S3 bereitgestellt wird. Wenn Sie die URL anklicken, https://my.app.com/api/hello
erhalten Sie eine JSON-Antwort von unserer Backend-API.
Das ideale Verhalten besteht darin, dass https://my.app.com/
ein Reverse-Proxy zu verwendet wird http://s3app.private
.
Ebenso möchten wir https://my.app.com/api/hello
einen Reverse-Proxy verwenden zuhttp://myapi.private/api/hello
Unsere aktuelle Apache-Konfiguration sieht folgendermaßen aus:
Listen 443
<VirtualHost *:443>
ServerName my.app.com
ErrorDocument 404 /index.html
ProxyErrorOverride On
RequestHeader set X-Forwarded-Proto https
SSLEngine on
SSLCertificateFile "/usr/local/apache2/certs/certificate.crt"
SSLCertificateKeyFile "/usr/local/apache2/certs/private.key"
SSLProtocol TLSv1.2
ProxyAddHeaders On
<Location "/">
ProxyPass "http://s3app.private/" retry=0
</Location>
<Location "/api">
ProxyPreserveHost On
ProxyPass "http://myapi.private/api" retry=0
</Location>
</VirtualHost>
Das aktuelle Verhalten ist, dass https://my.app.com/
wir unsere Frontend-Anwendung sehen, wenn wir darauf klicken. Allerdings https://my.app.com/api/hello
erhalten wir beim Klicken eine 503
Fehlermeldung vom Server.
Die Protokolle von Apache zeigen diese Zeilen:
[proxy:error] The timeout specified has expired: AH00957: HTTP: attempt to connect to <internal_ip>:80 (api.private) failed
[proxy_http:error] AH01114: HTTP: failed to make connection to backend: myapi.private, referer: https://my.app.com/
"GET /api/hello HTTP/1.1" 503 299
Wir haben geprüft, dass unsere API tatsächlich über diesen Mechanismus lauscht, indem wir Apache folgendermaßen nur auf die API verwiesen haben:
Listen 443
<VirtualHost *:443>
ServerName my.app.com
ErrorDocument 404 /index.html
ProxyErrorOverride On
RequestHeader set X-Forwarded-Proto https
SSLEngine on
SSLCertificateFile "/usr/local/apache2/certs/certificate.crt"
SSLCertificateKeyFile "/usr/local/apache2/certs/private.key"
SSLProtocol TLSv1.2
ProxyAddHeaders On
<Location "/api">
ProxyPreserveHost On
ProxyPass "http://myapi.private/api" retry=0
</Location>
</VirtualHost>
Auf diese Weise können wir https://my.app.com/api/hello
die erwartete JSON-Antwort sehen. Dies ist jedoch nur erfolgreich, wenn kein anderer Standortblock vorhanden ist.
Unser interner DNS wird von Route53 bereitgestellt und unsere API wird in ECS gehostet.
Für jede Hilfe wäre ich sehr dankbar!
Antwort1
Die Verwendung von zwei virtuellen Hosts kann hilfreich sein, ist aber möglicherweise nicht die beste Lösung.
Listen 443
<VirtualHost *:443>
ServerName my.app.com
ErrorDocument 404 /index.html
ProxyErrorOverride On
RequestHeader set X-Forwarded-Proto https
SSLEngine on
SSLCertificateFile "/usr/local/apache2/certs/certificate.crt"
SSLCertificateKeyFile "/usr/local/apache2/certs/private.key"
SSLProtocol TLSv1.2
ProxyAddHeaders On
<Location "/">
ProxyPass "http://s3app.private/" retry=0
</Location>
<Location "/api">
ProxyPreserveHost On
ProxyPass "http://localhost:8443/api" retry=0
</Location>
</VirtualHost>
Listen 8443
<VirtualHost *:8443>
ServerName localhost
<Location "/api">
ProxyPreserveHost On
ProxyPass "http://myapi.private/api" retry=0
</Location>
</VirtualHost>
Ich kenne keinen technischen Grund, warum dies Ihr Problem lösen würde, aber einen Versuch ist es vielleicht wert.