Nginx 反向代理
1. 站点监听3000 端口
server {
listen 80;
server_name a.xxxxx.com;
location / {
proxy_pass http:
}
}
2. 静态文件cdn
server {
listen 80;
server_name cdn.xxxxx.com;
location / {
index index.html;
root D:\sources\dist;
}
}
3. m.xxxxx.com来访问,所有数据接口由 xxxxx.com提供
server {
listen 80;
server_name m.xxxxx.com;
location /api {
# 代理api,以免跨域
proxy_pass https:
}
location / {
index index.html;
root D:/angularjs/dist;
}
}
解决前端跨域问题
1. 设置CORS
server {
listen 80;
server_name cdn.xxxxx.com;
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
index index.html;
root D:/sources/dist;
}
}
可以通过跨域反过来限制某些资源的是否可访问
if ($http_referer ~* 'xxxxx.com') {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
}
2. 做api 中转
server {
listen 80;
server_name m.xxxxx.com;
location /api {
proxy_pass https:
}
}