亲戚公司是做SAAS的电商系统,有一个需求,需要通过客户的域名访问托管在阿里云OSS中该域名文件夹下的资源。
比如用户访问的
http://mall.shop.com/base.css,其实是访问 http://aliyun-oss.aliyuncs.com/mall.shop.com/base.css
http://www.mall.com/banner.jpg,其实是访问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; #填写OSS访问域名 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; #这里填写变量$host,是访问的主机名,$request_uri完整的请求参数 } }
直接使用
proxy_pass http://aliyun-oss.aliyuncs.com/$host$request_uri; 一直请求都是502
proxy_pass http://106.14.228.198/$host$request_uri; 这个是可以的
因此得出结论:
proxy_pass中如果存在变量,是不能使用域名的形式,只能借助upstream
转自
(27条消息) Nginx proxy_pass使用$host变量_chaoqianggao4414的博客-CSDN博客
https://blog.csdn.net/chaoqianggao4414/article/details/100969309?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link&utm_relevant_index=2