У меня следующая настройка сервера (Ubuntu 19.04):
/var/www/example.com
Я пытаюсь заставить все запросы от example.com/api/*
в серверную часть NodeJS на порт 3333. Это мой sites-enabled/example.com.conf
:
<VirtualHost *:80>
ServerAdmin support@example.com
ServerName example.com
ServerAlias example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
ProxyRequests on
ProxyPreserveHost on
ProxyPass /api https://127.0.0.1:3333/api
ProxyPassReverse /api https://127.0.0.1:3333/api
</VirtualHost>
Если поможет, это мой sites-enabled/example.com-le-ssl.conf
:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin support@exampl
ServerName example.com
ServerAlias example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias www.example.com
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
С этой конфигурацией:
access.log
Как мне заставить фронтенд правильно проксировать / перенаправлять example.com/api/*
запросы к бэкэнду NodeJS?
Вы перенаправляете все HTTP-запросы на https, но ваши прокси-директивы находятся в виртуальном хосте http.
Переместите директивы proxy * на виртуальный хост HTTPS.
<VirtualHost *:80>
ServerAdmin support@example.com
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerAdmin support@example.com
ServerName example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias www.example.com
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
ProxyRequests off
ProxyPreserveHost on
ProxyPass /api https://127.0.0.1:3333/api
ProxyPassReverse /api https://127.0.0.1:3333/api
</VirtualHost>
Кстати, установите ProxyRequests
к off
. Это не отключает директивы ProxyPass, но включение этого превращает ваш сервер в открытый прокси. Обычно вы этого не хотите.
Что касается ошибки 500, прочтите журнал ошибок.