I have faced a problem. In my app there is lot's of article. So, this is not possible to remove the broken images from article body. So, I am thinking to remove that from server end. My server is nginx server.

I need to replace those images with a valid image if the is return 404

example.com(My server)
abc.com(cdn server/external link)

<img src="example.com/invalid.jpg"> will replace to <img src="https://example.com/1px.jpg"> 
<img src="abc.com/invalid.jpg"> will replace to <img src="https://example.com/1px.jpg"> 

I have tried the following which not working for me or I can not do it properly.

location ~ \.(png|jpg)$ {
    error_page 404 /sites/default/files/inline-images/1x1.png;
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
    alias /efs_share/nfs_data/data/files;
    try_files $uri $uri/ @drupal;
    error_page 404 /sites/default/files/inline-images/1x1.png;

Is there any way which will be replace any kind of 404 image whatever this is external or internal, this is not matter

  • This should be handled at the application layer if possible. Just throwing out a couple of ideas: in application file use regex to detect bad image and then send the updated one to template. Or, use if/else statement in the template language to switch them out, as you use the HTML in your example.
    – Jake 1986
    Jul 9 at 22:36
  • @Jake1986 Can you explain a little bit more or with an example? Jul 10 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.