ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

nginx设置跨域访问

2026/9/24 10:11:13 拓冰建站 浏览量
nginx设置跨域访问

目录

一:前端请求

二:后端设置


网站架构前端使用jquery请求,后端使用nginx+php-fpm

一:前端请求

<script>

$.getJSON('http://nngzh.youjoy.com/cc.php', {

                openid: 'sd',

        }, function(res) {

                 alert(res);

                if(res.code == 0) {

                       alert(res.msg);

                } else {

                        alert(res.msg);

                }

        });

 </script>

二:后端设置

后台:nginx+php-fpm

nginx配置文件

 listen 80;

                #listen [::]:80;

                server_name sdk.youjoy.tv;

                index index.html index.htm index.php default.html default.htm default.php;

                root  /data/wwwroot/sdk.youjoy.tv;

                include none.conf;

                #error_page   404   /404.html;

             

这三个内容表示跨域:

                add_header Access-Control-Allow-Origin 'http://127.0.0.1:5353';            *号表示所有都可以跨区,设置地址表示客户端的地址(这个就是客户端的本地地址,不是外网地址)

                add_header Access-Control-Allow-Headers X-Requested-With;

                add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

设置完重启Nginx