Apache服务于Django和Angular



我有一个由Django Rest API提供服务的Angular前端项目。我的项目采用以下结构。

example.com
|- client (holds angular files)
|- server (holds Django Rest Framework files)

角度应用程序通过http调用drf:

example.com/api/<params>

我正在使用Apache和uWSGI托管在Linode Ubunutu 17.04上。 我似乎无法找出同时为 Angular 和 Django 提供服务的最佳方式?我可以轻松地使用 WSGI 配置托管 Django Rest API,但无法弄清楚 Apache 如何知道指向 Angular 来处理正常请求。

以下解决方案有什么问题,或者有更好的方法吗?

在/etc/apache2/sites-available/example.com.conf 中

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/client/
</VirtualHost>
<VirtualHost *:80>
ServerName example.com/api
ServerAlias www.example.com/api
WSGIScriptAlias / var/www/example.com/index.wsgi
DocumentRoot /var/www/example.com/server/
</VirtualHost>

任何帮助将不胜感激!

我不知道这是否是最好的方法,但它对我有用:

<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ProxyPass /api/ http://localhost:8000/api/
ProxyPassReverse /api/ http://localhost:8000/api/
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
# Server
<VirtualHost *:8000>
<Directory var/www/example.com/server>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
# mod_wsgi configuration for django app
# python-home represents a location of venv, in this case same as application path, but it's not neccessarily the same
# python-path represents a location of application
WSGIDaemonProcess my_app python-home=var/www/example.com/server python-path=var/www/example.com/server
WSGIProcessGroup my_app
WSGIScriptAlias / var/www/example.com/server/wsgi.py
</VirtualHost>
# Client
<VirtualHost *:8080>
DocumentRoot /var/www/example.com/client
<Directory /var/www/example.com>
Require all granted
</Directory>
</VirtualHost>

通过在/etc/apache2/ports.conf中添加以下行来侦听端口 8000 和 8080:

# Internal: server
Listen 8000
# Internal: client
Listen 8080

说明:侦听端口 80 的 VirtualHost 充当代理,根据请求的 URL 将请求进一步重定向到不同的端口。如果 URL 以/api/开头,则请求将被重定向到端口 8000(内部(,而端口 8000 又由另一个虚拟主机处理。如果 URL 不以/api/开头,则请求将被重定向到代表客户端的端口 8080。

在这个解决方案中,我使用的是mod_wsgi而不是uWSGI,但该方法的工作方式相同(只有服务器虚拟主机的配置会有所不同(。

最新更新