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;
}
Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58

1 Answers1

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;
} 
Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
  • location pics/profile is working but PHP execution prevention block isnt working., anyways thanks for your input. –  Jul 13 '15 at 10:12
  • I had one extra `^` inside the `location` regex for PHP, it should work now. – Tero Kilkanen Jul 13 '15 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 ? –  Jul 13 '15 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.. – Tero Kilkanen Jul 13 '15 at 10:26