weixin_33736832 2015-04-14 22:20 采纳率: 0%
浏览 93

有没有办法避免nginx中的CORS问题?

有没有办法避免nginx中的CORS问题?我的一个应用程序运行在netty-server上,它附带了使用joc.lan作为域名的Play框架,而其他应用程序是在我的应用程序中集成的php Web服务器上。我的应用程序使用iframe加载,它使用chat.joc.lan作为域名,是joc.lan的一个子域。因此,当我的应用程序中的任何人试图访问其他应用程序的任何数据时,控制台上都会遇到如下错误:

Uncaught SecurityError: Blocked a frame with origin "http://chat.joc.lan" from accessing a frame with origin "http://joc.lan". Protocols, domains, and ports must match.

我通过在两个应用程序上将document.Domain设置为主域名joc.lan来解决此错误。对于Ajax请求,我使用的是JSONP,不过它不支持火狐和IE。以下是我的主要应用程序joc.lan:

server {
    listen       80;
    server_name  joc.lan;

    location / {
        proxy_pass http://localhost:9000;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # WebSocket support (nginx 1.4)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        }
}

以下是我在joc.lan内部使用iframe的代码:

server {
                listen  80;
                server_name chat.joc.lan;
                root /opt/apps/flyhi/chat;
                index   index.php;
                # caching for images and disable access log for images
                location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml|ttf|eot)$ {
                        access_log              off;
                        expires                 360d;
                }


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

location ~ \.php {
    fastcgi_pass  127.0.0.1:9011;
    fastcgi_index index.php;
    include fastcgi_params;

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    access_log off;
}


location / {
    try_files $uri $uri/ /index.php?r=$request_uri;

}
  • 写回答

1条回答 默认 最新

  • weixin_33749242 2015-04-15 10:11
    关注

    I am not sure but you can set the parameters in nginx config file for allowing CORS in all browsers.
    This link can be a help where there is nginx config file is given to allow CORS

    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵