Django的双斜线在URL中的问题(多Web服务器,Apache和Nginx的) [英] Django Double Slash in URLS Issue (Multiple Webservers, Apache and Nginx)
本文介绍了Django的双斜线在URL中的问题(多Web服务器,Apache和Nginx的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要看到一个例子:加载一个example.com,点击的链接到/测试,而不是去到http链接:// example.com/test/~~V它去到http:// www.example。网//测试/
或者,如果你登录时,会自动生成Django管理部分职位登录表单//管理,而不是管理。
好像这是一个Django的问题,但我改变了唯一的事情就是nginx的。
的附加注释夫妇(增加10月31日):
- 这个问题似乎非常相似的<一个href=\"http://serverfault.com/questions/134863/nginx-fastcgi-problems-with-django-double-slashes-in-url\">http://serverfault.com/questions/134863/nginx-fastcgi-problems-with-django-double-slashes-in-url但不同的是,我得到项目的根//管理/与管理//前加上斜杠。在成功的解决方案还没有帮我。
- 我使用的Django的当前SVN版本。
下面是nginx的fastcgi_conf:
#fastcgi_param SCRIPT_FILENAME $ DOCUMENT_ROOT $ fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $ fastcgi_script_name;
fastcgi_param QUERY_STRING QUERY_STRING $;
fastcgi_param REQUEST_METHOD $ REQUEST_METHOD;
fastcgi_param CONTENT_TYPE $ CONTENT_TYPE;
fastcgi_param CONTENT_LENGTH $ CONTENT_LENGTH;fastcgi_param SCRIPT_NAME $ fastcgi_script_name;
fastcgi_param REQUEST_URI $ REQUEST_URI;
fastcgi_param DOCUMENT_URI $ DOCUMENT_URI;
fastcgi_param DOCUMENT_ROOT $ DOCUMENT_ROOT;
fastcgi_param SERVER_PROTOCOL $ SERVER_PROTOCOL;fastcgi_param GATEWAY_INTERFACE CGI / 1.1;
fastcgi_param SERVER_SOFTWARE nginx的/ $ nginx_version;fastcgi_param REMOTE_ADDR $ REMOTE_ADDR;
fastcgi_param REMOTE_PORT $ REMOTE_PORT;
fastcgi_param SERVER_ADDR $ SERVER_ADDR;
fastcgi_param SERVER_PORT $ SERVER_PORT;
fastcgi_param SERVER_NAME $服务器名称;fastcgi_param PATH_INFO $ fastcgi_script_name;#PHP只,如果PHP与--enable-力的cgi-重定向需要建
fastcgi_param REDIRECT_STATUS 200;
的Django项目配置
服务器{
听80;
SERVER_NAME www.site.net; 位置 / {
fastcgi_pass UNIX:/path/to/site/server.sock;
包括fastcgi.conf;
访问日志/var/log/nginx_django.log为主;
} 位置^〜/管理/ $ {
fastcgi_pass UNIX:/path/to/site/server.sock;
包括fastcgi.conf;
访问日志/var/log/nginx_django.log为主;
} 位置〜* ^ + \\(MPG | AVI | MP3 | SWF |拉链| TGZ |广州| RAR | BZ2 | DOC | XLS | EXE | PPT | TXT |焦油|中期| MIDI | WAV | RTF | MPEG)。)
$ {
根/路径/要/网站/媒介;
limit_rate 2000K;
ACCESS_LOG /var/log/nginx_django_media.log下载;
ACCESS_LOG关闭;
} 位置^〜/静态/ {
根/路径/要/部位;
ACCESS_LOG /var/log/nginx_django_static.log下载;
到期30D;
} 位置/403.html {
根的/ opt / nginx的;
ACCESS_LOG关闭;
} 位置/401.html {
根的/ opt / nginx的;
ACCESS_LOG关闭;
} 位置可以找到/404.html {
根的/ opt / nginx的;
ACCESS_LOG关闭;
} 位置= {/_.gif
empty_gif;
ACCESS_LOG关闭;
}}
解决方案
fastcgi_param SCRIPT_NAME'';
这解决了 //
我的问题。
To see an example: load up a example.com, click on a link that's linking to /test and instead of going to http:// example.com/test/ it goes to http:// www.example.net//test/
Or if you login, the login form for the auto-generated django admin section posts to //admin instead of admin.
Seems like this is a django issue, but the only thing I changed was nginx.
Couple of additional notes (added Oct 31):
- This problem seems remarkably similar to http://serverfault.com/questions/134863/nginx-fastcgi-problems-with-django-double-slashes-in-url but the difference is that I'm getting slashes added before the project root "//admin/" vs. "admin//". The successful solution there hasn't helped me.
- I'm using the current SVN version of Django.
Here is the nginx fastcgi_conf:
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
Django project config
server {
listen 80;
server_name www.site.net;
location / {
fastcgi_pass unix:/path/to/site/server.sock;
include fastcgi.conf;
access_log /var/log/nginx_django.log main;
}
location ^~ /admin/$ {
fastcgi_pass unix:/path/to/site/server.sock;
include fastcgi.conf;
access_log /var/log/nginx_django.log main;
}
location ~* ^.+\.(mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg))
$ {
root /path/to/site/media;
limit_rate 2000K;
access_log /var/log/nginx_django_media.log download;
access_log off;
}
location ^~ /static/ {
root /path/to/site;
access_log /var/log/nginx_django_static.log download;
expires 30d;
}
location /403.html {
root /opt/nginx;
access_log off;
}
location /401.html {
root /opt/nginx;
access_log off;
}
location /404.html {
root /opt/nginx;
access_log off;
}
location = /_.gif {
empty_gif;
access_log off;
}
}
解决方案
fastcgi_param SCRIPT_NAME '';
This solve the //
problem for me.
这篇关于Django的双斜线在URL中的问题(多Web服务器,Apache和Nginx的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文