django - 如何在子路径中托管Django项目?

标签 django nginx django-rest-framework gunicorn

我正在使用Django REST framework构建API,该API通过Gunicorn和Nginx提供。项目“exampleproject”必须在子路径中运行,例如:https://100.100.100.100/exampleproject(示例IP地址)。我没有为该IP注册的域名。

当前,起始页按预期在https://100.100.100.100/exampleproject呈现。但是,“产品”的资源路径不起作用。起始页显示的不是https://100.100.100.100/exampleproject/products,而是https://100.100.100.100/products-不起作用。

我在exampleproject中配置了/etc/nginx/sites-enabled/default的子路径,如下所示:

server {

    # ...

    location /exampleproject/ {
        proxy_pass        http://localhost:8007/;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  Host $host;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;
    }

当我手动访问https://100.100.100.100/exampleproject/products时,Nginx在/var/log/nginx/access.log中记录以下内容:

"GET /products/ HTTP/1.1" 404 151 "-"

最佳答案

我发现here需要在settings.py中将以下设置添加到Django的配置中:

FORCE_SCRIPT_NAME = '/exampleproject'

这似乎重写了嵌套资源的所有路径。

关于django - 如何在子路径中托管Django项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28147916/

相关文章:

python - 在 Django 子类中而不是在基类中强制执行唯一的外键

python - 增强 django 模型以将外部数据合并到查询响应中

google-chrome - ERR_INSECURE_RESPONSE + 子域

ruby-on-rails - 无法在 Ubuntu Server 18.04 上安装 Phusion Passenger

ubuntu - Nginx 作为虚拟主机和反向代理

django - AWS Elastic Beanstalk 上的 Celery 配置问题 - "No config updates to processes"

python - 没有 csrf_token 的 Django 1.4

django - many=True TypeError 对象不可迭代

python - 在 Windows 上的 Python 3.5 上安装 Django-channels 时出错

python - Django休息框架: Issue using OrderingFilter and django-filter simultaneously