server { listen 8080; include enable-php.conf; root /home/wwwroot/image; location / { add_header 'Access-Control-Allow-Origin' 'http://www.tisunion.com'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET'; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ /\. { deny all; } access_log /home/wwwlogs/image.log access; }
要在nginx上啓用跨域請求,需要添加指令:
add_header 'Access-Control-Allow-Origin' 'http://www.tisunion.com';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET';
註釋如下:
第一條指令:授權從www.tisunion.com的請求
第二條指令:當該標誌爲真時,響應於該請求是否可以被暴露
第三條指令:指定請求的方法,可以是GET,POST等
如果需要允許來自任何域的訪問,可以這樣配置:
Access-Control-Allow-Origin *;
重啓nginx
service nginx reload