您的位置:首页 > 运维架构 > Nginx

nginx解决Ajax跨域问题

2013-04-27 16:19 537 查看
今天遇到一个ajax跨域问题,下拉框的数据源要从一个接口获得,但是该接口被部署到另外一台服务器上,在本地可以通过http请求访问,并可以返回json的数据,但是放到页面中不可以获取到下拉框的值,发现chrome控制台中该请求成功,但是没有返回值,于是便遇到了跨域的问题,请教一同事,问题得到解决:

1.搭建nginx服务器

  下载nginx,我用的是nginx1.0.0,下载之后放到一个目录中,修改其中的配置文件conf目录中的ngnix.conf文件

2.找到配置中的server{}标签,在里面的localtion/{}标签中添加一句

  proxy_pass http://localhost:8080/;
  添加后的整体效果为:

location / {
root   html;
index  index.html index.htm;
proxy_pass     http://localhost:8080/; }


  其中第4行为新加入的

3.在该标签下自己新建一个标签,如下:

location /partner{
proxy_pass http://10.23.3.31/partner; }


这个proxy_pass http://10.23.3.31/partner就是你要访问的域
server标签的整体配置为:

server {
listen       80;
server_name  localhost;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location / {
root   html;
index  index.html index.htm;
proxy_pass http://localhost:8080/; }
location /partner{
proxy_pass http://10.23.3.31/partner; }

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1; #}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
}


其中12、14、15、16行为自己手动添加的

修改完毕后,启动nginx.exe程序

注意,此时访问的路径会发生变化,不需要带端口号,如果之前的访问为:localhost:8080//oss-api-server...

现在需要改为:localhost//oss-api-server...即可得到从别的域中取回来的数据!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: