千家信息网

怎样在服务器端的nginx.conf中增加配置以及解决前端跨域问题

发表于:2024-10-05 作者:千家信息网编辑
千家信息网最后更新 2024年10月05日,怎样在服务器端的nginx.conf中增加配置以及解决前端跨域问题,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。第一步:http {
千家信息网最后更新 2024年10月05日怎样在服务器端的nginx.conf中增加配置以及解决前端跨域问题

怎样在服务器端的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';

有些道友用的是phpstudy配置,用上面的结果行不通

那么我测试出新的方式:

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中增加配置以及解决前端跨域问题的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

0