2017-08-31 11 views
6

は、ここに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ファイルは、デフォルトと同じ滞在にある設定ファイルです。

私は(api.localhost /管理/ログイン)api.localhostする要求を送信した後、私はまだ405エラーが表示されます。

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

はいの場合問題が発生した場合は何も言及していませんか? –

+0

@ Tarun Lalwani api.localhostにリクエストを送信しようとするとまだ405エラーが表示されますが、理由はわかりません – qwang

答えて

4

問題があなたの状態であればということです親のヘッダーを/に送信しないでください。プリフライトレスポンスヘッダーを確認すると、

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 

となります。何も表示されません。あなたのために2つの可能な修正。あなただけ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-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; 
    } 
} 

を持っている場合は内部add_headerをコピーします。 /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; 

を使用する必要があります。 CORSは/apiにのみ許可されています。 CORSのどの位置に関わらない場合は、コア・ヘッダーをサーバー・ブロックに移動する2番目の方法を使用できます

+0

2番目の試みを試しましたが、まだ動作しません。 – qwang

+0

このレスポンスは502不良ゲートウェイになります – qwang

+0

502の応答は、CORSの問題が解決されたことを意味し、新しいエラーはAPIサーバーに関するものですか? – qwang

関連する問題