【发布时间】:2020-11-29 14:01:51
【问题描述】:
我根据Corey Schafer's youtube playlist about Django 在 Ubuntu Linux 服务器上设置了一个 Apache2 HTTP 服务器,以通过 WSGI 为 django 应用程序(学习 Django 的爱好项目)提供服务。服务器设置为使用 HTTPS 而不是 HTTP。
我想也使用这个 HTTP 服务器来部署一个使用 Angular 10 开发的单页应用程序 (SPA)。我试图让这个配置满足 3 个条件:
- 服务我的 django 应用程序
- 为我的 Angular SPA 服务
- 服务 Angular SPA无需我更改 Angular SPA 的 index.html 文件(例如,通过修改 js-script-tags 中使用的 src-url)
第 3 点大部分都在那里,所以我可以快速轻松地部署更新到我的 SPA,只要有一个更新包就从 git-repo 中拉出。
到目前为止,我在下面配置的内容(并成功地为我的 Django 应用程序提供服务)在 url 命名空间 <DOMAIN>/wiki 中正确地为我的 django-app 服务。
Angular SPA 在 url 命名空间 <DOMAIN>/wiki2 中提供服务。
#Apache2 config file aldrunewiki-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
...
# Configuration for the Angular SPA
AliasMatch ^/wiki2/(.*)$ /home/isofruit/AldruneWikiFrontend/dist/frontend/index.html
<Directory /home/isofruit/AldruneWikiFrontend/dist/frontend>
Require all granted
DirectoryIndex index.html
</Directory>
# Configuration for the Django Application
Alias /static /home/isofruit/AldruneWiki/static
<Directory /home/isofruit/AldruneWiki/static>
Require all granted
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^https://aldrune.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https://www.aldrune.com/.*$ [NC]
RewriteRule .(mp3|jpg|png)$ - [F]
</Directory>
Alias /media /home/isofruit/AldruneWiki/media
<Directory /home/isofruit/AldruneWiki/media>
Require all granted
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^https://aldrune.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https://www.aldrune.com/.*$ [NC]
RewriteRule .(mp3|jpg|png)$ - [F]
</Directory>
<Directory /home/isofruit/AldruneWiki/aldrunewiki>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias /wiki /home/isofruit/AldruneWiki/aldrunewiki/wsgi.py
WSGIDaemonProcess aldrunewiki_app python-path=/home/isofruit/AldruneWiki python-home=/home/isofruit/AldruneWiki/venv
WSGIProcessGroup aldrunewiki_app
SSLCertificateFile <REMOVED>
SSLCertificateKeyFile <REMOVED>
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
就我获得 index.html 而言,这确实有效,但它尝试加载的 JS 文件失败并出现 404:
这是因为我的 index.html 中 <script>-tags 的 src-URL 具有 <DOMAIN>/filename.js 模式,而我在 aldrunewiki-le-ssl.conf 中没有正确的别名。
#Example of script tags in index.html
...
<script src="runtime.c6a2assa9aec81ed0sc9.js" defer></script>
<script src="polyfills.5c8ss3dfb7c3b4aass01.js" defer></script>
我不知道如何很好地解决这个问题。
如果我不想更改我的 Angular-SPA index.html,我需要在 aldrunewiki-le-ssl.conf 中使用别名,将 <DOMAIN>/filename.js 映射到我的 AldruneWikiFrontEnd 目录(Angular-SPA 所在的位置)。但这不会破坏我的 Django 应用程序的 url 命名空间,因为它看起来像这样:Alias / /home/isofruit/AldruneWikiFrontEnd/dist/frontend?
我放弃第 3 点并设置一个 gitook 来更改我的 Angular-SPA index.html 中的脚本标签会更好吗?
【问题讨论】:
标签: apache deployment