Назад | Перейти на главную страницу

Nginx dynamic PHP 404 (с перезаписью URL)

Я пытаюсь настроить динамические страницы 404, чтобы я мог сохранить URL-адрес отсутствующего содержимого, показать его на странице 404 и сохранить согласованность шаблона моего сайта.

Вот мой сайт:

server {
    listen       80;
    server_name  www.example.com;

    client_max_body_size 1024M;
    client_body_buffer_size 512M;

    if ($http_host !~* "^www\.example\.com"){
       set $rule_0 1$rule_0;
    }

    if ($rule_0 = "1"){
        rewrite ^/(.*)$ http://www.example.com/$1 permanent;
        break;
    }

    rewrite ^/([a-zA-Z0-9-_]+)$ /profile.php?url=$1 last;

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 1y;
        log_not_found off;
    }

    location / {
        root   /var/www/html/example.com;
        index  index.php index.html index.htm;
    }

    location ~ \.php$ {
        root           /var/www/html/example.com;
        fastcgi_pass  example_fast_cgi;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/example.com$fastcgi_script_name;
        include        fastcgi_params;
    }

    error_page 404 = @handler;

    location @handler {
        root           /var/www/html/example.com;
        try_files $uri /404.php = 404;   
    }

    location ~ /\.ht {
        deny  all;
    }
}

Кажется, что он почти готов, но все страницы, на которых должна отображаться страница 404, получают приглашение загрузить необработанный файл PHP. Добавление параметров FastCGI ничего не дало.

Это должно делать свое дело.

server {
    listen 80;
    server_name example.com;
    return 301 $scheme://www.$server_name$request_uri;
}

server {
    listen 80;
    server_name www.example.com;
    root /var/www/html/example.com;
    index index.php index.html index.htm;
    error_page 404 /404.php;
    location / {
        rewrite ^/([a-zA-Z0-9-_]+)$ /profile.php?url=$1 last;
        location ~ /\. {
            return 404;
        }
        location ~* ^.+\.php$ {
            include fastcgi_params;
            fastcgi_pass php;
         }
    }
}