一些客户面临跨域请求被阻止的问题

时间:2019-01-15 16:09:10

标签: reactjs nginx django-rest-framework

已部署站点here 但是一些客户面临http://idealnemoloko.com:3000/api

上的跨域请求被阻止的情况

这很奇怪,因为我周围的大多数环境对该网站都还可以,而且可以正常工作。

无法解决可能的问题。

我在Django应用中启用了cors:

INSTALLED_APPS = [
    ...,
    'corsheaders',
    ...
]

MIDDLEWARE = [
    ...,
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]

CORS_ORIGIN_WHITELIST = (
    'idealnemoloko.com',
    'www.idealnemoloko.com',
)

CORS_ALLOW_METHODS = (
   'GET',
   'OPTIONS',
   'HEAD'
)

附加react app nginx.conf和django nginx.conf:

Django应用的Nginx设置:

upstream idealnemoloko_server {
    server api:3000;
}

server {

    listen 80;
    server_name idealnemoloko.digioceideal.pp.ua;
    client_max_body_size 256m;

    location / {
        proxy_pass http://idealnemoloko_server;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;       
    }

    location /static/ {
        alias /api/static/;
    }
}

React应用程序的Nginx设置:

server {
        # listen on port 80
        listen 80;

        # where the root here
        root /usr/share/nginx/html;
        # what file to server as index
        index index.html index.htm;

        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to redirecting to index.html
            try_files $uri $uri/ /index.html; 
        }

        # Media: images, icons, video, audio, HTC
        location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
          expires 1M;
          access_log off;
          add_header Cache-Control "public";
        }

        # Javascript and CSS files
        location ~* \.(?:css|js)$ {
            try_files $uri =404;
            expires 1y;
            access_log off;
            add_header Cache-Control "public";
        }

        # Any route containing a file extension (e.g. /devicesfile.js)
        location ~ ^.+\..+$ {
            try_files $uri =404;
        }
}

请大家寻求帮助。我还需要提到我正在使用docker和docker-compose。如果需要这些设置,请在评论中询问。

期待任何帮助!

0 个答案:

没有答案