【问题标题】:nginx proxy_pass to https / image-server not workingnginx proxy_pass 到 https / image-server 不起作用
【发布时间】:2020-02-22 17:11:32
【问题描述】:

我正在尝试代理图像服务器 imagekit.io,以便所有请求都具有相同的域名。

已经在HTTPS nginx服务器(带有自签名证书)甚至HTTP服务器中尝试了多种配置。

假设网址是https://ik.imagekit.io/hj8sm3kk7/brochures/92/1579/suzuki-gsx-r150-615123.pdf

使用以下配置,我正在尝试点击http://localhost.com:8800/brochures/92/1579/suzuki-gsx-r150-615123.pdf

server {
        listen       8800;
        server_name  localhost.com;

        location /brochures {   
            proxy_ignore_headers Set-Cookie;
           proxy_set_header Host ik.imagekit.io;                       
           # proxy_set_header X-Real-IP $remote_addr;
           # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Pragma no-cache;
           proxy_set_header Accept $http_accept;
           proxy_set_header User-Agent $http_user_agent;
            proxy_set_header Accept-Encoding $http_accept_encoding;
            proxy_set_header Accept-Language $http_accept_language;
            proxy_set_header sec-fetch-mode navigate;
            proxy_set_header sec-fetch-site cross-site;
            proxy_set_header sec-fetch-user ?1;
            proxy_set_header :authority ik.imagekit.io;
            proxy_set_header :method GET;
            proxy_set_header :path $path;
            proxy_set_header :scheme https;
            proxy_set_header Upgrade-Insecure-Requests 1;

           proxy_pass https://ik.imagekit.io/hj8sm3kk7/brochures;
        }
}

但是这有效:

http://localhost.com:8800/financial-advisor/mfs-investment-management-review打开与https://smartasset.com/financial-advisor/mfs-investment-management-review相同的页面

location /financial-advisor {                        
           proxy_ignore_headers Set-Cookie;
           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_pass https://smartasset.com;
       }

【问题讨论】:

    标签: nginx proxy webserver nginx-reverse-proxy proxypass


    【解决方案1】:

    真正的问题是 ik.imagekit.io 的 SSL 问题,在 proxy_pass 值中使用 http:// 而不是 https:// 会有所帮助。


    我给出的原始(和错误)答案:


    第一个示例需要对 URL 路径进行操作。考虑一个类似的方法:

    location ~ ^/(brochures/.*) {
      # all the previous config you used should be added here
      proxy_pass  https://ik.imagekit.io/hj8sm3kk7/$1;
    }
    

    这会将请求代理到正确的 URL 路径,该路径与原始请求不同。 $1 是请求 URL 路径中与第一个括号 (...) 匹配的部分。

    【讨论】:

    • 不起作用。还尝试了下面的配置和 url localhost.com:8800/hj8sm3kk7/brochures/92/1579/…> location /hj8sm3kk7 { # all other config; proxy_pass https://ik.imagekit.io; } 那也不起作用。我猜还有其他问题。
    • 嗯考虑启用日志记录以查看确切的代理 URL?将$proxy_host$upstream_addr 添加到access_log,看看你的请求会发生什么。
    • 不知何故$proxy_host$upstream_addr 都在给-。使用了这个日志格式log_format upstreamlog '[$time_local] $remote_addr - $remote_user - $server_name to: "$upstream_addr": "$request" "$request_method $scheme://$proxy_host$request_uri $server_protocol"';我得到的日志是[27/Oct/2019:01:27:06 +0530] 127.0.0.1 - - - localhost.com to: "-": "GET /brochures/92/1579/suzuki-gsx-r150-615123.pdf HTTP/1.1" "GET http://-/brochures/92/1579/suzuki-gsx-r150-615123.pdf HTTP/1.1"这个日志是你建议的位置配置。
    • 似乎规则不适用。尝试添加~,这样你就有location ~ ^/hj8sm3kk7,没有它只有http://localhost/hj8sm3kk7 被代理。 NOPE 以上不正确,忘记我说过了。
    • 尝试使用 location ~ ^/hj8sm3kk7 { #other config; proxy_pass https://ik.imagekit.io; } 和 url localhost.com:8800/hj8sm3kk7/brochures/92/1579/…>。还是一样的错误:(
    猜你喜欢
    • 2019-01-22
    • 1970-01-01
    • 2013-02-26
    • 2018-09-28
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    相关资源
    最近更新 更多