enabe CORS form any site/localhost in nginx -
i have configure nginx node, , run simple express app,
var express = require('express') var app = express() app.get('/', function (req, res) { res.json({ "name":"rajesh" }) }) app.listen(8010, function () { console.log('example app listening on port 8010!') })
using postman can see request on vpsip:8010
json string successfuly.
but when use html+jquery ajax code ::
$.ajax({ method: "get", url: "http://vpsip:8010/" }) .done(function( msg ) { alert( "data saved: " + msg ); });
i error
no 'access-control-allow-origin' header present on requested resource. origin 'http://anything' therefore not allowed.
my nginx default file is
location /exapi { proxy_pass http://localhost:8010/exapi; proxy_http_version 1.1; proxy_set_header upgrade $http_upgrade; proxy_set_header connection 'upgrade'; proxy_set_header host $host; proxy_cache_bypass $http_upgrade; if ($request_method = 'options') { add_header 'access-control-allow-origin' '*'; # # om nom nom cookies # add_header 'access-control-allow-credentials' 'true'; add_header 'access-control-allow-methods' 'get, post, options'; # # custom headers , headers various browsers *should* ok aren't # add_header 'access-control-allow-headers' 'dnt,x-customheader,keep-alive,user-agent,x-requested-with,if-modified-since,cache-control,content-type'; # # tell client pre-flight info valid 20 days # add_header 'access-control-max-age' 1728000; add_header 'content-type' 'text/plain charset=utf-8'; add_header 'content-length' 0; return 204; } if ($request_method = 'post') { add_header 'access-control-allow-origin' '*'; add_header 'access-control-allow-credentials' 'true'; add_header 'access-control-allow-methods' 'get, post, options'; add_header 'access-control-allow-headers' 'dnt,x-customheader,keep-alive,user-agent,x-requested-with,if-modified-since,cache-control,content-type'; } if ($request_method = 'get') { add_header 'access-control-allow-origin' '*'; add_header 'access-control-allow-credentials' 'true'; add_header 'access-control-allow-methods' 'get, post, options'; add_header 'access-control-allow-headers' 'dnt,x-customheader,keep-alive,user-agent,x-requested-with,if-modified-since,cache-control,content-type'; } }
can please tell me how enable cors request on nginx(in vps), can create express apis.
got same problem time ago, api service (http://vps264757.ovh.net/gamefactoryservice/resources/maze?w=50&h=50 / http://vps264757.ovh.net/maze.html). here default sites-enabled works properly, proxying glassfish @ localhost/18000. hope helps :
#glassfish location /gamefactoryservice/ { index index.html; add_header access-control-allow-origin $http_origin; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-nginx-proxy true; proxy_ssl_session_reuse off; proxy_set_header host $http_host; proxy_redirect off; proxy_set_header x-forwarded-server $host; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:18000/gamefactoryservice/; } #static content location / { root /usr/share/nginx_static_content; } error_page 500 501 502 503 504 505 506 507 508 509 510 511 /50x.html; #error location = /50x.html { add_header access-control-allow-origin $http_origin; internal; }
Comments
Post a Comment