2

i am trying to serve images which are stored in different location other than document root.

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

i have added above code block in the nginx .conf file and did

systemctl restart nginx 

and when i tried to access

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

it gives me 404 not found error.

how can i fix this ? i have specified document root at the top of nginx configuration file like this

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

i have checked and the file exists at

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

update :

my full config is like this

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; } 
5
  • Show full config Commented Jul 13, 2015 at 7:51
  • @AlexeyTen updated with full config file Commented Jul 13, 2015 at 8:03
  • location ~* \.(?:ico|css|js|gif|jpe?g|png)$ matches request so it never get to your "avatar" location. Commented Jul 13, 2015 at 8:06
  • @AlexeyTen i commented out those lines, restarted nginx and still getting 404 thanks Commented Jul 13, 2015 at 8:15
  • Read the answer here serverfault.com/questions/629406/nginx-rewite-rules-403-error/… specifically the section under the edit Commented Jul 13, 2015 at 8:40

1 Answer 1

1

The location block you use works like this:

The url http://your-site.com/pics/profile/pic.jpg is served from /home/data/site_data/profile/pic.jpg/pic.jpg.

This is because alias refers to the directory where the file is served from.

If you want to serve /home/data/site_data/profile/pic.jpg on that URL, you can use this location block:

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

Also, I would use a location like this for PHP execution prevention:

location ^~ ^/(cache|content|css|images|js|pics)/.+\.php$ { return 404; } 
4
  • location pics/profile is working but PHP execution prevention block isnt working., anyways thanks for your input. Commented Jul 13, 2015 at 10:12
  • I had one extra ^ inside the location regex for PHP, it should work now. Commented Jul 13, 2015 at 10:17
  • still able to execute php inside cache. anyways thats not the issue, here , i would like to ask , is there more work for os if nginx had to rewrite something which is not available under public html ? like what i am trying to achieve ? Commented Jul 13, 2015 at 10:22
  • Well, all extra rewrites add some load to the server, but it is not much at all, so one doesn't really need to care about that, except for really busy sites. I am just perfectionist.. Commented Jul 13, 2015 at 10:26

You must log in to answer this question.