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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题