如何使用nginx反向代理设置Django

4

我有一个Django项目,目前在本地开发服务器(127.0.0.1:8888)上运行良好。我想将其部署到我的VPS上,并使用nginx使其可以通过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模式: /admin 当前路径djangoApp/与上述任何一个都不匹配。" 我能否修改settings.py中的根URL来缓解这个问题?
2个回答

6
我通过在nginx.conf文件中添加以下内容来解决这个问题:
location /djangoApp {
    rewrite  ^/djangoApp/(.*) /$1 break;
    proxy_pass http://127.0.0.1:8888;
}

感谢这个 Stack Overflow 的问题交流


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/;
                }
         }

上述方法应该有效。您漏掉了代理传递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;
                }
         }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接