nginx解决代理跨域问题

宝塔面板代理的配置文件地址

/www/server/panel/vhost/nginx/proxy/xxx.xxx.com

增加核心代码

add_header Access-Control-Allow-Origin * always;
 add_header Access-Control-Allow-Headers *;
 add_header Access-Control-Allow-Methods "GET, POST, PUT, OPTIONS";
 if ($request_method = 'OPTIONS') {
     return 200;
 }
location ^~ /
{
    add_header Access-Control-Allow-Origin * always;
    add_header Access-Control-Allow-Headers *;
    add_header Access-Control-Allow-Methods "GET, POST, PUT, OPTIONS";
    if ($request_method = 'OPTIONS') {
        return 200;
    }

    proxy_pass http://xx.xx.xx.cn;
    proxy_set_header Host xx.xx.xx.cn;
    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;

    add_header X-Cache $upstream_cache_status;

    #Set Nginx Cache
    
    
    set $static_filesQgoLUN2 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
    	set $static_filesQgoLUN2 1;
    	expires 12h;
        }
    if ( $static_filesQgoLUN2 = 0 )
    {
    add_header Cache-Control no-cache;
    }
}

你可能感兴趣的:(nginx,运维)