对于我的NGINX服务器,我设置了一个虚拟服务器来清理静态内容。 目前我正在设置,以便图像有一个到期date。 但是,当我为此创build一个位置指令时,所有内容都会导致404错误。
我现在的configuration是这样的:
/srv/www/static.conf
server { listen 80; server_name static.*.*; location / { root /srv/www/static; deny all; } location /images { expires 1y; log_not_found off; root /srv/www/static/images; } }
注意,这个文件包含在http指令中的/etc/nginx/nginx.conf中
我正在尝试访问图片,比方说… static.example.com/images/screenshots/something.png 。 果然,图片也存在于/srv/www/static/images/screenshots/something.png 。 然而,去说的地址不起作用,只是告诉我404 Not Found 。
但是,如果我删除location /images并更改location /以下…
location / { root /srv/www/static; }
有用! 我在这里做错了什么?
您的configuration符合nginxconfiguration陷阱您应该在configurationnginx之前阅读它。
要回答你的问题,你不应该定义root位置,定义一次,位置标签会自动让你分配访问特定的目录。
而不是定义图像目录的自定义根目录,使用try_files 。 $uri将映射/images/目录与/static/images/ 。
试试这个configuration:
server { listen 80; server_name static.*.*; root /srv/www; location /static/ { deny all; } location /images/ { expires 1y; log_not_found off; autoindex off; try_files $uri static/images$uri; } }