dsjojts9734 2014-09-19 15:34
浏览 36
已采纳

Nginx向后端发送丢失图像的请求

I'm working on a website that has copies of the same images in various sizes e.g.

/images/200x100/someimage.jpg

/images/400x200/someimage.jpg

etc.

The images are served directly by Nginx and only php requests get passed to the fastcgi.

If an image can't be found I would like to pass the request to fastcgi so that I can see if we can generate a version of the image in the correct size and then return that.

I just can't get it working - if the image is missing I can get it to call the script I want (instead of just returning a 404) BUT it is just returning the source of the php script.

This is the relevant part of my conf file:

location ~ (^|/)\. {
              return 404;
    }

    location /imgs {
          location ~ \.php$ {return 403;}
    }

    #Static Contents
    location ~* ^.+.(jpg|jpeg|png|gif|bmp)$ {
        try_files $uri  /$uri @backend;
        add_header Pragma "public";
        add_header Cache-Control "public";
        expires     1y;
        access_log  off;
        log_not_found off;
    }

    # Static Contents
    location ~* ^.+.(ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|eot|woff|svg|htc)$ {
        add_header Pragma "public";
        add_header Cache-Control "public";
        expires     1y;
        access_log  off;
        log_not_found off;
    }

    location / {
                # Check if a file exists, or route it to index.php.
                try_files $uri $uri/ /index.php?$query_string;
    }

    location ~\.php$ {
        try_files $uri =404;
                fastcgi_pass unix:/dev/shm/apache-php.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;
    }

    location @backend {
        #return 410;
        rewrite  ^(.*)$ /image.php?url=$1;
        fastcgi_pass unix:/dev/shm/apache-php.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

A missing image will then pass into the location @backend but I can't pass the $uri to a script.

I've been through all kinds of variations what am I doing wrong? Any suggestions gratefully appreciated! Thanks.

Update

I've got this working in another VM perfectly (I copied the backend block and images blocks into the other conf file) - the only difference is that the one that works is using Apache instead of fastcgi.

  • 写回答

1条回答 默认 最新

  • doukang5966907 2014-09-22 08:41
    关注

    The problem was a just a typo.

    I was trying to rewrite to /image.php it should have been /images.php

    The working version of the above conf file is as follows:

    location ~ (^|/)\. {
              return 404;
    }
    
    location /imgs {
          location ~ \.php$ {return 403;}
    }
    
    #Static Contents
    location ~* ^.+.(jpg|jpeg|png|gif|bmp)$ {
        try_files $uri  /$uri @backend;
        add_header Pragma "public";
        add_header Cache-Control "public";
        expires     1y;
        access_log  off;
        log_not_found off;
    }
    
    # Static Contents
    location ~* ^.+.(ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|eot|woff|svg|htc)$ {
        add_header Pragma "public";
        add_header Cache-Control "public";
        expires     1y;
        access_log  off;
        log_not_found off;
    }
    
    location / {
                # Check if a file exists, or route it to index.php.
                try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~\.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/dev/shm/apache-php.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
    
    location @backend {
    
        rewrite  ^(.*)$ /images.php?url=$1;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP