Ngnix 常见应用场景配置
Table of Contents
1 反向代理配置
location /api/ { proxy_pass http://192.168.0.240:8000/; # ... }
通过 proxy_pass
来反向代理请求
2 CORS 跨越配置
location /api/ { # add_header Access-Control-Allow-Origin '*'; add_header Access-Control-Allow-Origin 'http://localhost:8000'; add_header Access-Control-Allow-Methods 'GET,POST,PUT,DELETE,OPTIONS'; add_header Access-Control-Allow-Credentials 'true'; 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'; # ... }
主要是添加了三个用于控制 CORS 的头信息:
Access-Control-Allow-Origin
允许的来源Access-Control-Allow-Credentials
设置为 true,允许 ajax 异步请求带 cookie 信息Access-Control-Allow-Headers
设置为 x-requested-with,content-type,允许 ajax 余部请求。