Henry
发布于 2024-04-04 / 151 阅读
0
0

Nginx反代Docker私有镜像服务

背景简介

使用Nginx 反向代理 Docker私有镜像服务。

环境配置

  1. nginx/1.22.1

详细步骤

新建配置文件:docker-repo.conf

Nginx 默认自动包含 /etc/nginx/conf.d 目录下的配置文件,因此在此目录下创建配置文件即可

添加配置

server {
		# 配置域名
        server_name repo.sample.com;
        # 监听端口
        listen 80;
  


		#允许上传任意大小的文件
        client_max_body_size 0;
        # log 配置
        access_log /var/log/nginx/private_docker_repo_access.log;
        error_log /var/log/nginx/private_docker_repo_error.log;

        location / {
                # 设置代理服务器的地址端口
                proxy_pass http://127.0.0.1:30001;
                # proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                #设置代理超时时间
                proxy_read_timeout 600s;
                proxy_send_timeout 600s;
                
                # 缓冲设置
                proxy_buffering off;
                proxy_request_buffering off;

                # GZIP 压缩
                gzip on;
                gzip_vary on;
                gzip_proxied any;
                gzip_types text/plain application/json;

        }
}

注意事项:

  • 连接超时:为了处理长时间运行的操作,如大型镜像的上传和下载,您需要增加Nginx的连接超时设置。
  • 缓冲:对于大文件传输,您可能需要调整Nginx的缓冲设置,以避免消耗过多的内存。
  • GZIP压缩:虽然对于镜像层本身压缩可能不会有太大帮助,但是对于Docker仓库的API响应,启用GZIP压缩可以减少传输数据的大小。
  • 日志:配置访问日志和错误日志,以便于监控和故障排查。
  • 安全:除了启用SSL/TLS之外,还可以考虑添加额外的安全头,如X-Content-Type-Options、X-Frame-Options、Content-Security-Policy等。
  • 缓存:虽然对于私有仓库来说,缓存可能不是主要考虑因素,但如果您的使用场景适合,可以配置Nginx作为缓存服务器。

请勿添加proxy_set_header Host $host;,否则会产生 Upload failed: unknown blob 报错。
验证配置文件是否正确:

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启 nginx 服务

# systemctl restart nginx

至此,Nginx 代理配置已完成。


评论