[Nginx] Solve the problem of being blocked by CORS policy: No’Access-Control-Allow-Origin’ header is present on the requested resource.

When a cross-domain request interface is required, it will appear

been blocked by CORS policy: No’Access-Control-Allow-Origin’ header is present on the requested resource.

It can be solved in code or directly in nginx

Similar to the code deployed by GOFLY under nginx

Just add the header header

                add_header Access-Control-Allow-Origin * ;
                add_header Access -Control-Allow-Methods ' GET, POST, OPTIONS ' ;

 

server{
       listen 80 ;
        server_name gofly.sopans.com;
        access_log   / var /log/nginx/ gofly.sopans.com.access.log main;
        location / static {
                root / var /www/html/go-fly; // Own deployment path 
        }        
        location / {
                add_header Access -Control-Allow-Origin * ;
                add_header Access -Control-Allow-Methods ' GET, POST, OPTIONS ' ;
                proxy_pass http: // 127.0.0.1:8081; 
                    proxy_http_version 1.1 ;
                    proxy_set_header X -Real- IP $remote_addr;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection " upgrade " ;
                    proxy_set_header Origin "" ;
        }
}

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *