2016-02-12 67 views
2

轉發到使用nginx代理的獲取請求我有一個簡單的python bottle api和一個nginx服務器,託管index.html,使ajax請求運行在本地的api瓶子api主機在端口8001.問題是我不處理CORS,因爲它運行在一個封閉的環境,它只是一個簡單的API來管理一些活動目錄的東西,它位於防火牆和基本身份驗證後面。我並不擔心跨站點腳本。我需要知道的是,如果我編程,從是否有可能使用nginx代理將端口

   $.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我的Ajax請求,如果那是可能的。我看了幾個例子,但無法完全找到我需要的。

有人可以幫我這個nginx的代碼,我需要將請求轉發到位於localhost:/ NEWUSER當檢測到/ 80:8001,而不是。

這是因爲當我打電話給本地主機:8001它給了我在web控制檯cors錯誤。

我試圖在nginx的禁用CORS

nginx的配置

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'; 

} }

回答

2

這裏是我的解決方案,我想通了。

# 
# A virtual host using mix of IP-, name-, and port-based configuration 
# 

upstream admanager.oneplatform.build { 
    server localhost:8001; 
} 

server { 
     listen  80 default_server; 
     server_name _; 
     root /opt/admanager1; 
     index index.html; 

     auth_basic "Restricted"; 
     auth_basic_user_file /etc/nginx/.htpasswd; 

     location /newuser/ { 
       proxy_set_header X-Real-IP $remote_addr; 
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
       proxy_set_header X-NginX-Proxy true; 
       proxy_pass http://localhost:8001/newuser/; 
       proxy_ssl_session_reuse off; 
       proxy_set_header Host $http_host; 
       proxy_redirect off; 
     } 

     location /update/ { 
       proxy_set_header X-Real-IP $remote_addr; 
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
       proxy_set_header X-NginX-Proxy true; 
       proxy_pass http://localhost:8001/update/; 
       proxy_ssl_session_reuse off; 
       proxy_set_header Host $http_host; 
       proxy_redirect off; 
     } 

} 
相關問題