在以下配置中,当上游服务器返回 HTTP 200 OK 以外的响应时,对 hostnameb.example.org 的请求缺少 X-custom-server-info 标头。
hostnamea.example.org 响应是正确的(包括标题)。
知道为什么或如何解决它吗?
worker_processes auto;
worker_rlimit_nofile 8192;
pid /opt/bitnami/nginx/tmp/nginx.pid;
events {
worker_connections 4096;
}
http {
include /opt/bitnami/nginx/conf/mime.types;
add_header X-custom-server-info "someinfo" always;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Forwarded "";
proxy_http_version 1.1;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 180;
proxy_send_timeout 180;
proxy_read_timeout 180;
proxy_buffers 32 4k;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
#proxy_set_header X-Forwarded-Proto $scheme;
#proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Port 443;
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_names_hash_bucket_size 128; # this seems to be required for some vhosts
default_type application/octet-stream;
resolver dns-default.openshift-dns.svc.cluster.local valid=300s;
server {
listen 8081;
server_name hostnamea.example.org;
set $bff "http://bff.project.svc.cluster.local";
location / {
proxy_pass $bff;
}
}
server {
listen 8081;
server_name hostnameb.example.org;
set $bff "http://bff.project.svc.cluster.local";
location / {
proxy_pass $bff;
add_header Access-Control-Allow-Origin https://external.example.org always;
add_header Access-Control-Allow-Credentials true always;
}
}
log_format upstreamlog '[$time_local] "$request" UA:"$http_user_agent"';
access_log /opt/bitnami/nginx/logs/access.log upstreamlog;
error_log /opt/bitnami/nginx/logs/error.log;
}
答案1
当存在另一个指令时,该add_header
指令不会从http
块继承到server
和块。location
add_header
你可以这样修复它
location / {
proxy_pass $bff;
add_header X-custom-server-info "someinfo" always;
}
location / {
proxy_pass $bff;
add_header X-custom-server-info "someinfo" always;
add_header Access-Control-Allow-Origin https://external.example.org always;
add_header Access-Control-Allow-Credentials true always;
}
重新启动nginx
sudo systemctl restart nginx
,现在它应该可以工作了