0
我想爲Google Places API啓用CORS,以便通過帶有WkWebView的Ionic 2應用程序調用它。NGINX爲Google Places API調用啓用CORS
我在我的nginx的默認配置這樣做:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name _;
location/{
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Nginx doesn't support nested If statements, so we
# concatenate compound conditions on the $cors variable
# and process later
# If request comes from allowed subdomain
# (*.googleapis.com) then we enable CORS
if ($http_origin ~* (https?://.*\.googleapis\.com(:[0-9]+)?$)) {
set $cors "1";
}
# OPTIONS indicates a CORS pre-flight request
if ($request_method = 'OPTIONS') {
set $cors "${cors}o";
}
# Append CORS headers to any request from
# allowed CORS domain, except OPTIONS
if ($cors = "1") {
more_set_headers 'Access-Control-Allow-Origin: $http_origin' always;
more_set_headers 'Access-Control-Allow-Credentials: true' always;
proxy_pass http://111.111.111.111:80;
}
# OPTIONS (pre-flight) request from allowed
# CORS domain. return response directly
if ($cors = "1o") {
more_set_headers 'Access-Control-Allow-Origin: $http_origin';
more_set_headers 'Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE';
more_set_headers 'Access-Control-Allow-Credentials: true';
more_set_headers 'Access-Control-Allow-Headers: Origin,Content-Type,Accept';
add_header Content-Length 0;
add_header Content-Type text/plain;
return 204;
}
# Requests from non-allowed CORS domains
proxy_pass http://111.111.111.111:80;
}
}
但我geeting一個502錯誤網關,每次我打電話:
http://111.111.111.111/maps/api/place/textsearch/json?key=APIKEY&query=starbucks
任何幫助,請?
你應該在你的應用程序處理不是全局CORS。例如,一個資源只能通過GET獲得,另一個通過POST和PUT獲得。 –
在這種情況下,我的應用程序不能因爲WkWebView的限制。我需要在服務器端做到這一點。 :( – Eusthace