带有www前缀

时间:2017-08-28 07:13:34

标签: angularjs nginx cors gunicorn django-cors-headers

我有一个网站,后端nginx,python,django和frontend angularjs。我在后端有CORS策略只允许前端来源,但是我在使用www作为前缀浏览我最后开发的部分时遇到错误。错误是:

  

XMLHttpRequest无法加载http://backend.com/api/endpoint。从' http://backend.com/api/endpoint'重定向到' http://backend.com/api/endpoint'已被CORS政策阻止:“访问控制 - 允许 - 来源”#39;标头的值为' http://frontend.com'这不等于提供的原产地。起源' http://www.frontend.com'因此不允许访问

两个www和不是,前端网址都包含在CORS白名单中。 我认为nginx配置错误,但该网站在每个部分都运行良好,除了最后一个部分。

修改

我使用corsheaders将CORS标头添加到响应中 这个配置很差,我只是将地址列入白名单

CORS_ORIGIN_WHITELIST = (
    'frontend.com',
    'www.frontend.com',
    'media.frontend.com',
    'backend.com',
)

这是Nginx配置。 我们使用PhantomJS将网站的渲染版本返回给机器人



########################################################################
# Frontend AngularJS Application
########################################################################

server {
    listen 80;
    root /home/frontend/current;
    #listen 443 ssl;

    server_name frontend.com;
    #ssl_certificate /etc/nginx/ssl/frontend.com.crt;
    #ssl_certificate_key /etc/nginx/ssl/frontend.com.key;

    access_log /var/log/nginx/www.frontend.com/access.log;
    error_log  /var/log/nginx/www.frontend.com/error.log;

    keepalive_timeout 5;
    charset utf-8;

    location ~ ^/(scripts.*js|styles|images) {
        gzip_static on;
        expires 1y;
        add_header Cache-Control public;
        add_header ETag "";

        break;
    }

    location / {
        include /etc/nginx/mime.types;
        try_files $uri @phantomjs;
    }

    location @phantomjs {
        # We set control variable to 2 to be used in rewrite if a bot visits the page
        set $i 0;
        # If any of the user agent in the RegEX, we set the control variable to 1...
        if ($http_user_agent ~* "Facebot|dapulse|slack|facebook|linkedin|googlebot|yahoo|bingbot|baiduspider|yandex|yeti|yodaobot|gigabot|ia_archiver|facebookexternalhit|twitterbot|developers\.google\.com") {
            set $i 1;

        }
        # ...or if Googlebot in particular (or we appended phantomjs=1 query string for debug) 
        # we set the control variable to 1 as well
        if ($args ~ "_escaped_fragment_|phantomjs=1") {
            set $i 1;
        }
        # Then, finally, if control variable is true (set to 1) we pass the $uri to phantomjs proxy
        # $uri will be something like /some/path/to/something WITHOUT any query string argument
        if ($i = 1) {
            # rewrite .* /$scheme://$host$request_uri? break;
            rewrite .* /$uri? break;
            proxy_pass http://localhost:9999;
        }
        if ($i = 0) {
            rewrite .* /index.html?$query_string break;
        }
    }
}

########################################################################
# Media Assets
########################################################################

server {
    listen 80;
    #listen 443 ssl;

    server_name media.frontend.com;
    #ssl_certificate /etc/nginx/ssl/media.frontend.com.crt;
    #ssl_certificate_key /etc/nginx/ssl/media.frontend.com.key;

    access_log /var/log/nginx/media.frontend.com/access.log;
    error_log  /var/log/nginx/media.frontend.com/error.log;

    root /home/frontend/media;
    keepalive_timeout 5;
    charset utf-8;

    location ~ ^/(scripts.*js|styles|images) {
        gzip_static on;
        expires 1y;
        add_header Cache-Control public;
        add_header ETag "";

        break;
    }
}

########################################################################
# API & Backend Django Application
########################################################################

server {
    listen 80;
    server_name backend.com;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        #proxy_pass http://media.frontend.com:80/;
        #root /home/frontend/media;
        alias /home/frontend/media/;
    }

    location /media/ {
        #proxy_pass http://media.frontend.com:80/;
        #root /home/frontend/media;
        alias /home/frontend/media/media/;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/backend/wsgi.sock;
    }
}




0 个答案:

没有答案