Django + uWSGI + nginx url映射 [英] Django + uWSGI + nginx url mapping
问题描述
我将使用Django作为一个API服务,应该存在于这个链接上:
项目。 test / api
Django项目本身是空白(1.9.6),只是创建了一个应用程序,迁移并创建了一个超级用户。
我的项目结构看起来像这个:
/ vagrant / api / here-lives-whole-django
/ vagrant / api / uwsgi_params
/ vagrant / frontend / some-frontend-工作分离自django
我的NGINX设置如下所示:
上游测试{
server 127.0.0.1:8000;
}
server {
listen 80;
server_name www.testing.test;
charset utf-8;
client_max_body_size 75M;
access_log /var/log/nginx/www.testing.test.access.log;
error_log /var/log/nginx/www.testing.test.error.log;
location / api / {
include / vagrant / api / uwsgi_params;
uwsgi_pass测试;
}
location / {
root / vagrant / frontend;
}
}
uwsgi_params文件:
uwsgi_param QUERY_STRING $ query_string;
uwsgi_param REQUEST_METHOD $ request_method;
uwsgi_param CONTENT_TYPE $ content_type;
uwsgi_param CONTENT_LENGTH $ content_length;
uwsgi_param REQUEST_URI $ request_uri;
uwsgi_param PATH_INFO $ document_uri;
uwsgi_param DOCUMENT_ROOT $ document_root;
uwsgi_param SERVER_PROTOCOL $ server_protocol;
uwsgi_param REQUEST_SCHEME $ scheme;
uwsgi_param HTTPS $ https if_not_empty;
uwsgi_param REMOTE_ADDR $ remote_addr;
uwsgi_param REMOTE_PORT $ remote_port;
uwsgi_param SERVER_PORT $ server_port;
uwsgi_param SERVER_NAME $ server_name;
我的Django网址格式:
urlpatterns = [
url(r'^ admin /',admin.site.urls),
]
Atm。我使用以下命令运行Django:
uwsgi --socket:8000 --wsgi-file wsgi.py
我可以达到Django,但是我尝试访问
www.testing.test / api / admin
我得到一个404(从Django调试)。
找不到页面(404)
请求方法:GET
请求URL:http://www.testing.test/api/admin
使用在testing.urls中定义的URLconf,Django按照以下顺序尝试了这些URL模式:
^ admin /
当前的URL,api / admin与之不匹配。
我知道对于习惯这些东西的人来说,这将是一件非常简单的事情,原谅我,因为Im新的。
Ps。我发现一些类似的问题,人们使用
uwsgi_param SCRIPT_NAME / api;
uwsgi_modifier1 30;
但这只是让我的Django告诉我一个404与
请求URL:http://www.testing.test/api/api/admin
当我请求
请求URL:http://www.testing.test / api / admin
我很难理解你的问题但基本上恢复到这一点:我想要 nginx
将所有调用传递给 / api /
到django,但是django正在接收
nginx窍门( / api /
不适用于内部网址)
我会使用 nginx
的重写来解决这个问题
location / api / {
rewrite ^ / api /(.*)/ $ 1 break;
include / vagrant / api / uwsgi_params;
uwsgi_pass测试;
}
它将删除 / api /
部分URL,并保留在块中 uwsgi_pass
到Django。
django解决方案(在我看来很脏)
上面的工作是第一个URL进入django。然而,django对于被删除的前缀是无能为力的,并且无法为自己生成URL。这可能不是一些简单的API中的问题,但这将是一个更常见的问题。
我猜,唯一可行的方法是将前缀添加到基本URLconf。
from django.conf.urls import include
mypatterns = [
url(r'^ admin /',admin.site.urls),
]
urlpatterns = [
url(r'^ api /',include(mypatterns)) ,
]
虽然,我觉得这是一个非常脏的解决方案。
I want to run Django with uWSGI behind a NGINX.
I will use Django as a API service which should live on this link: project.test/api The Django project itself is blank (1.9.6), just created a app, migrated and created a superuser.
My Project structure looks like this:
/vagrant/api/here-lives-whole-django
/vagrant/api/uwsgi_params
/vagrant/frontend/some-frontend-work-seperated-from-django
My NGINX setup looks like this:
upstream testing {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name www.testing.test;
charset utf-8;
client_max_body_size 75M;
access_log /var/log/nginx/www.testing.test.access.log;
error_log /var/log/nginx/www.testing.test.error.log;
location /api/ {
include /vagrant/api/uwsgi_params;
uwsgi_pass testing;
}
location / {
root /vagrant/frontend;
}
}
The uwsgi_params file:
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
My Django url pattern:
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
Atm. Im running Django with following command:
uwsgi --socket :8000 --wsgi-file wsgi.py
Im able to reach Django but when I try to access
www.testing.test/api/admin
I get a 404 (debug from Django).
Page not found (404)
Request Method: GET
Request URL: http://www.testing.test/api/admin
Using the URLconf defined in testing.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, api/admin, didn't match any of these.
I know it will be something very simple for people who are used to this stuff, forgive me because Im new.
Ps. I found some similar questions where people worked with
uwsgi_param SCRIPT_NAME /api;
uwsgi_modifier1 30;
But this just made my Django tell me a 404 with
Request URL: http://www.testing.test/api/api/admin
when I requested
Request URL: http://www.testing.test/api/admin
I had trouble to understand your question but basically it resumes to this: I want nginx
to pass all calls to /api/
to django but django is receiving the URL with /api/
prefixed.
nginx trick (does not work for internal URLs)
I'd use nginx
's rewrite to solve this
location /api/ {
rewrite ^/api/(.*) /$1 break;
include /vagrant/api/uwsgi_params;
uwsgi_pass testing;
}
It shall drop the /api/
part of the URL and remain in the block to uwsgi_pass
into Django.
django solution (dirty in my opinion)
The above works for the first URL going into django. Yet, django is clueless about the prefix that was removed and will have trouble to generate URLs to itself. That might not be a problem in some simple APIs but it would be a problem more often than not.
I guess, the only viable way is to add the prefix to the base URLconf.
from django.conf.urls import include
mypatterns = [
url(r'^admin/', admin.site.urls),
]
urlpatterns = [
url(r'^api/', include(mypatterns)),
]
Although, I feel this is a very dirty solution.
这篇关于Django + uWSGI + nginx url映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!