资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

怎样在服务器端的nginx.conf中增加配置以及解决前端跨域问题,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

察哈尔右翼中旗网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联公司从2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司

第一步: 

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


当前名称:怎样在服务器端的nginx.conf中增加配置以及解决前端跨域问题
文章地址:http://cdkjz.cn/article/igjpjc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220