您好,登錄后才能下訂單哦!
怎樣在服務器端的nginx.conf中增加配置以及解決前端跨域問題,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
第一步:
http { ...... add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers X-Requested-With; add_header Access-Control-Allow-Methods GET,POST,OPTIONS; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; ...... }
提示
如果以上都不能解決的話需要把憑證改為true
add_header 'Access-Control-Allow-Credentials' 'true';
那么完整的就是:
add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Headers' "Origin, X-Requested-With, Content-Type, Accept"; add_header 'Access-Control-Allow-Headers' 'X-Requested-With'; add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
那么我測試出新的方式:
server { listen 80; server_name www.crm.com crm.com; root "F:/wwwroot/taobao.com/pc2"; add_header Access-Control-Allow-Origin * always; add_header Access-Control-Allow-Methods * always; add_header Access-Control-Allow-Headers * always; location / { add_header Access-Control-Allow-Origin * always; add_header Access-Control-Allow-Methods * always; add_header Access-Control-Allow-Headers * always; index index.php index.html error/index.html; error_page 400 /error/400.html; error_page 403 /error/403.html; error_page 404 /error/404.html; error_page 500 /error/500.html; error_page 501 /error/501.html; error_page 502 /error/502.html; error_page 503 /error/503.html; error_page 504 /error/504.html; error_page 505 /error/505.html; error_page 506 /error/506.html; error_page 507 /error/507.html; error_page 509 /error/509.html; error_page 510 /error/510.html; include F:/MFCRM/API/pc_v0.2/public/nginx.htaccess; autoindex off; } location ~ \.php(.*)$ { add_header Access-Control-Allow-Origin * always; add_header Access-Control-Allow-Methods * always; add_header Access-Control-Allow-Headers * always; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } }
不知道是環境的原因還是其他的原因,有時候設置一處不起作用。
看完上述內容,你們掌握怎樣在服務器端的nginx.conf中增加配置以及解決前端跨域問題的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。