如何使用nginx反向代理设置Django
我有一个Django项目,我已经在127.0.0.1:8888的开发服务器上启动并运行了该项目。我正在尝试使用nginx在我的vps上运行它,所以我可以在example.com/djangoApp上看到它 这是我的nginx.conf:如何使用nginx反向代理设置Django,django,nginx,Django,Nginx,我有一个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;
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;
}
location /djangoApp {
rewrite ^/djangoApp/(.*) /$1 break;
proxy_pass http://127.0.0.1:8888;
}
当我导航到example.com/djangoApp时,它抛出一个错误:“使用djangoApp.URL中定义的URLconf,Django尝试了以下URL模式,顺序如下:
/管理员
当前路径djangoApp/与这些路径中的任何一个都不匹配。“
我可以在settings.py中修改根url以缓解此问题吗?我通过添加到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;
}
location /djangoApp {
rewrite ^/djangoApp/(.*) /$1 break;
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;
}
}