目前很多网站都是用前后端完全分离的模式实现,即:后端通过API提供数据,前端使用API获取数据并渲染。不过这样做会存在API跨域的问题,这里介绍一种通过Nginx配置解决跨域问题的方法。
Nginx整体配置如下:
upstream service {
server 127.0.0.1:8080;
}
map $http_origin $cors_header {
default "";
"~^https?://localhost(:[0-9]+)?$" "$http_origin";
}
server {
listen 80;
server_name 127.0.0.1;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location = /favicon.ico { deny all; error_log off; access_log off; log_not_found off; }
location /api/ {
if ($request_method = 'OPTIONS') {
add_header 'Content-Length' 0 always;
add_header 'Content-Type' 'text/plain charset=UTF-8' always;
add_header 'Access-Control-Allow-Origin' '$cors_header' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Origin,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Accept,Cookie,Set-Cookie, X-AUTH-USER, X-AUTH-TOKEN' always;
return 200;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '$cors_header' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Headers' 'Origin,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Accept,Cookie,Set-Cookie, X-AUTH-USER, X-AUTH-TOKEN' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '$cors_header' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Headers' 'Origin,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Accept,Cookie,Set-Cookie, X-AUTH-USER, X-AUTH-TOKEN' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
}
uwsgi_pass service;
include uwsgi_params;
}
}
其中,"map $http_origin $cors_header"将需要跨域的域名或者IP解析出来,方便后面的配置处理。
API路径配置中的 "X-AUTH-USER, X-AUTH-TOKEN",是API中传递的自定义HEADER,需要在 "Access-Control-Allow-Headers"中指明。
Over!
Nginx整体配置如下:
upstream service {
server 127.0.0.1:8080;
}
map $http_origin $cors_header {
default "";
"~^https?://localhost(:[0-9]+)?$" "$http_origin";
}
server {
listen 80;
server_name 127.0.0.1;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location = /favicon.ico { deny all; error_log off; access_log off; log_not_found off; }
location /api/ {
if ($request_method = 'OPTIONS') {
add_header 'Content-Length' 0 always;
add_header 'Content-Type' 'text/plain charset=UTF-8' always;
add_header 'Access-Control-Allow-Origin' '$cors_header' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Origin,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Accept,Cookie,Set-Cookie, X-AUTH-USER, X-AUTH-TOKEN' always;
return 200;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '$cors_header' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Headers' 'Origin,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Accept,Cookie,Set-Cookie, X-AUTH-USER, X-AUTH-TOKEN' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '$cors_header' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Headers' 'Origin,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Accept,Cookie,Set-Cookie, X-AUTH-USER, X-AUTH-TOKEN' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
}
uwsgi_pass service;
include uwsgi_params;
}
}
其中,"map $http_origin $cors_header"将需要跨域的域名或者IP解析出来,方便后面的配置处理。
API路径配置中的 "X-AUTH-USER, X-AUTH-TOKEN",是API中传递的自定义HEADER,需要在 "Access-Control-Allow-Headers"中指明。
Over!