Nginx proxy pass uses the $host variable

Keywords: Linux

A related company is an e-commerce system for SAAS. It has a requirement that resources hosted under the domain name folder in Alibaba cloud OSS need to be accessed through the customer's domain name.
For example, user access
http://mall.shop.com/base.css, actually visit http://aliyun-oss.aliyuncs.com/mall.shop.com/base.css
http://www.mall.com/banner.jpg, in fact, is to visit http://aliyun-oss.aliyuncs.com/www.mall.com/banner.jpg

upstream sh_aliyun {
        server aliyun-oss.aliyuncs.com:80 weight=10 max_fails=3 fail_timeout=10s;
}

server {
        listen 0.0.0.0:80;
        server_name _;
        location / {
             proxy_redirect off ;
             proxy_set_header Host  aliyun-oss.aliyuncs.com;   #Fill in OSS access domain name
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Real-Port $remote_port;
             proxy_set_header REMOTE-HOST $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             client_max_body_size 50m;
             client_body_buffer_size 256k;
             proxy_connect_timeout 30;
             proxy_send_timeout 30;
             proxy_read_timeout 60;
             proxy_buffer_size 256k;
             proxy_buffers 4 256k;
             proxy_busy_buffers_size 256k;
             proxy_temp_file_write_size 256k;
             proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404;
             proxy_max_temp_file_size 128m;
             proxy_pass http://SH ﹣ aliyun / $host $request ﹣ URI; fill in the variable $host here, which is the host name accessed, and the complete request parameters of $request ﹣ URI.
        }
}

Stepped pits:
Direct use
proxy_pass http://aliyun-oss.aliyuncs.com/$host$request_uri ; always requested 502
proxy_pass http://106.14.228.198/$host$request_uri ; this is OK

Therefore, it is concluded that:
If there are variables in proxy pass, you can't use the form of domain name. You can only use upstream.

Posted by luv2climb on Sat, 02 Nov 2019 08:55:03 -0700