仅当Nginx不存在文件时,才将URL重定向到PHP [英] Redirect URL to PHP only if file does not exist with Nginx

查看:281
本文介绍了仅当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;
}

这篇关于仅当Nginx不存在文件时,才将URL重定向到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆