我正在尝试在 Docker 中设置一组服务:Unifi、PHP、Nginx 和 Certbot,其中 Unifi 和 PHP 是后端服务,Nginx 以反向代理模式为它们提供服务,而 Certbot 定期运行以获取 Nginx 的 SSL 证书。
我基本上可以正常工作;所有 GET 请求都可以正常工作,我可以查看 Unifi 提供的页面。但是,由于 CORS,通过 AJAX 发出的所有 POST 请求都会引发 403 错误。
现在,我不太熟悉如何操作 CORS 标头,也不知道是什么导致了错误。是浏览器、Nginx 还是 unifi?不过,我只能更改 Nginx 的配置。
这是我在所有 AJAX 发布请求中从浏览器检查器/网络监视器收到的错误:
POST
scheme https
host example.com:8443
filename /api/stat/device
Address (server_ip_address):8443
Status 403 Forbidden
Version HTTP/2
Transferred 141 B (0 B size)
Referrer Policy strict-origin-when-cross-origin
RESPONSE HEADERS
content-length 0
content-type text/plain
date Fri, 17 Sep 2021 00:59:09 GMT
server nginx
X-Firefox-Spdy h2
REQUEST HEADERS
Accept application/json, text/plain, */*
Accept-Encoding gzip, deflate, br
Accept-Language en-US,en;q=0.5
Connection keep-alive
Content-Length 0
Cookie unifises=(random token here); csrf_token=(random token here)
DNT 1
Host example.com:8443
Origin https://example.com:8443
Referer https://example.com:8443/setup/configure/controller-name
Sec-Fetch-Dest empty
Sec-Fetch-Mode cors
Sec-Fetch-Site same-origin
TE trailers
User-Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0
X-Csrf-Token (random token here)
这是 Nginx 配置:
# enbables GZIP compression
gzip on;
# compression level (1-9)
# 6 is a good compromise between CPU usage and file size
gzip_comp_level 6;
# minimum file size limit in bytes to avoid negative compression
gzip_min_length 256;
# compress data for clients connecting via proxies
gzip_proxied any;
# directs proxies to cache both the regular and GZIp versions of an asset
gzip_vary on;
# disables GZIP compression for ancient browsers
gzip_disable "msie6";
server {
listen 80;
listen [::]:80;
server_name example.com;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/certbot/;
}
# Redirect relevant Unifi paths Unifi Address and Port
location / {
rewrite ^ https://$host:8443$request_uri?;
}
}
server {
listen 8443 ssl http2;
listen [::]:8443 ssl http2;
server_name example.com;
server_tokens off;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_buffer_size 8k;
ssl_dhparam /etc/ssl/certs/dhparam-2048.pem;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
ssl_ecdh_curve secp384r1;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 1.1.1.1 1.0.0.1 208.67.222.222 208.67.220.220;
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
proxy_pass https://unifi:8443/;
proxy_set_header Authorization "";
proxy_pass_request_headers on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Ssl on;
proxy_http_version 1.1;
proxy_buffering off;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
auth_basic "Restricted";
proxy_set_header Referer "";
}
}
我已经厌倦了在 Stack Exchange 上和 Stack Exchange 外寻找比我能跟踪的更多的指南,这就是为什么我的配置现在如此混乱。
那么,如何修改 Nginx 来处理 XHR 请求而不会因 CORS 而失败?
编辑 1:我将端口 443 添加到 Nginx 监听端口 8443 旁边。如果我通过 443 访问 Unifi 并将其代理到 unifi:8443,它会按预期工作。但是,我需要它在 8443 上透明地工作。
修改 2:我尝试添加另一个“中间人”Nginx 容器,配置略有修改。我将对原始 Nginx 容器上端口 8443 的请求代理到端口 443 上的第二个容器,并将其反向代理到 8443 上的 Unifi。结果与之前没有“中间人”代理相同。因此 Web -> 8443 上的 Nginx --> 443 上的 Nginx -> 8443 上的 Unfi。删除了此配置,因为它不起作用,而且效率低下。