NGINX:在proxy_pass中使用方案变量

ztyzrc3y  于 2023-03-01  发布在  Nginx
关注(0)|答案(1)|浏览(334)

我正尝试根据值**$host变量为proxy_pass规则使用不同的方案**。
然而,这似乎并不起作用,这是我们观察到的行为。
如果我们用proxy_pass https://$upstream;这样的常量模式设置 proxy_pass,一切都很好,但是如果我们尝试用自定义变量替换硬编码的模式值(https),然后使用proxy_pass $myscheme://$upstream;Nginx似乎忽略了$myscheme,并尝试在不使用模式的情况下解析$upstream,这显然是失败的。
即使我们将变量设置为set $myscheme https;,也会发生这种情况。
这种行为正常吗?我们做错了什么?有没有办法根据运行时设置的变量值使用不同的方案?

当前(不工作)配置

根据我们的测试,看起来(至少我们运行的版本)Nginx实际上没有替换 proxy_pass 中的变量

worker_processes 4;
worker_rlimit_nofile 100000;

events {
    worker_connections 100000;
    multi_accept on;
    use epoll;
}

http {

    log_format timed_combined '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $body_bytes_sent '
    '"$http_referer" "$http_user_agent" '
    '$request_time $upstream_response_time';

    server {
            listen 443;
            server_name myservername;

            ssl on;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_certificate /etc/letsencrypt/live/xxx;
            ssl_certificate_key /etc/letsencrypt/live/xxx;

            gzip on;
            gzip_comp_level 2;
            gzip_min_length 1000;
            gzip_proxied expired no-cache no-store private auth;
            gzip_types application/json text/json text/plain application/x-javascript text/xml text/css application/xml;
    
            access_log /var/log/nginx/access.log timed_combined;
    
            set $myscheme https;
            set $myhost myhostname;
    
            location / {
                     proxy_pass $myscheme://$myhost;
            }
      }

}

NGINX版本:一、10.三

fnvucqvd

fnvucqvd1#

这适用于前面提到的Nginx 1.10.3(和最新的1.23.3):

    • 一米一米一**
docker pull -q nginx:1.10.3 &>/dev/null && echo Docker image pulled
docker run -d --name nginx-test -p 8000:8000 nginx:1.10.3 1>/dev/null && echo Docker container started

echo -e "\nConfiguring Nginx..."

docker exec nginx-test bash -c <<END '
# Restarting Nginx kills the docker container. reload instead.
# Clear existing servers to prevent port binding errors.
NGINX_CONF=/etc/nginx/conf.d/default.conf
nginx -v
rm -f $NGINX_CONF
nginx -s reload 2>/dev/null && echo Nginx reloaded with no listeners

# Write test config
cat <<"ENDGINX" > $NGINX_CONF
server { 
  listen 127.0.0.1:80;

  default_type "text/plain";
  add_header x-scheme $scheme always;
  return 200;
}

server { 
  listen 8000 default_server;
  server_name _;

  set $pxy_proto $arg_proto;
  set $pxy_host  127.0.0.1;

  add_header x-proto $pxy_proto always;

  location / {
    proxy_pass $pxy_proto://$pxy_host;
  }
}
ENDGINX

nginx -s reload 2>/dev/null && echo Nginx reloaded with testing config
'
END

sleep 1 # Await Nginx startup

echo -e "\nTesting URLs...\n"
echo http://localhost:8000/?proto=http ; curl -sSI $_ | egrep '(HTTP|proto|scheme)'
echo
echo http://localhost:8000/?proto=FAIL ; curl -sSI $_ | egrep '(HTTP|proto|scheme)'
echo

docker -v stop nginx-test 1>/dev/null && echo Docker container stopped
docker -v rm nginx-test   1>/dev/null && echo Docker container removed
echo -e "\nDone! 🎉"
    • 结果**
Testing URLs...

http://localhost:8000/?proto=http
HTTP/1.1 200 OK
x-scheme: http
x-proto: http

http://localhost:8000/?proto=FAIL
HTTP/1.1 500 Internal Server Error
x-proto: FAIL

相关问题