【问题标题】:How to set up Django with nginx reverse proxy如何使用 nginx 反向代理设置 Django
【发布时间】:2020-12-23 18:59:27
【问题描述】:

我有一个 Django 项目,我在 127.0.0.1:8888 的开发服务器上启动并运行该项目。我正在尝试让它在我的带有 nginx 的 vps 上运行,所以我可以在 example.com/djangoApp 上看到它。

这是我的 nginx.conf:

server {
    server_name example.com;
            location /otherLocation/ {
                    proxy_pass http://127.0.0.1:10000;
            }

            location /djangoApp/ {
                     proxy_pass http://127.0.0.1:8888;
            }

当我导航到 example.com/djangoApp 时,它会抛出一个错误:“使用 djangoApp.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式: /行政 当前路径 djangoApp/ 与其中任何一个都不匹配。”

我可以修改 settings.py 中的根 url 来缓解这种情况吗?

【问题讨论】:

    标签: django nginx


    【解决方案1】:

    我通过添加到 nginx.conf 解决了这个问题:

    location /djangoApp {
        rewrite  ^/djangoApp/(.*) /$1 break;
        proxy_pass http://127.0.0.1:8888;
    }
    

    感谢this SO exchange

    【讨论】:

      【解决方案2】:
              server {
                     server_name example.com;
                      location /otherLocation/ {
                             proxy_pass http://127.0.0.1:10000/;
                      }
      
                      location /djangoApp/ {
                             proxy_pass http://127.0.0.1:8888/;
                      }
               }
      

      以上应该可以。您在 proxy_pass url 末尾缺少“/”

      或者,你可以这样做

               server {
                     server_name example.com;
                      location /otherLocation {
                             proxy_pass http://127.0.0.1:10000;
                      }
      
                      location /djangoApp {
                             proxy_pass http://127.0.0.1:8888;
                      }
               }
      

      【讨论】:

        猜你喜欢
        • 2016-08-09
        • 2016-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-11
        • 1970-01-01
        相关资源
        最近更新 更多