Skip to content

Instantly share code, notes, and snippets.

@chrisliuqq
Last active October 30, 2020 17:55
Show Gist options
  • Save chrisliuqq/61ef5ef605c626d4082c9cf209f244d7 to your computer and use it in GitHub Desktop.
Save chrisliuqq/61ef5ef605c626d4082c9cf209f244d7 to your computer and use it in GitHub Desktop.
nginx backend service proxy

original question

如果想要在 nginx server static file,先 proxy pass 到某一服務幫忙轉 webp 並且 serve webp 的服務,在 port 5555,因為害怕那個服務死掉,所以死掉時,要保留原本自動 alias 到原本的 image file
原本
    location /media/ {
        alias /home/scott/project-name/media/;
    } 
現在
    location /media/ {
        proxy_pass http://127.0.0.1:5555;
        alias /home/scott/project-name/media/;
    } 
但我試了,如果 port 5555 服務死掉,nginx 也只會噴 502,並不會回去執行 alias

solution

location @imgfile-url{
   alias /home/scott/project-name/media/;
}
location /media/ {
   proxy_pass http://127.0.0.1:5555;
   error_page 502 =200 @imgfile-url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment