A
A
alestro2020-11-20 23:17:09
PHP
alestro, 2020-11-20 23:17:09

How to redirect the request to the handler if the file is not found?

There is a need to give files through a php script.
Currently, if the file is not found, nginx returns 404 not found. How can I redirect a request to a php handler if the file doesn't exist? Tried with try_files $uri $uri/ /index.php?$query_string - but no effect.

A piece of the nginx 1.17 config:

# Force HTTPS
add_header Strict-Transport-Security 'max-age=2592000' always;
if ($scheme ~* ^(?!https).*$) {
   return 301 https://$host$request_uri;
}

# Force www.site.com => site.com
# if ($host ~* ^www\.(.+)$) {
#    return 301 $scheme://$1$request_uri;
# }

# Disable access to backup/config/command/log files
# if ($uri ~* ^.+\.(?:bak|co?nf|in[ci]|log|orig|sh|sql|tar|sql|t?gz|cmd|bat)$) {
#    return 404;
# }

# Disable access to hidden files/folders
if ($uri ~* /\.(?!well-known)) {
    return 404;
}

# Disable MIME sniffing
add_header X-Content-Type-Options 'nosniff' always;

location ~* ^.+\.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv|svgz?|ttf|ttc|otf|eot|woff2?)$ {
        expires 1d;
        access_log off;
}

location / {
    # Force index.php routing (if not found)
    try_files $uri $uri/ /index.php?$query_string;

    # Force index.php routing (all requests)
    rewrite ^/(.*)$ /index.php?/$1 last;

    location ~ \.php$ {
        try_files      $fastcgi_script_name =404;

        # limit_conn   addr 16;
        # limit_req    zone=flood        burst=32 nodelay;

        # add_header   X-Frame-Options   'SAMEORIGIN' always;
        # add_header   Referrer-Policy   'no-referrer-when-downgrade' always;

        # CSP syntax: <host-source> <scheme-source>(http: https: data: mediastream: blob: filesystem:) 'self' 'unsafe-inline' 'unsafe-eval' 'none'
        # Content-Security-Policy-Report-Only (report-uri https://site.com/csp/)
        # add_header   Content-Security-Policy  "default-src 'self'; connect-src 'self'; font-src 'self'; frame-src 'self'; img-src 'self'; manifest-src 'self'; media-src 'self'; object-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; base-uri 'none'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests" always;

        fastcgi_pass   backend;
        include        '%sprogdir%/userdata/config/nginx_fastcgi_params.txt';
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question