仅当Nginx不存在文件时,才将URL重定向到PHP

问题描述:

我正试图让Nginx重写URL空间./data/(.+).png到serveImage.php?guid = $ 1

I am trying to get Nginx to rewrite URL space ./data/(.+).png to serveImage.php?guid=$1

server {
    server_name my.server;
    listen 80;
    listen 127.0.0.1:80;
    root /var/www/my.server;
    index index.html;

    location / {
        try_files $uri $uri/ index.html;
        rewrite ^/data/(.+).png serveImage.php?guid=$1 last;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }
}

我做错了什么? serveImage.php在文档根目录中确实存在.

What am I doing wrong? serveImage.php does exist in the document root.

重写似乎没有按计划进行(access.log或error.log似乎都没有暗示该规则已被捕获).我制作了一个更通用的路由器,它可能也可以更好地满足其他未知需求.

Rewriting did not seem to work as planned (nothing that appeared to access.log or error.log gave even a hint that the rule was even caught). I made a more generic router that might fit better the other yet unknown needs as well.

location / {
    try_files $uri $uri/ @router;
    index index.html index.php;
    error_page 403 = @router;
    error_page 404 = @router;
}

location @router {
    rewrite ^(.*)$ /router.php?$1;
}