doudouwen2763
2013-10-20 19:15
浏览 150
已采纳

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

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;
    }
}

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

图片转代码服务由CSDN问答提供 功能建议

我试图让Nginx重写URL空间./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  ; 
} 
 
位置〜\ .php $ {
 fastcgi_pass 127.0.0.1:9000; 
 fastcgi_index index.php; 
包括fastcgi_params; 
 fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; 
 fastcgi_param HTTPS off  ; 
} 
} 
   
 
 

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dounai6613 2013-10-20 19:44
    已采纳

    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;
    }
    
    已采纳该答案
    打赏 评论
  • dongxia527680 2013-10-21 08:36

    Well if it's always /data I would create a specific location for it, like this

    location ~ /data/(.+).png {
        try_files $uri /serveImage.php?guid=$1;
    }
    

    Try this and tell me if it works.

    打赏 评论

相关推荐 更多相似问题