nginx允许php跨域,nginx 配置允许跨域无效。。。。

我起了2个服务器,一个放前端代码,一个放后端代码

这样前端访问后端的文件就会跨域,我想通过配置允许跨域

前端使用node服务器,ip http://localhost:8083/,代码如下:

请求后端的一个文件test.json

123

123

let xhr = new XMLHttpRequest();

xhr.open('GET', 'http://localhost:1111/test.json', true);

xhr.onreadystatechange=function () {

if (xhr.readyState === 4 && xhr.status === 200) {

console.log(xhr.responseText)

}

}

xhr.send();

后端用的nginx服务器,ip http://localhost:1111,放了个test.json文件

nginx配置如下:

从网上找了好多跨域配置都不行

#user nobody;

worker_processes 1;

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

#pid logs/nginx.pid;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '

# '$status $body_bytes_sent "$http_referer" '

# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;

#tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65;

#gzip on;

server {

listen 1111;

server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

root html;

index index.html;

# 指定允许跨域的方法,*代表所有

add_header Access-Control-Allow-Methods *;

# 预检命令的缓存,如果不缓存每次会发送两次请求

add_header Access-Control-Max-Age 3600;

# 带cookie请求需要加上这个字段,并设置为true

add_header Access-Control-Allow-Credentials true;

# 表示允许这个域跨域调用(客户端发送请求的域名和端口)

# $http_origin动态获取请求客户端请求的域 不用*的原因是带cookie的请求不支持*号

add_header Access-Control-Allow-Origin $http_origin;

# 表示请求头的字段 动态获取

add_header Access-Control-Allow-Headers

$http_access_control_request_headers;

# OPTIONS预检命令,预检命令通过时才发送请求

# 检查请求的类型是不是预检命令

if ($request_method = OPTIONS){

return 200;

}

}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ \.php$ {

# proxy_pass http://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ \.php$ {

# root html;

# fastcgi_pass 127.0.0.1:9000;

# fastcgi_index index.php;

# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

# include fastcgi_params;

#}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ /\.ht {

# deny all;

#}

}

# another virtual host using mix of IP-, name-, and port-based configuration

#

#server {

# listen 8000;

# listen somename:8080;

# server_name somename alias another.alias;

# location / {

# root html;

# index index.html index.htm;

# }

#}

# HTTPS server

#

#server {

# listen 443 ssl;

# server_name localhost;

# ssl_certificate cert.pem;

# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;

# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;

# ssl_prefer_server_ciphers on;

# location / {

# root html;

# index index.html index.htm;

# }

#}

}

结果依旧报跨域,大神们给看看

bVbAvKD

这是请求和响应,显示200,但还是报跨域

bVbAwHV

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值