V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
keroppi
V2EX  ›  NGINX

nginx 配置遇到的一个怪事,请教各位

  •  
  •   keroppi · Apr 24, 2020 · 4356 views
    This topic created in 2196 days ago, the information mentioned may be changed or developed.

    给 PHP 接口弄了个站点配置,代码已经设置好跨域

    如果是在 PC 端访问 https://domain.com/demo/ 能访问,正常

    但如果前端 用 ajax 请求 https://domain.com/demo/ 报跨域错误:

    Access to XMLHttpRequest at 'https://domain.com/demo/' from origin 'http://127.0.0.1:5500' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
    

    但奇怪的是代码不变,把接口地址改成具体的 php 文件地址如: https://domain.com/demo/index.php ,就正常了

    站点配置:

    server {
    
        listen 80;
        server_name _;
        root /var/www;
        index index.php index.html;
    
        client_max_body_size 50m;
    
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Content-Type-Options "nosniff";
    
        charset utf-8;
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
    
    
    
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass php:9000;
            try_files $uri =404;
        }
    
    }
    
    30 replies    2020-05-21 14:53:06 +08:00
    linauror
        1
    linauror  
       Apr 24, 2020
    看配置来说,发起和请求的域名不一致应该都会出现跨域的提示,请求具体页面应该也会报错
    keroppi
        2
    keroppi  
    OP
       Apr 24, 2020
    @linauror 那为什么填写完整的地址就不报错了呢?
    linauror
        3
    linauror  
       Apr 24, 2020
    贴下 network 看看请求
    xxyang
        4
    xxyang  
       Apr 24, 2020
    贴下 network 里面的 header
    ligolas
        6
    ligolas  
       Apr 24, 2020
    keroppi
        7
    keroppi  
    OP
       Apr 24, 2020
    ligolas
        8
    ligolas  
       Apr 24, 2020
    index.php
    这个页面的 header 里面有 cors header
    / 这里会路由到比如你的默认页面,这个里面没有 cors header 吧?
    keroppi
        9
    keroppi  
    OP
       Apr 24, 2020
    @ligolas
    / 是直接到 index.php ,而 index.php 有跨域设置的

    例如用 Postman 测试的时候直接 post https://domain.com/demo/ 没有问题,就前端不行。
    linauror
        10
    linauror  
       Apr 24, 2020
    @keroppi 成功的部分,request headers 部分也截图出来下
    wangyanrui
        11
    wangyanrui  
       Apr 24, 2020
    是不是代码里面设置了跨域,Nginx 也配了跨域?
    之前 Java 就是遇见过两个地方都配了 cors,然后就出现了这种情况
    icanbeyrhero
        12
    icanbeyrhero  
       Apr 24, 2020
    看看配置文件是不是没有改对文件,我记得外面有一个配置文件
    jugelizi
        13
    jugelizi  
       Apr 24, 2020 via iPhone
    你确认下前端是直接请求你接口还是自己 mok 了代理
    580a388da131
        14
    580a388da131  
       Apr 24, 2020   ❤️ 1
    你的配置文件里没有设置跨域啊

    add_header Access-Control-Allow-Origin *;
    lower
        15
    lower  
       Apr 24, 2020
    你那不是 https 吗?? https 怎么配置的
    huijiewei
        16
    huijiewei  
       Apr 24, 2020
    NGINX 的 / 到 PHP 还有一次 30x 跳转,而你的 Nginx 并没有配置跨域可用,所以就出错了

    解决办法就是 楼上的,Nginx 也配置一下
    keroppi
        17
    keroppi  
    OP
       Apr 24, 2020
    @580a388da131 PHP 代码设置了,其实还是注意看 目录形式请求不可以,完整地址可以这点
    keroppi
        18
    keroppi  
    OP
       Apr 24, 2020
    huijiewei
        19
    huijiewei  
       Apr 24, 2020
    另外 Postman 是 HTTP 请求测试工具,并没有浏览器跨域限制
    keroppi
        20
    keroppi  
    OP
       Apr 24, 2020
    @580a388da131
    @huijiewei

    在 nginx.conf
    添加了
    ```
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Headers X-Requested-With;
    add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
    ```

    问题依然是一样,直接目录不行,完整的文件才可以
    huijiewei
        21
    huijiewei  
       Apr 24, 2020
    @keroppi 试试这个
    add_header Access-Control-Allow-Origin * always;
    add_header Access-Control-Allow-Methods * always;
    keroppi
        22
    keroppi  
    OP
       Apr 24, 2020
    @huijiewei

    依然不行。。
    keroppi
        23
    keroppi  
    OP
       Apr 24, 2020
    @lower http 也是一样的问题
    vindurriel
        24
    vindurriel  
       Apr 24, 2020 via iPhone
    fastcgi_param HTTP_HOST your.domain.com;
    canwushuang
        25
    canwushuang  
       Apr 24, 2020 via Android
    san.html 里面的问题?
    canwushuang
        26
    canwushuang  
       Apr 25, 2020 via Android
    触发了同源策略?
    ligolas
        27
    ligolas  
       Apr 26, 2020
    回头看最初的报错,是 prefight 请求的时候没有通过 cors 检查,所以出问题的是 options 请求,不是 post 请求,因为不了解你的网络实际情况,不确定中间还有没有别的 nginx 之类的,所以你先用 postman,发一个 options 请求过去,看看返回的 header 里面的服务器信息,来确定到底是什么服务器回应了这个 options 请求
    ligolas
        28
    ligolas  
       Apr 27, 2020
    keroppi
        29
    keroppi  
    OP
       Apr 29, 2020
    @ligolas 感谢,我会先测试下,不过没有中间的 nginx 了
    ligolas
        30
    ligolas  
       May 21, 2020
    @keroppi 估计过了时间,这个问题已经被你放下了,昨天正好帮一个小伙儿看 cors 的问题,然后想起你这个帖子了。
    梳理了一下,然后再结合你发的图,你的 PHP 的跨域配置中,设置了 Access-Control-Allow-Headers Content-Type, 但是 nginx 上并没有,而你的 Content-Type 正好是简单请求之外的。所以你把 nginx 的那项配置改为 add_header Access-Control-Allow-Headers X-Requested-With, Content-Type; 大概率应该可以了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   983 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 65ms · UTC 21:54 · PVG 05:54 · LAX 14:54 · JFK 17:54
    ♥ Do have faith in what you're doing.