在 apache 中部署工作 django 应用程序时出现 URL 重定向问题(通过 mod_wsgi)
我有一个 django 应用程序,可以在 django 开发服务器下完美运行。我正在尝试使用 mod_wsgi 将其部署在 apache2.2 中,但出现错误。
在 httpd.conf 文件中,我使用以下命令将我的应用程序“安装”在根目录 /myapp 下:
WSGIScriptAlias /myapp /home/path_to_my_app/apache_conf/django.wsgi
我已按照标准指示准备 django.wsgi。现在,我可以访问应用程序的主页模板,但似乎存在重定向错误。 “/myapp”根目录不会自动插入 ULR 重定向请求。主页面模板通过urls.py向views.py中部分模块请求无法正确下发。
apache 错误日志:
127.0.0.1 - - [21/Feb/2011:16:11:44 +0100] "GET /myapp/ HTTP/1.1" 200 1795
127.0.0.1 - - [21/Feb/2011:16:11:46 +0100] "GET /api/dir HTTP/1.1" 404 205
“/api/dir”不存在,它应该与 urls.py 中的模式匹配,但 id 没有
我已经花了超过 2 天的时间使用它,请有人帮忙吗?
I've a django app that works perfectly under the django development server. I'm trying to deploying it in apache2.2 using the mod_wsgi and I have errors.
In the httpd.conf file I "mounted" my app under the root /myapp using:
WSGIScriptAlias /myapp /home/path_to_my_app/apache_conf/django.wsgi
I've followed the standard indication to prepare django.wsgi. Now, I can reach the main page template of my app but it seems to have redirection errors. The "/myapp" root is not inserted automatically on ULRs redirection requests. Request from the main page template to some modules in views.py via urls.py cannot be correctly delivered.
apache error log:
127.0.0.1 - - [21/Feb/2011:16:11:44 +0100] "GET /myapp/ HTTP/1.1" 200 1795
127.0.0.1 - - [21/Feb/2011:16:11:46 +0100] "GET /api/dir HTTP/1.1" 404 205
"/api/dir" doesn't exist, it should be matched from a pattern in urls.py, but id doesn't
I've spent more than 2 days with it, please can somebody help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来您的 apache 配置中缺少一些内容:
在我的服务器的工作配置中,我有:
您的 WSGIScriptAlias 行是正确的,但您需要告诉 wsgi 您将如何运行您的应用程序。
您很可能收到 404 错误,因为没有 WSGIDaemonProcess 行告诉 WSGI 处理程序如何使用您的进程。
http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives可能是一个有用的参考。
It appears you are missing some things from your apache config:
In the working config for my server I have:
Your WSGIScriptAlias line is correct but you need to tell wsgi how you are going to run your application.
You are most likely getting 404's because there is not a WSGIDaemonProcess line to tell the WSGI handler how to work with your process.
http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives might be a helpful reference.
最后我发现了错误。它没有连接到 httpd.conf 文件,而是连接到如何将 URL 指定给 django urls.py 文件和模板。
当我以这种方式安装 myapp 时:
我相信 django 模板文件中指定的 URL 应该带有初始斜杠,如下所示:
'/api/dir'
这样一来,应用程序只能在 django 开发服务器上运行,而不能在 apache 上运行。
相反,如果您使用不带开头斜杠的 URL,例如:
'api/dir'
该应用程序在 django 开发服务器和 apache 上都能正常工作!
即使在 django urls.py 文件的模式匹配中,也必须避免使用起始斜杠:
像这样:
(r'^api/dir$', 'available_services')
而不是这样的:
(r'^/api/dir$', 'available_services')
也许这对于 django 专家用户来说是显而易见的事情,但如果你是像这样的新手对我来说,这可能会让您损失一定的时间,因为这是一个很难检测到的问题。
Finally I found the error. It was not connected to httpd.conf file but to how URLs are specified both to django urls.py file and to templates.
As I mounted myapp in this way:
I was believing that URLs specified in django template files should carry an initial slash, like this:
'/api/dir'
It results that in this way the application works only on django development server but not on apache.
Instead if you use URLs without initial slash like:
'api/dir'
The app works correctly both on django development server and on apache!
You must avoid using starting slashes even on pattern matching of django urls.py file:
like this:
(r'^api/dir$', 'available_services')
and NOT like this:
(r'^/api/dir$', 'available_services')
Maybe this is an obvious thing for expert django users but if you're novice like me, this can make you loose a certain amount of time because it's a hard problem to be detected.
实际上,情况比这要复杂一些。
有时您需要绝对的应用内 url,因为您不想访问相对于当前 url 的 url。
例如,您在主布局模板中有一个全局菜单(即由项目中的每个其他模板“扩展”的模板):您不能拥有诸如“home”、“contact”、“about”、“blog”之类的相对网址“因为如果您位于 /yourapp/blog/entries/2014/02/06/add (假设存在这样的网址)并且单击任何菜单项,您将转到(例如,对于“home”)/yourapp/blog /entries/2014/02/06/add/home 而不是 /home,这是您所期望的。
虚拟主机的丑陋解决方案在这里很好(在Java中,Struts 2有一个选项来解析“动作”的url(即命名的url/入口点),包括部署在Web容器(例如tomcat)中的部署上下文目录)。 ..不知道您可以猜测有任何“上下文”设置(也许可以将当前的“WSGIScriptAlias”键作为环境变量返回)。之后,使用回 url 的初始斜杠(“/home”)。
Actually it is a bit more complicated than just that.
There are some times when you need absolute in-app urls, because you don't want to reach an url which is relative to the current url.
e.g. you have a global menu in a main layout template (i.e. a template which is "extend"ed by each other template in your project): you cannot have relative urls like "home", "contact", "about", "blog" because if you are in /yourapp/blog/entries/2014/02/06/add (let's say such url exist) and you click any menu item, you would go to (say, for "home") /yourapp/blog/entries/2014/02/06/add/home instead to /home, which you would expect.
The ugly solution of virtual hosts is good here (in Java, Struts 2 has an option to resolve urls for "actions" (i.e. named urls / entry points) including the deploy context directory as deployed in the web container (e.g. tomcat)) ... don't know it there's any "context" setting you can guess (perhaps it's possible to return the current "WSGIScriptAlias" key as an environment variable). After that, using back the initial slash for urls ("/home").