Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx proxy and 404 redirect

Hi I would like configure my nginx server to proxy for amazon S3 and do something like mod_rewrite in apache - if proxy for amazon is 404 (file does'nt exist on amazon) then redirect me to my local file. It's possibble to do ?

This is my nginx config file:

upstream app{
 server 127.0.0.1:3000;
}

server {
  listen 0.0.0.0:80;
  server_name www.mypage.com mypage.com;
  access_log /var/log/nginx/mypagecom.log;

  location /photos{
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://amazons3.mypage.com/photos;
    proxy_redirect off;
    error_page 404 /myerrorfile.jpg;
  }
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://app;
    proxy_redirect off;
  }


}

Can anyone help me ?

like image 658
Kim Yu Avatar asked Nov 26 '11 13:11

Kim Yu


People also ask

What is nginx proxy redirect?

Nginx provides proxy_redirect directive which can be used in http, server, or location context. The syntax is: proxy_redirect redirect replacement. In this example, the proxied server (upstream Apache or Lighttpd) returned line Location: http://server1.cyberciti.biz:8080/app/.

Can Nginx use proxy?

The PROXY protocol enables NGINX and NGINX Plus to receive client connection information passed through proxy servers and load balancers such as HAproxy and Amazon Elastic Load Balancer (ELB). With the PROXY protocol, NGINX can learn the originating IP address from HTTP, SSL, HTTP/2, SPDY, WebSocket, and TCP.


1 Answers

Add

proxy_intercept_errors on;

to location /photos. Then your error_page 404 /myerrorfile.jpg will work even when the 404 error comes from the upstream server.

like image 56
Sergey Vlasov Avatar answered Nov 16 '22 03:11

Sergey Vlasov