首页 > 解决方案 > 如何在apache2中为spring boot中的嵌入式tomcat服务器配置反向代理?

问题描述

我正在尝试为 spring boot 应用程序配置反向代理,但它不起作用。它从 www/example.com 提供 html 页面,而不是从 Spring Boot 应用程序提供内容。

这是我的 application.properties

server.address=127.0.0.1
server.port=8080
server.servlet.context-path=/
server.use-forward-headers=true
server.tomcat.remote_ip_header=x-forwarded-for
server.tomcat.protocol_header=x-forwarded-proto

这是我的 example.com.conf

<VirtualHost *:80>
ServerAdmin webmaster@localhost
ProxyPreserveHost On
ServerName example.com
ServerAlias www.example.com
Redirect / https://www.example.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<VirtualHost *:443>    
SSLEngine On
SSLProxyEngine On
ServerName example.com
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
RequestHeader set X-Forwarded-Proto https
RequestHeader set X-Forwarded-Port 443
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf    
</VirtualHost>

标签: javaspring-bootapache2reverse-proxy

解决方案


事实证明,有一个名为 example.com-le-ssl.conf 的文件覆盖了 example.com.conf 中的配置,所以我只是将 example.com.conf 中的配置复制到了 example.com-le-ssl .conf 并且它起作用了。我的猜测是 example.com-le-ssl.conf 是在我添加 ssl 证书时创建的。


推荐阅读