开发者

Nginx static media, and problems with trailing slashes

开发者 https://www.devze.com 2023-03-10 17:17 出处:网络
Sigh... just when I thought I had figured out all the issues with trailing slashes in URLs for Django - and I start working with nginx...

Sigh... just when I thought I had figured out all the issues with trailing slashes in URLs for Django - and I start working with nginx...

So I'm configuring nginx to serve static media, and failing repeatedly - despite my config looking exactly like all the other static-media questions on SO. Eventually I realize that it's not the nginx config, but my HTML file, which includes a trailing slash on the .css file:

# hello.html (invalid)
<link rel="stylesheet" href="/media/css/hello.css/" type="text/css" />
# resulting log error
[error] 27705#0: "/home/www/static/css/hello.css/index.html" is not found 
request: "GET /media/css/hello.css/ HTTP/1.1"开发者_如何学Python
# hello.html (valid)
<link rel="stylesheet" href="/media/css/hello.css" type="text/css" />

By removing the trailing slash on the filename, it worked fine. But why? Shouldn't URLs end in trailing slashes?

I recently went through all my Django templates, adding slashes to every media file. Do I have to remove them all, or is there some configuration option in nginx that I'm missing?


Having the slash in the request will make most servers assume that you want the hello.css folder in the css folder. Obviously, that's going to confuse it.

Shouldn't URLs end in trailing slashes?

Nope. Do a view-source for this page, or almost any other.

<link rel="shortcut icon" href="http://cdn.sstatic.net/stackoverflow/img/favicon.ico">

See? No trailing slash.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号