Skip to content

Instantly share code, notes, and snippets.

@wilsoncook
Created November 16, 2016 09:43
Show Gist options
  • Save wilsoncook/338e16b029d68077514331d5c9095139 to your computer and use it in GitHub Desktop.
Save wilsoncook/338e16b029d68077514331d5c9095139 to your computer and use it in GitHub Desktop.
利用nginx动态转发实现ajax跨域
server {
listen 80;
listen 443;
server_name zx250local.com;
# 测试,转发实现跨域
# 后面就可以这样在zx250local.com域名下发起其他域名的AJAX请求了,比如:
# $.getJSON('/cross/http%3a%2f%2fmobile.minmetals.com.cn%3a8050%2fwkjt%2fversion.html%3f1479282583007', function(result){console.log(result)})
location ~ /cross/(?<target>.+) { # 请求时,<target>中最好全部urlencode一遍
# rewrite /cross/(.+)$ /$1 break;
resolver 8.8.8.8; # 这里因为域名在$target变量内,会导致dns解析失效,这里需要手动再指定一下dns
proxy_pass http://$target; # 注:若这里将http://也在$target中动态设置,则会导致 invalid URL prefix in "http:/mobile.minmetals.com.cn:8050/wkjt/version.html?1479282583007" 错误,暂时还不清楚原因
}
}
@wilsoncook
Copy link
Author

仅测试用

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment