2017-08-31 22 views
6

मेरी शीर्षक के रूप में, यहाँ config conf.d/api-server.confNginx प्रॉक्सी सर्वर में CORS को कैसे सक्षम करें?

server { 
    listen 80; 
    server_name api.localhost; 

    location/{ 
    add_header 'Access-Control-Allow-Origin' 'http://api.localhost'; 
    add_header 'Access-Control-Allow_Credentials' 'true'; 
    add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; 
    add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH'; 

    if ($request_method = 'OPTIONS') { 
     add_header 'Access-Control-Max-Age' 1728000; 
     add_header 'Content-Type' 'text/plain charset=UTF-8'; 
     add_header 'Content-Length' 0; 
     return 204; 
    } 

    proxy_redirect off; 
    proxy_set_header host $host; 
    proxy_set_header X-real-ip $remote_addr; 
    proxy_set_header X-forward-for $proxy_add_x_forwarded_for; 
    proxy_pass http://127.0.0.1:3000; 
    } 
} 

nginx.conf फ़ाइल डिफ़ॉल्ट रूप में एक ही रहने में स्थित फ़ाइल है।

XMLHttpRequest cannot load http://api.localhost/admin/login. Response 
to preflight request doesn't pass access control check: No 'Access- 
Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://admin.localhost:3000' is therefore not allowed access. 
The response had HTTP status code 405. 

That is the response from server That's the request looks like

+1

यदि आप किसी भी मुद्दे का सामना कर रहे हाँ तो मुद्दा क्या है अगर उल्लेख नहीं किया है करने के लिए कोर हेडर चलती के दूसरे दृष्टिकोण का उपयोग कर सकते हैं? जब मैं api.localhost लिए अनुरोध भेजने के लिए कोशिश –

+0

@Tarun लालवानी मैं अभी भी 405 त्रुटि प्राप्त करते हैं, मैं नहीं पता है मैं एक दूसरे की कोशिश की क्यों – qwang

उत्तर

4

मुद्दा यह है कि अगर हालत है अपने है:

बाद मैं (api.localhost/व्यवस्थापक/प्रवेश) api.localhost को अनुरोध भेजने, मैं अभी भी 405 त्रुटि प्राप्त / में माता-पिता में हेडर भेजने के लिए नहीं जा रहे हैं। यदि आप प्रीफलाइट प्रतिक्रिया शीर्षलेखों की जांच करते हैं तो यह

HTTP/1.1 204 No Content 
Server: nginx/1.13.3 
Date: Fri, 01 Sep 2017 05:24:04 GMT 
Connection: keep-alive 
Access-Control-Max-Age: 1728000 
Content-Type: text/plain charset=UTF-8 
Content-Length: 0 

और यह कुछ भी नहीं देता है। तो आपके लिए दो संभव फिक्स। कॉपी add_header अंदर अगर ब्लॉक भी

server { 
    listen 80; 
    server_name api.localhost; 

    location/{ 
    add_header 'Access-Control-Allow-Origin' 'http://api.localhost'; 
    add_header 'Access-Control-Allow_Credentials' 'true'; 
    add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; 
    add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH'; 

    if ($request_method = 'OPTIONS') { 
     add_header 'Access-Control-Allow-Origin' 'http://api.localhost'; 
     add_header 'Access-Control-Allow_Credentials' 'true'; 
     add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; 
     add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH'; 
     add_header 'Access-Control-Max-Age' 1728000; 
     add_header 'Content-Type' 'text/plain charset=UTF-8'; 
     add_header 'Content-Length' 0; 
     return 204; 
    } 

    proxy_redirect off; 
    proxy_set_header host $host; 
    proxy_set_header X-real-ip $remote_addr; 
    proxy_set_header X-forward-for $proxy_add_x_forwarded_for; 
    proxy_pass http://127.0.0.1:3000; 
    } 
} 

या आप स्थान ब्लॉक के बाहर स्थानांतरित कर सकते हैं, इसलिए हर अनुरोध प्रतिक्रिया

server { 
    listen 80; 
    server_name api.localhost; 

add_header 'Access-Control-Allow-Origin' 'http://api.localhost'; 
add_header 'Access-Control-Allow_Credentials' 'true'; 
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; 
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH'; 

    location/{ 

    if ($request_method = 'OPTIONS') { 
     add_header 'Access-Control-Max-Age' 1728000; 
     add_header 'Content-Type' 'text/plain charset=UTF-8'; 
     add_header 'Content-Length' 0; 
     return 204; 
    } 

    proxy_redirect off; 
    proxy_set_header host $host; 
    proxy_set_header X-real-ip $remote_addr; 
    proxy_set_header X-forward-for $proxy_add_x_forwarded_for; 
    proxy_pass http://127.0.0.1:3000; 
    } 
} 

आप केवल अनुमति देना चाहते हैं है CORS के लिए अपने config में कुछ स्थानों । जैसे /api तो आप अपने हेडर

add_header 'Access-Control-Allow-Origin' 'http://api.localhost'; 
add_header 'Access-Control-Allow_Credentials' 'true'; 
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; 
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH'; 

के साथ एक टेम्पलेट conf बनाना चाहिए और फिर अपने OPTIONS ब्लॉक और /api ब्लॉक में

include conf.d/corsheaders.conf; 

का उपयोग करें। इसलिए सीओआरएस को केवल /api के लिए अनुमति है। आप CORS के लिए जो स्थान परवाह नहीं है तो आप सर्वर ब्लॉक

+0

, लेकिन अभी भी – qwang

+0

इस बार काम नहीं करता प्रतिक्रिया 502 खराब गेटवे बन – qwang

+0

है 502 प्रतिक्रिया का मतलब है कि सीओआरएस समस्या हल हो गई है और नई त्रुटि एपीआई सर्वर के बारे में है? – qwang

संबंधित मुद्दे