51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

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

Umami的Nginx反向代理后Halo博客访问时的跨域问题 {#umami%E7%9A%84nginx%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E5%90%8Ehalo%E5%8D%9A%E5%AE%A2%E8%AE%BF%E9%97%AE%E6%97%B6%E7%9A%84%E8%B7%A8%E5%9F%9F%E9%97%AE%E9%A2%98}

1、nginx的配置文件增加以下代码 {#1%E3%80%81nginx%E7%9A%84%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%A2%9E%E5%8A%A0%E4%BB%A5%E4%B8%8B%E4%BB%A3%E7%A0%81}

    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%E3%80%81%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E9%85%8D%E7%BD%AE}

2.1、配置基础反向代理 {#2.1%E3%80%81%E9%85%8D%E7%BD%AE%E5%9F%BA%E7%A1%80%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86}

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后台配置的分享路径无法打开时的配置方式 {#2.2%E3%80%81halo%E5%90%8E%E5%8F%B0%E9%85%8D%E7%BD%AE%E7%9A%84%E5%88%86%E4%BA%AB%E8%B7%AF%E5%BE%84%E6%97%A0%E6%B3%95%E6%89%93%E5%BC%80%E6%97%B6%E7%9A%84%E9%85%8D%E7%BD%AE%E6%96%B9%E5%BC%8F}

添加/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"; 

赞(0)
未经允许不得转载:工具盒子 » Umami的Nginx反向代理后Halo博客访问时的跨域问题