मुद्दा यह है कि अगर हालत है अपने है:
बाद मैं (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 के लिए जो स्थान परवाह नहीं है तो आप सर्वर ब्लॉक
यदि आप किसी भी मुद्दे का सामना कर रहे हाँ तो मुद्दा क्या है अगर उल्लेख नहीं किया है करने के लिए कोर हेडर चलती के दूसरे दृष्टिकोण का उपयोग कर सकते हैं? जब मैं api.localhost लिए अनुरोध भेजने के लिए कोशिश –
@Tarun लालवानी मैं अभी भी 405 त्रुटि प्राप्त करते हैं, मैं नहीं पता है मैं एक दूसरे की कोशिश की क्यों – qwang