Nginx解決跨域問題
跨域問題
當我們在同一個站點請求時,是不存在什么問題的,但是當我們從一個站點向另外一個站點訪問的時候就會出現跨域問題
解決跨域問題(CORS跨域資源共享)
1.Cross-Origin Resource Sharing(跨域資源共享)
2.允許瀏覽器向跨Origin的服務器發起js請求獲取響應
3.Jsonp、SpringBoot Cors、Nginx
Nginx的解決方案
在nginx.conf中配置文件中的server指令塊下面配置以下內容,之后就可以解決跨域問題了
server {
listen 80;
server_name localhost;
# 允許跨域請求的域,*代表所有
add_header 'Access-Control-Allow-Origin' *;
# 允許帶上cookie請求
add_header 'Access-Control-Allow-Credentials' 'true';
# 允許請求的方法,比如 GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Method' *;
# 允許請求的header
add_header 'Access-Control-Allow-Headers' *;
location / {
root html;
index index.html index.htm;
}
}