修改nginx配置

1
2
3
4
5
6
7
8
#查看容器
docker ps -a

#进入容器
docker exec -it 容器ID /bin/sh

#重启容器
docker restart 容器ID

跨域nginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
listen 80;
server_name 127.0.0.1;

#跨域配置
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,accesstoken';

root /www;

location / {
index index.htm index.html index.php;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}

......
}

跨域注意事项

1
2
错误提示
Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:9080, *', but only one is allowed. Origin 'http://localhost:9080' is therefore not allowed access.

Access-Control-Allow-Origin只允许一个值
nginx或php配置其中一种就可以了

php配置跨域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//通用控制器添加:
public function init() {
parent::init();
//允许所有来源访问
header('Access-Control-Allow-Origin:*');
//允许访问的方式
header('Access-Control-Allow-Method:GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS,HEAD');
//允许自定义的头部参数
header("Access-Control-Allow-Headers:Origin,X-Requested-With,Content-Type,Accept,Authorization");
}

//或者直接在入口文件头部添加:
//允许所有来源访问
header('Access-Control-Allow-Origin:*');
//允许访问的方式
header('Access-Control-Allow-Method:GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS,HEAD');
//允许自定义的头部参数
header("Access-Control-Allow-Headers:Origin,X-Requested-With,Content-Type,Accept,Authorization");