supereagle / experiences

Summary of practical experience in work.
2 stars 0 forks source link

NGINX redirects 404 errors to an external URL #25

Closed supereagle closed 7 years ago

supereagle commented 7 years ago

Background

Want to migrate the Docker Registry from the old one(R1) to the new one(R2). R1 already has many old images, R2 is empty but will be the only registry after R1 deprecated in the future. All new images will be pushed into R2. When pull images, directly pull them from R2 if found in R2, otherwise pull from R1. There is a Nginx(Harbor) in front of them, and distributes the image requests to Docker Registry. The requests are firstly directed to R2. If the image is not found in R2, there will be an 404 error, then Nginx needs to redirect this request to R1.

Example

nginx.conf

worker_processes  1;
error_log  logs/error.log;

pid        E:/nginx-1.6.3/logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        # Docker registry migration
        error_page  404 =301 https://github.com$request_uri;
    }
}

Test The test url http://localhost/abcde will be directed to https://github.com/abcde. The $request_uri in nginx.conf makes the request uri kept when directed.

References