问题
I am setting an Nginx configuration with my Django project.
In order to provide some static file without /static/
showing on the URL,
I add some rewrite rules in Nginx configuration.
Here is part of my Nginx configuration:
location /static/ {
location ~* \.(png|jpg|jpeg|gif|css|js)$ {
access_log off;
expires 30d;
}
alias /path/to/myproject/static/;
}
location ~ ^/favicon.ico$ {
rewrite ^/favicon.ico$ /static/favicon.ico;
}
location /foo/ {
rewrite ^/foo/(.*)$ /static/abc/$1;
}
location /bar/ {
rewrite ^/bar/(.*)$ /static/bar/$1;
}
location / {
fastcgi_pass myproject;
include django_fastcgi_params;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
When I access https://myproject.com/foo, it will show 404 page of Django. I think it is because there is no matching location in Nginx (should end up with slash /
) and no matching URL in Django's urls.py
.
When I access https://myproject.com/foo/, it will show index.html
under myproject/static/foo/
folder, or it will be 403 Forbidden if there is no index.html
.
But I found that...
When I access https://myproject.com/foo/abc, it will 301 Moved Permanently to https://myproject.com/static/foo/abc/
When I access https://myproject.com/foo/abc/, it will directly show https://myproject.com/foo/abc/, which is the index.html
file under myproject/static/abc/
Why these two URLs work differently?
Is there any modification I should do?
回答1:
Modify APPEND_SLASH=False
setting in the settings.py
of django project. For more information see this - https://docs.djangoproject.com/en/2.2/ref/settings/#append-slash
来源:https://stackoverflow.com/questions/55981805/nginx-configuration-with-static-files-under-django