2

I have a media folder on URL example.com/media/ and I want to deny users running scripts in my server. My server app is nginx and in How to deny script execution inside writable directories I couldn't find anything special about specific URL. This is my nginx server config:

# sites-avalaible/default
server {
        listen  80;
        listen  443 ssl;
        server_name     www.example.com example.com;
        ssl_certificate /var/www/example/ssl/ssl.crt;
        ssl_certificate_key     /var/www/example/ssl/ssl.key;
        location /static  {
                alias   /var/www/example/static;
                expires 7d;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";

        }
        location /media  {
                alias  /var/www/example/media;
                # limit download speed after 5mb download
                limit_rate_after 5m;
                limit_rate 120k;
                limit_req zone=lh burst=5 nodelay;
        }
        location / {
                proxy_pass      http://127.0.0.1:8000;
        }
}

I want to deny running any executable stuff in my media URL in which users can upload their files to server. How can I do that? For example when user navigates to example.com/media/bomb.py nginx return 404 error page. I've also changed media folder executing permissions but I need to do it for nginx in order to stop viewing script files.

hamidfzm
  • 4,595
  • 8
  • 48
  • 80
  • Your current configuration already do not allow script execution in media directory. You don't need to do anything – Alexey Ten Mar 09 '14 at 14:33

3 Answers3

3

This will disable all php and py script execution inside media directory. Like this you can disable any other script.

 location /media/ { 
    location ~ .*\.(php)?$ 
    { 
      deny all; 
    } 
    location ~ .*\.(py)?$ 
    { 
      deny all; 
    }
 }
Harikrishnan
  • 9,688
  • 11
  • 84
  • 127
1

Edit: If support for other script types are added later, it would be safer to only serve recognized media types and deny the rest:

location ^~ /media/ {
    location ~ \.(?:jpg|png|gif)$ {}
    return 403; # or 404
}
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35
-2

Location for media to prevent execution of php

location ^~ /media/ {}

AnmolNagpal
  • 387
  • 2
  • 8