莫名其妙的Nginx配置
首先,我有一个图片,位置存放在 /var/www/joker/storage/app/public/images/laravel-echo.png
:
此时我想让访问 http://localhost/storage/images/laravel-echo.png
时,读取上面的那个图片。此时我就需要加一个 Nginx 的 location 配置:
location /storage/ {
alias /var/www/joker/storage/app/public/;
autoindex on;
}
加入以后,我重启了 Nginx。访问图片地址时,居然将图片地址重定向到了 http://localhost/storage/images/laravel-echo.png/
,在 URI 后面多了一个「 / 」斜杠。然后就返回 404
了,百思不得姐啊。下面是我的 Nginx 配置信息:
# 80 端口
server {
listen 80;
server_name localhost;
root /var/www/joker/public;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /storage/ {
alias /var/www/joker/storage/app/public/;
autoindex on;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 443 端口
server {
listen 443 ssl;
server_name localhost;
root /var/www/joker/public;
index index.html index.htm index.php;
ssl_certificate /etc/nginx/cert/5518746_localhost.pem;
ssl_certificate_key /etc/nginx/cert/5518746_localhost.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /storage/ {
alias /var/www/joker/storage/app/public/;
autoindex on;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
问题:#
为什么 80 端口会重定向到 URI 末尾加「 / 」的地址?如从:#
http://localhost/storage/images/laravel-echo.png
重定向到:
http://localhost/storage/images/laravel-echo.png/
为什么 443 端口没有问题?#
答案:#
浏览器缓存…… 尴尬。下面粘一个浏览器的加载流程图吧,转自网络:
第一次访问图片地址,未找到文件,便 301
重定向到新的地址。此时,第一次的请求结果已经缓存到浏览器。之后的每一次访问都会读取浏览器的缓存。图片的缓存需要通过浏览器的设置选项删除,开发者工具的 storage 中无效。
推荐文章: