我正在尝试使用nginx反向代理来代理AWS API并遇到一个问题,如果我静态定义上游服务器,它运行正常,但如果我尝试动态生成它会抛出502 Bad网关错误。我不确定如何解决这个问题。
/etc/nginx/nginx.conf
for i in c:
print(i)
的/ etc / nginx的/启用的站点 - /默认
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/*;
}
在上面的配置中,它不起作用,但如果我继续并将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端点(比如说http://dynamodb.us-west-2.amazonaws.com),它就可以正常工作。
答案 0 :(得分:1)
此处的问题是proxy_pass
未设置为独立变量且未设置resolver
标志。添加这两个项会强制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;
}
}
答案 1 :(得分:0)
您需要使用
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_pass
中使用变量时,你需要提供完整的uri和args