在 AWS EC2 的多个子域上部署一个包含多个应用程序的 django 项目
Deploy one django project with multiple app on multiple subdomains on AWS EC2
我有一个 Django 项目,其中包含 2 个应用程序,即 admin
和 api
。
管理应用依赖于 api
应用来访问模型。
我有 2 个子域,例如:admin.xxxx.com
和 api.xxxx.com
。
该项目目前使用gunicorn + nginx部署在AWS EC2中。
更新
所有管理员请求都传递给:some.ip.address.0:8000/admin/
,所有 api 请求都传递给 some.ip.address.0:8000/
有什么方法可以将我的 some.ip.address.0:8000/admin/ 指向 admin.xxxx.com 和 some.ip.address.0:8000/ 到 api.xxxx.com?
更新 2:
myproject_nginx.conf 文件:
upstream myproject_backend_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/home/ubuntu/myproject_backend/myproject_backend.sock fail_timeout=0;
}
server{
listen 80;
listen [::]:80;
server_name admin.mydomain.in;
location / {
proxy_pass http://13.***.***.***:8000/admin/;
}
location /static/ {
alias /home/ubuntu/myproject_backend/static/;
}
location /media/ {
alias /home/ubuntu/myproject_backend/media/;
}
}
server {
listen 80;
server_name 13.***.***.***:8000 api.mydomain.in www.api.mydomain.in;
client_max_body_size 4G;
location /static/ {
alias /home/ubuntu/myproject_backend/static/;
}
location /media/ {
alias /home/ubuntu/myproject_backend/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
# Try to serve static files from nginx, no point in making an
# *application* server like Unicorn/Rainbows! serve static files.
if (!-f $request_filename) {
proxy_pass http://myproject_backend_server;
break;
}
}
}
我的项目urls.py 文件:
from django.urls import path, re_path, include
from django.conf.urls.static import static
from django.conf import settings
from django.views.static import serve
urlpatterns = [
re_path(r'^', include('api_app.urls')),
...
path('admin/', include('admin_app.urls')),
...
re_path(r'^static/(?P<path>.*)$', serve,
{'document_root': settings.STATIC_ROOT}),
re_path(r'^media/(?P<path>.*)$', serve,
{'document_root': settings.MEDIA_ROOT}),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
它打开我的管理员登录页面,但我尝试登录时显示:/admin/admin在此服务器上找不到。
有什么问题请指教?
是的,为了做到这一点,您必须将其中两个域指向托管您的 django 应用程序的 EC2 实例(如果您使用的是 ELB,则为 ELB)并配置 Nginx,以便它将请求从一个域重定向到 admin
从其他到 api
路径。
据我所知,当用户在浏览器中输入此地址 http://admin.mydomain.in
时,您希望显示一个管理页面,并且此管理页面由您的 django 应用程序处理,因此您使用 nginx 代理 http://13.***.***.***:8000/admin/
从那里可以访问您的管理页面。
但这里的问题是您的应用程序不知道该怎么做。所以它需要一个专门用于此目的的中间人(在你的例子中是 Gunicorn )。此外,nginx 不能直接与您的 django 应用程序通信,因为它不打算提供动态内容。
因此,要解决此问题,您需要配置 gunicorn 以便它侦听地址 http://13.***.***.***:8000
到 nginx 将转发请求的位置。然后在这个地址上使用 运行 gunicorn 并将参数作为您的应用程序名称。您可以阅读 post serving a request from gunicorn 的第二个答案来配置您的 gunicorn 文件。
我有一个 Django 项目,其中包含 2 个应用程序,即 admin
和 api
。
管理应用依赖于 api
应用来访问模型。
我有 2 个子域,例如:admin.xxxx.com
和 api.xxxx.com
。
该项目目前使用gunicorn + nginx部署在AWS EC2中。
更新
所有管理员请求都传递给:some.ip.address.0:8000/admin/
,所有 api 请求都传递给 some.ip.address.0:8000/
有什么方法可以将我的 some.ip.address.0:8000/admin/ 指向 admin.xxxx.com 和 some.ip.address.0:8000/ 到 api.xxxx.com?
更新 2:
myproject_nginx.conf 文件:
upstream myproject_backend_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/home/ubuntu/myproject_backend/myproject_backend.sock fail_timeout=0;
}
server{
listen 80;
listen [::]:80;
server_name admin.mydomain.in;
location / {
proxy_pass http://13.***.***.***:8000/admin/;
}
location /static/ {
alias /home/ubuntu/myproject_backend/static/;
}
location /media/ {
alias /home/ubuntu/myproject_backend/media/;
}
}
server {
listen 80;
server_name 13.***.***.***:8000 api.mydomain.in www.api.mydomain.in;
client_max_body_size 4G;
location /static/ {
alias /home/ubuntu/myproject_backend/static/;
}
location /media/ {
alias /home/ubuntu/myproject_backend/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
# Try to serve static files from nginx, no point in making an
# *application* server like Unicorn/Rainbows! serve static files.
if (!-f $request_filename) {
proxy_pass http://myproject_backend_server;
break;
}
}
}
我的项目urls.py 文件:
from django.urls import path, re_path, include
from django.conf.urls.static import static
from django.conf import settings
from django.views.static import serve
urlpatterns = [
re_path(r'^', include('api_app.urls')),
...
path('admin/', include('admin_app.urls')),
...
re_path(r'^static/(?P<path>.*)$', serve,
{'document_root': settings.STATIC_ROOT}),
re_path(r'^media/(?P<path>.*)$', serve,
{'document_root': settings.MEDIA_ROOT}),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
它打开我的管理员登录页面,但我尝试登录时显示:/admin/admin在此服务器上找不到。
有什么问题请指教?
是的,为了做到这一点,您必须将其中两个域指向托管您的 django 应用程序的 EC2 实例(如果您使用的是 ELB,则为 ELB)并配置 Nginx,以便它将请求从一个域重定向到 admin
从其他到 api
路径。
据我所知,当用户在浏览器中输入此地址 http://admin.mydomain.in
时,您希望显示一个管理页面,并且此管理页面由您的 django 应用程序处理,因此您使用 nginx 代理 http://13.***.***.***:8000/admin/
从那里可以访问您的管理页面。
但这里的问题是您的应用程序不知道该怎么做。所以它需要一个专门用于此目的的中间人(在你的例子中是 Gunicorn )。此外,nginx 不能直接与您的 django 应用程序通信,因为它不打算提供动态内容。
因此,要解决此问题,您需要配置 gunicorn 以便它侦听地址 http://13.***.***.***:8000
到 nginx 将转发请求的位置。然后在这个地址上使用 运行 gunicorn 并将参数作为您的应用程序名称。您可以阅读 post serving a request from gunicorn 的第二个答案来配置您的 gunicorn 文件。