是否可以使用nginx代理将get请求转发到具有端口的同一URL

时间:2016-02-12 02:50:15

标签: ajax nginx url-rewriting proxy

我有一个简单的python bottle api,以及一个承载index.html的nginx服务器,该服务器对端口8001上本地主机上运行的瓶子api发出ajax请求。问题是我没有处理CORS因为它在一个封闭的环境中运行,它只是一个简单的api来管理一些活动目录的东西,它位于防火墙和基本身份验证之后。我并不担心跨站点脚本。我需要知道的是,如果我从

编程我的ajax请求
                $.ajax({ 
                   type: "GET",
                   url: "http://localhost:8001/newuser/" + "firstName=" + fname + "&lastName=" + lname + "&email=" + email + "&password=" + new_password,
                   success: function(data){        
                     alert(data);
                     document.getElementById("alert").innerHTML = data.toString();
                   }
                });

                $.ajax({ 
                   type: "GET",
                   url: "/newuser/" + "firstName=" + fname + "&lastName=" + lname + "&email=" + email + "&password=" + new_password,
                   success: function(data){        
                     alert(data);
                     document.getElementById("alert").innerHTML = data.toString();
                   }
                });

如何编写nginx重写以获取ajax请求并使其在本地主机上转到端口8001,如果可能的话。我看了几个例子,但却找不到我需要的东西。

有人可以帮助我使用nginx代码吗,我需要将请求转发到localhost:8001而不是:80当检测到/ newuser /时。

这是因为当我调用localhost:8001时,它会在Web控制台中出现cors错误。

我试图在nginx中禁用CORS

nginx config

location * {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

}

虚拟nginx配置

    location * {
         if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            #
            # Om nom nom cookies
            #
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            #
            # Custom headers and headers various browsers *should* be OK with but aren't
            #
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            #
            # Tell client that this pre-flight info is valid for 20 days
            #
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
         }
         if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
         }
         if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';


 }
}

1 个答案:

答案 0 :(得分:2)

这是我解决的问题。

.dropdown-menu li:hover{ color:black}