Amazon web services Nginx反向代理不使用动态代理\u pass

Amazon web services Nginx反向代理不使用动态代理\u pass,amazon-web-services,nginx,proxy,reverse-proxy,Amazon Web Services,Nginx,Proxy,Reverse Proxy,我试图使用nginx反向代理来代理AWS API,并且遇到了一个问题,即如果我静态地定义上游服务器,它工作得很好,但是如果我尝试动态地生成它,它会抛出一个502坏网关错误。我不确定如何解决这个问题 /etc/nginx/nginx.conf user www-data; worker_processes auto; pid /run/nginx.pid; events { worker_connections 768; # multi_accept on; }

我试图使用nginx反向代理来代理AWS API,并且遇到了一个问题,即如果我静态地定义上游服务器,它工作得很好,但是如果我尝试动态地生成它,它会抛出一个502坏网关错误。我不确定如何解决这个问题

/etc/nginx/nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        ##
        # Logging
        ##

        log_format upstreamlog '[$time_local] $host $remote_addr - $remote_user - $server_name to: $upstream_addr ($upstream_http_name): $request upstream_response_time $upstream_response_time msec $msec request_time $request_time';

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}
/etc/nginx/sites enabled/default

server {
        listen 80;

        server_name (.*)\.(.*)\.(.*)\.colinaws\.com (.*)\.(.*)\.colinaws\.com (.*)\.colinaws\.com;

        access_log /var/log/nginx/proxy.log upstreamlog;

        location / {
                if ($host ~* (.*)\.colinaws\.com) {
                        proxy_pass http://$1.amazonaws.com;
                }
                if ($host ~* (.*)\.(.*)\.colinaws\.com) {
                        proxy_pass http://$1.$2.amazonaws.com;
                }
                if ($host ~* (.*)\.(.*)\.(.*)\.colinaws\.com) {
                        proxy_pass http://$1.$2.amazonaws.com;
                }
                proxy_set_header Host $host;
                proxy_pass_header Authorization;
                proxy_pass_header X-Amz-Target;
                proxy_pass_header X-Amz-Date;
                proxy_pass_header User-Agent;
                proxy_pass_header Content-Type;
                proxy_pass_header Content-Length;
                proxy_pass_header Accept-Encoding;
        }
}
在上面的配置中,它不起作用,但是如果我继续将代理过程静态定义为AWS端点(比如说),它就可以正常工作。

您需要使用

server {
        listen 80;

        server_name (.*)\.(.*)\.(.*)\.colinaws\.com (.*)\.(.*)\.colinaws\.com (.*)\.colinaws\.com;

        access_log /var/log/nginx/proxy.log upstreamlog;

        location / {
                if ($host ~* (.*)\.colinaws\.com) {
                        proxy_pass http://$1.amazonaws.com$request_uri$is_args$args;
                }
                if ($host ~* (.*)\.(.*)\.colinaws\.com) {
                        proxy_pass http://$1.$2.amazonaws.com$request_uri$is_args$args;
                }
                if ($host ~* (.*)\.(.*)\.(.*)\.colinaws\.com) {
                        proxy_pass http://$1.$2.amazonaws.com$request_uri$is_args$args;
                }
                proxy_set_header Host $host;
                proxy_pass_header Authorization;
                proxy_pass_header X-Amz-Target;
                proxy_pass_header X-Amz-Date;
                proxy_pass_header User-Agent;
                proxy_pass_header Content-Type;
                proxy_pass_header Content-Length;
                proxy_pass_header Accept-Encoding;
        }
}

proxy\u pass
中使用变量时,还需要提供完整的uri和参数

这里的问题是
proxy\u pass
没有设置为独立变量,也没有设置
解析器
标志。这两项的添加迫使nginx在每次传递时重新解析URL,而不是在开始时缓存IP地址。我的最终(稳定)配置如下所示:

server {
        # Listen on port 80, this will be updated to port 443 once ssl is enabled
        listen 80;

        # Server names should map to incoming requests so we can regex process out the service and
        # region later
        server_name (.*)\.(.*)\.(.*)\.colinaws\.com (.*)\.(.*)\.colinaws\.com;

        # Log the incoming information for debug purposes. The formatter 'upstreamlog' can be found in
        # the config root at /etc/nginx/nginx.conf
        #access_log /var/log/nginx/proxy.log upstreamlog;

        # Need to have a DNS server to resolve the FQDNs provided to proxy_pass
        resolver 8.8.8.8;

        # Parse the incoming FQDN and determine the upstream server
        if ($host ~* (.*)\.(.*)\.colinaws\.com) {
                set $upstream http://$1.amazonaws.com;
        }
        if ($host ~* (.*)\.(.*)\.(.*)\.colinaws\.com) {
                set $upstream http://$1.$2.amazonaws.com;
        }

        # Proxy the request upstream
        location / {
                proxy_pass $upstream;
                proxy_set_header Host $host;
                proxy_pass_header Authorization;
                proxy_pass_header X-Amz-Target;
                proxy_pass_header X-Amz-Date;
                proxy_pass_header User-Agent;
                proxy_pass_header Content-Type;
                proxy_pass_header Content-Length;
                proxy_pass_header Accept-Encoding;
        }
}

这不是问题所在,我自己设法解决了。这是分辨率缓存的问题。将在几分钟内更新此票证AWS API发布到/并且没有可供参考的参数,否则您将非常正确。