Umami的Nginx反向代理后Halo博客访问时的跨域问题

1、nginx的配置文件增加以下代码

    add_header 'Access-Control-Allow-Origin' '*' always; 
    add_header 'Access-Control-Allow-Credentials' 'true'; 
    add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, DELETE, PUT, OPTIONS'; 
    add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, X-Custom-Header, Access-Control-Expose-Headers, Token, Authorization'; 
    add_header 'Access-Control-Allow-Headers' '*'; 
    add_header 'Access-Control-Max-Age' 1728000; 

2、反向代理配置

2.1、配置基础反向代理

location ^~ / {
    proxy_pass http://IP:端口; 
    proxy_set_header Host $host; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header REMOTE-HOST $remote_addr; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection "upgrade"; 
    proxy_set_header X-Forwarded-Proto $scheme; 
    proxy_http_version 1.1; 
    add_header X-Cache $upstream_cache_status; 
    add_header Strict-Transport-Security "max-age=31536000";
    add_header Cache-Control no-cache; 
}

无须特殊配置,正常配置即可。

2.2、Halo后台配置的分享路径无法打开时的配置方式

添加/share/反向代理配置

location ^~ /share/ {
    proxy_hide_header "Access-Control-Allow-Origin"; 
    proxy_hide_header "Content-Security-Policy"; 
    proxy_pass http://IP:端口/share/; 
    proxy_set_header Host $host; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header REMOTE-HOST $remote_addr; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection "upgrade"; 
    proxy_set_header X-Forwarded-Proto $scheme; 
    proxy_http_version 1.1; 
    add_header X-Cache $upstream_cache_status; 
    add_header Strict-Transport-Security "max-age=31536000"; 
    add_header Cache-Control no-cache; 
}

注意:必须增加以下代码

    proxy_hide_header "Access-Control-Allow-Origin"; 
    proxy_hide_header "Content-Security-Policy";