简单的nginx别名在尝试提供文档根以外的文件时不工作

我正在尝试提供存储在文档根以外的其他位置的图像。

#avatar files block location ~^/pics/profile/(.*)$ { alias /home/data/site_data/profile/$1; } 

我在nginx .conf文件中添加了上面的代码块,并做了

 systemctl restart nginx 

当我试图访问

 http://www.example.com/pics/profile/1/1.jpg 

它给了我404没有发现错误

我怎样才能解决这个问题 ? 我已经在这个nginx configuration文件的顶部指定了文档根目录

 root /usr/share/nginx/site.com/html; 

我已经检查,并存在的文件

 /home/data/site_data/profile/1/1.jpg 

更新:

我的完整configuration是这样的

 server { listen 80; server_name example.com; charset utf-8; client_max_body_size 6M; root /home/data/nginx/example.com/html; location / { index index.php index.html; } #error_page 403 404 500 502 503 504 /404.html; location ~* \.(?:ico|css|js|gif|jpe?g|png)$ { expires 1M; access_log off; add_header Cache-Control "public"; } access_log /dev/null; #restrict php execution from these directories location ^~ /cache/ {location ~* \.php$ { return 404; }} location ^~ /content/ {location ~* \.php$ { return 404; }} location ^~ /css/ {location ~* \.php$ { return 404; }} location ^~ /images/ {location ~* \.php$ { return 404; }} location ^~ /js/ {location ~* \.php$ { return 404; }} location ^~ /pics/ {location ~* \.php$ { return 404; }} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.(php)$ { try_files $uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #avatar files block location ~^/pics/profile/(.*)$ { alias /home/data/site_data/profile/$1; } include /etc/nginx/example.com_rewrite_rules.conf; } 

你使用的location块是这样的:

urlhttp://your-site.com/pics/profile/pic.jpg来自/home/data/site_data/profile/pic.jpg/pic.jpg

这是因为alias是指文件所在的目录。

如果您想在该URL上提供/home/data/site_data/profile/pic.jpg ,则可以使用以下位置块:

 location /pics/profile { alias /home/data/site_data/profile; } 

另外,我会使用这样的位置来防止PHP执行:

 location ^~ ^/(cache|content|css|images|js|pics)/.+\.php$ { return 404; }