【问题标题】:Configure Apache for Angular and Tomcat on the same host在同一主机上为 Angular 和 Tomcat 配置 Apache
【发布时间】:2021-09-04 02:22:47
【问题描述】:

我正在尝试在 Centos 7 主机上提供我的应用程序,我已经为前端部署了 Angular 应用程序,为后端部署了 Java Spring Boot 应用程序。时间> 我的版本是Apache 2.4.6Tomcat 9Angular 7

假设我的域是example.com。我希望用户在浏览器上编写以下内容以访问 Angular 应用程序路由,即前端:

example.com/**

我还希望能够在浏览器上编写以下内容以访问 Java 应用程序路由,即后端:

example.com/api/**

在阅读了这篇文章后,我尝试使用 Apache 的 mod_proxy 来做到这一点:https://stackoverflow.com/a/33095758/350061。但是,我只设法通过example.com 为Angular 提供服务,而我的API 只在其原始端点上提供服务,即example.com:8080/my-api 而不是通过example.com/api

这些是我的 Apache 设置:

/etc/httpd/conf.d/example.conf

<VirtualHost *:80>
   ServerName example.com
   ProxyPreserveHost On
   DocumentRoot "/var/www/my-ng-app/dist/my-ng-app"

   RewriteRule ^/api/(.*)     http://localhost:8080/$1 [P,L,QSA]
   ProxyPassReverse /api/     http://localhost:8080/
</VirtualHost>

/var/www/my-ng-app/.htaccess

RewriteEngine On

RewriteRule ^api/(.*)      http://localhost:8080/my-api/$1 [P,L,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 resource doesn't exist, use index.html
RewriteRule ^ /index.html

我已将.htaccess 配置为从该文件夹中读取,并验证它确实已被读取。此外,即使我删除了将不存在资源重定向到index.html.htaccess 的最后一部分,也不会像我希望的那样从example.com/api 提供api。它只从example.com:8080/my-api 获得服务

关于如何配置它的任何想法?

【问题讨论】:

    标签: apache .htaccess tomcat9


    【解决方案1】:

    经过长时间的谷歌搜索和反复试验,我的解决方案包括以下更改:

    1. 我编辑了 Tomcat 的 server.xml 连接器以通过 localhost 处理请求。这使得重写设置可以通过localhost:8080 访问我的Java 应用程序,但使我无法通过example.com:8080 远程访问我的Java 应用程序,这很好。
    2. 我将SELinux 配置为允许Apache (httpd) 启动出站连接,在我的情况下连接到localhost
    3. 我对原来的 VirtualHost.htaccess 配置进行了一些修复

    更具体地说:

    1. sudo nano /opt/tomcat/latest/conf/server.xml
      

      找到端口 8080 的连接器并添加 address 属性,如下所示:

      <Connector port="8080" protocol="HTTP/1.1"
                 address="127.0.0.1"
                 connectionTimeout="20000"
                 redirectPort="8443" />
      
    2. /usr/sbin/setsebool -P httpd_can_network_connect 1
      

      -P 保留设置以供将来重新启动

    3. /etc/httpd/conf.d/example.conf

      <VirtualHost *:80>
         ServerName example.com
         ProxyPreserveHost On
         DocumentRoot "/var/www/my-ng-app/dist/my-ng-app"
      
         RewriteRule ^/api/(.*)     http://localhost:8080/my-api/$1 [P,L,QSA]
         ProxyPassReverse /api/     http://localhost:8080/my-api/
      </VirtualHost>
      

      /var/www/my-ng-app/.htaccess

      # 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 resource doesn't exist, use index.html
      RewriteRule ^ /index.html
      

    完成这些更改后,只需重新启动 TomcatApache,您就可以通过 example.com/api 访问您的 API

    【讨论】:

      猜你喜欢
      • 2012-04-14
      • 2013-01-30
      • 1970-01-01
      • 2015-02-01
      • 2011-06-11
      • 2018-03-27
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      相关资源
      最近更新 更多