浏览器对于非简单的请求,会分两次应答。第一次 预检请求(method:OPTIONS),主要验证来源是否合法,并返回允许的 Header 等。第二次才是真正的 HTTP 应答。所以服务器必须处理 OPTIONS 应答,OPTIONS 请求要做 204 处理。
以下为需在 nginx 中配置的几个参数:
Access-Control-Allow-Origin
(必含):允许的域名,只能填通配符或者单域名Access-Control-Allow-Methods
(必含):允许跨域请求的 http 方法Access-Control-Allow-Headers
:返回支持的 http 请求头Access-Control-Allow-Credentials
(可选):标志着当前请求是否包含 cookies 信息,布尔值。只有一个可选值:true,如果不包含 cookies,请略去该项,而不是填 false。这一项与XmlHttpRequest2 对象当中的 withCredentials 属性应保持一致,即 withCredentials 为true时该项也为 true;withCredentials为false时,省略该项不写。反之则导致请求失败。Access-Control-Max-Age
(可选):以秒为单位的缓存时间,用于缓存预检请求。配置实例
server {
listen 443 ssl;
server_name api.domain.com;
ssl on;
ssl_certificate /etc/nginx/ssl/api/214108476430356.pem;
ssl_certificate_key /etc/nginx/ssl/api/214108476430356.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /main {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS';
add_header Access-Control-Allow-Credentials 'true';
add_header Access-Control-Allow-Headers 'Accept, Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, DELETE, PUT, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Accept, Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
}
}
server {
listen 80;
server_name service.domain.ai;
rewrite ^(.*)$ https://$server_name$1 permanent;
}
其中
$1
指 (.*) 匹配到的部分,permanent
表示永久性重定向
当一个站点只允许 https 访问时,如果使用 http 访问会报 497 错误码,因此可以利用 497 状态码重定向到 https
利用497状态码重定向到 https
server {
listen 80;
server_name docs.lvrui.io
error_page 497 https://$host$uri?$args;
}
server {
listen 443 ssl;
server_name docs.lvrui.io;
index index.html index.htm;
access_log /var/log/nginx/docs.log main;
ssl on;
ssl_certificate /etc/ssl/docs.20150509.cn.crt;
ssl_certificate_key /etc/ssl/docs.20150509.cn.key;
error_page 404 /404.html;
location / {
root /var/www/html/docs;
}
}
# index.html
<html>
<meta http-equiv="refresh" content="0;url=https://docs.lvrui.io/">
html>
server {
listen 80;
server_name docs.lvrui.io;
location / {
# 将 index.html 文件放到下面的目录下
root /var/www/html/refresh/;
}
}
server {
listen 443 ssl;
server_name docs.lvrui.io;
index index.html index.htm;
access_log /var/log/nginx/docs.log main;
ssl on;
ssl_certificate /etc/ssl/docs.20150509.cn.crt;
ssl_certificate_key /etc/ssl/docs.20150509.cn.key;
error_page 404 /404.html;
location / {
root /var/www/html/docs;
}
}