我有一个在localhost:8888使用tornado的应用程序,这是我设置的头文件:
def set_default_headers(self):
self.add_header("Access-Control-Allow-Origin", "*")
self.add_header("Access-Control-Expose-Headers","Accept-Ranges")
self.add_header("Access-Control-Expose-Headers","Content-Encoding")
self.add_header("Access-Control-Expose-Headers"," Content-Length")
self.add_header("Access-Control-Expose-Headers","Content-Range")
localhot:8888上的应用程序需要从locahost:80获取静态文件,localhost:80上的nginx服务器如下所示:
server {
listen 80;
server_name localhost;
root /var/www/statics;
passenger_enabled on;
passenger_use_global_queue on;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Origin [http://localhost:8888;]
add_header Access-Control-Expose-Headers Accept-Ranges;
add_header Access-Control-Expose-Headers Content-Encoding;
add_header Access-Control-Expose-Headers Content-Length;
add_header Access-Control-Expose-Headers Content-Range;
add_header accept_ranges bytes;
client_max_body_size 512M;
}
但是浏览器中的错误是持久的:
Refused to get unsafe header "Accept-Ranges"
在看到这个issue之后,我尝试添加上面所有这些头文件,其中相关的op给出了他的解决方案,使静态pdf服务器返回这些头文件
Access-Control-Allow-Headers: Range
Access-Control-Expose-Headers: Accept-Ranges, Content-Encoding, Content-Length,
如何在nginx和tornado中实现这一点?
发布于 2013-06-17 16:13:51
使用GET方法获取静态文件,只需在nginx中添加Access-Control-Allow-Origin头部即可。我创建了下面的代码来展示如何从跨域访问静态文件。
我已经创建了一个tornado服务器(),它托管一个html文件。我正在尝试使用nginx托管的javascript访问静态文件。
龙卷风server.py
import tornado.web
import tornado.ioloop
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render('index.html')
if __name__ == '__main__':
app = tornado.web.Application([
(r'/', MainHandler)
])
app.listen(12303)
tornado.ioloop.IOLoop.instance().start()
index.html
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<p> Hi </p>
<button id="btn">click</button>
<div id="content"></div>
</body>
<script>
$(document).ready(function(){
$('#btn').click(function(){
$.get('http://localhost:12300/stacktest/abc.html', function(data){
$('#content').append(data);
});
});
});
</script>
</htm>
nginx配置
server{
listen 12300;
server_name localhost;
location /stacktest {
alias D:/stackof_test;
index index.html;
add_header Access-Control-Allow-Origin http://localhost:12303;
}
}
请注意,我在windows上,位置"D:/stackof_test“包含一个名为"abc.html”的文件,其中包含以下内容
<p>Got the file</p>
https://stackoverflow.com/questions/16627042
复制相似问题