A
A
Ad4ptec2015-05-18 23:12:47
Nginx
Ad4ptec, 2015-05-18 23:12:47

How to give all files in a directory a 404 error?

Initially, it was necessary to change the folder by redirects. The code below works, but it is necessary that all files in the /old/ directory give a 404 error, and all files in the /files/ directory must be available.

set $domain $host;
  # делаем доступными php файлы из /old/
  location ~ ^/files/(.*\.php)$ {
    root /home/$domain/html/old/$1;
    try_files $uri @php;
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
  }
  # остальные файлы из /old/
  location ~ ^/files($|/.*) {
    root /home/$domain/html/old$1;
  }
  location @php {
    rewrite ^/files/(.*)$ /old/$1 last;
  }

Tried the code below, doesn't work.
location ~ ^/old($|/.*) {
    if ($remote_addr != 127.0.0.1) {
      return 404;
    }
  }

The problem can certainly be solved by proxying apache, but this is the most extreme option, because the extra load on the server is not needed. Tell me who faced similar situations, share your experience, I will be very grateful.
Full server config:
server {
  server_name "~^www\.(.*)$" ;
  return 301 $scheme://$1$request_uri ;
}
server {
    listen 8080;
  listen 80 default;
  server_name _;
  set $domain $host;
  root /home/$domain/html;
  server_name_in_redirect off;
  set_real_ip_from   199.27.128.0/21;
  set_real_ip_from   173.245.48.0/20;
  set_real_ip_from   103.21.244.0/22;
  set_real_ip_from   103.22.200.0/22;
  set_real_ip_from   103.31.4.0/22;
  set_real_ip_from   141.101.64.0/18;
  set_real_ip_from   108.162.192.0/18;
  set_real_ip_from   190.93.240.0/20;
  set_real_ip_from   188.114.96.0/20;   
  set_real_ip_from   197.234.240.0/22;
  set_real_ip_from   198.41.128.0/17;
  set_real_ip_from   162.158.0.0/15;
  set_real_ip_from   104.16.0.0/12;
  set_real_ip_from   2400:cb00::/32;
  set_real_ip_from   2606:4700::/32;
  set_real_ip_from   2803:f800::/32;
  set_real_ip_from   2405:b500::/32;
  set_real_ip_from   2405:8100::/32;
  real_ip_header     CF-Connecting-IP;
  real_ip_recursive on;
  
  if (!-d /home/$domain/html) {
    return 444;
  }
  
  location ~ ^/files/(.*\.php)$ {
    alias /home/$domain/html/old/$1;
    try_files $uri @php;
    include fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
  }
  location ~ ^/files($|/.*) {
    alias /home/$domain/html/old$1;
  }
  location @php {
    rewrite ^/files/(.*)$ /old/$1 last;
  }
    location ~ \.php$ {
    if ($request_uri = /index.php) {
      rewrite ^ $scheme://$domain? permanent;
    }
    try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
    }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2015-05-19
@Ad4ptec

In general, the following code should be enough to solve your question:

location ~* ^/old/ {
  return 404;
}

O
Oleg Soldatov, 2015-05-19
@Soldata

Not files give errors, but the server. Deny folder access in .htaccess

A
Ad4ptec, 2015-05-19
@Ad4ptec

Oleg Soldatov , there is no such file, the server is running on nginx.
I figured it out, Alexey Ten was right, the whole problem was in rewrite. In the fastcgi_params configuration file, the path to the scripts was incorrectly spelled out, so a double slash was put in the path to the script.
As a result, we get the correct solution:

set $domain $host;
  # делаем доступными php файлы из /old/
  location ~ ^/files/(.*\.php)$ {
    alias /home/$domain/html/old/$1;
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
  }
  # остальные файлы из /old/
  location ~ ^/files($|/.*) {
    alias /home/$domain/html/old$1;
  }
  location ~* ^/old/ {
    return 404;
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question