Nginx解决跨域问题

如果在b工程的页面直接发送ajax请求a时会发生跨域问题,那么解决方案为:将A和B同时代理到Nginx,由Nginx做请求路由,直接在B工程页面中直接访问Nginx即可
          

 server {
                listen       80;
                server_name  www.chx.com;

                #charset koi8-r;

                #access_log  logs/host.access.log  main;

                location /a {
                    #proxy_connect_timeout 1;
                    #proxy_send_timeout 1;
                    #proxy_read_timeout 1;
                    proxy_pass http://www.a.com:8080/a/;
                    index index.html index.htm;
                    
              }
            location /b {
                    #proxy_connect_timeout 1;
                    #proxy_send_timeout 1;
                    #proxy_read_timeout 1;
                    proxy_pass http://www.b.com:8081/b/;
                    index index.html index.htm;
                }
            }

B工程页面请求:

$("#button").click(function () {
                    $.ajax({
                        url:"http://www.chx.com/a/AServlet?username="+$("#username").val(),
                        type:"GET",
                        success:function (result) {
                            alert(result);
                        }
                    })
                });

 

直接在B工程访问Nginx,由Nginx在内部做转发,以解决跨域问题

 

posted @ 2020-02-10 17:32  怀鑫  阅读(196)  评论(0编辑  收藏  举报