大网数据广告活动
服务器运维问答

nginx route all 404 requests to a php script

· 52CE 我爱测

I want to route all 404 requests to a php script, How should I do that? My nginx config is: server { listen 81; listen [::]:81; root /srv/http/paste.lan/www; autoindex on; client_max_body_size 20M; index index.txt index.html inde…

问题原文

原帖链接 · hanshenrik · CC BY-SA 4.0

I want to route all 404 requests to a php script, How should I do that? My nginx config is:

server {
    listen 81;
    listen [::]:81;
    root /srv/http/paste.lan/www;
    autoindex on;
    client_max_body_size 20M;
    index index.txt index.html index.htm index.php;
    server_name paste.lan;
    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    }


    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny all;
    }
}

Things I have tried:

Attempt #1:

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.php;
    }
  • This only works for URI's that does not end with .php , for example /DoesNotExist.ph is passed to index.php , but /DoesNotExist.php get the standard nginx 404 page.

Attempt #2:

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    error_page 404 /index.php;

This sort-of works, all 404 requests are passed to index.php but this forces the response code to be 404, even if index.php contains:

<?php
http_response_code(200);
die("index.php");

it will still be served with response code 404 :(

Attempt #3:

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    error_page 404 =200 /index.php;

This also sort-of works, all 404 requests are passed to index.php but this forces the response code to be 200, even if index.php contains:

<?php
http_response_code(400);// HTTP 400 Bad Request
die("index.php");

it will still be served as HTTP 200 OK :(

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 4.0

The error_page directive includes an option to change the response code to another.

From the manual page:

If an error response is processed by a proxied server or a FastCGI/uwsgi/SCGI/gRPC server, and the server may return different response codes (e.g., 200, 302, 401 or 404), it is possible to respond with the code it returns:

error_page 404 = /404.php;

You should use:

error_page 404 = /index.php;

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-03-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。