简体   繁体   中英

WordPress CORS Error When Trying To Save — Nginx Reverse Proxy

My WordPress URL is: localhost/test-press/whs/

But my front-end (Site Url) is: localhost:9991

I'm getting CORS error whenever I try to save something in WordPrss admin menu.

What's wrong with my configuration:

server {
        listen       9991;
        server_name  localhost;
        
        location ~ ^/wp-content/uploads {
            add_header Access-Control-Allow-Origin *;
            root G:\\XAMPP\\htdocs\\test-press\\whs\\;
        }
        
        location / {
            add_header Access-Control-Allow-Origin *;
            proxy_pass http://localhost:9994/;
            proxy_buffering off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Port $server_port;
            proxy_set_header Host localhost;
            proxy_set_header Cookie $http_cookie;
        }
        
        root G:\\XAMPP\\htdocs\\test-press\\whs\\;
        location ~ ^/wp-json {
            # CORS
            add_header Access-Control-Allow-Origin "*";
            #rewrite ^(.+)$ /index.php?q=$1 last;
            proxy_set_header Host localhost;
            proxy_set_header Cookie $http_cookie;
            index index.php;
            try_files $uri $uri/ /index.php?$args;
        }
        
        location ~ \.php$ {
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods *;
            try_files  $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass   127.0.0.1:9123; #9123
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

you can add this code

add_action('init', 'allow_origin_qrl');
function allow_origin_qrl()
{
//header("Access-Control-Allow-Origin: *"); 
header('Access-Control-Allow-Origin: *');
header('Content-Type: *; charset=utf-8');
header('Access-Control-Allow-Headers: Origin, Content-Type');
   //header('Content-type: *; charset=utf-8');

header('Access-Control-Allow-Methods: GET,POST');

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM