【发布时间】:2019-01-22 13:20:38
【问题描述】:
我有一个 django 项目,其中包含 2 个应用程序,即 admin 和 api。
管理应用依赖于api 应用来访问模型。
我有 2 个子域,例如:admin.xxxx.com 和 api.xxxx.com。
该项目目前使用 gunicorn + nginx 部署在 AWS EC2 中。
更新
所有admin请求都通过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 not found on this server.
请提出问题所在?
【问题讨论】:
-
是的,你在 nginx 端执行此操作,请遵循以下指南:digitalocean.com/community/tutorials/…
-
@Ahtisham 请检查我更新的问题。那里有什么问题?我无法调试问题。
-
:) 我认为您可以为此定义多个服务器模块,请查看文档:nginx.org/en/docs/http/ngx_http_core_module.html#server_name
-
当您说
proxy_pass http://13.***.***.***:8000/admin/;时,需要将其映射到您的应用程序,这可以使用 Gunicorn 完成。它是您的应用程序和 nginx 之间的接口。 -
我认为对这篇文章 stackoverflow.com/questions/12858674/… 的第二个回答会有所帮助。
标签: django amazon-web-services subdomain