django vue nginx前后端分离(折腾)

安装

django

pip install django==1.9

nginx

sudo apt-get install nginx

vue

参考官网



需要解决几个问题
============

  1. 跨域请求问题:

No 'Access-Control-Allow-Origin' header is present on the requested resource.
解决办法:
安装 django-cors-headers
django配置:

CORS_ORIGIN_WHITELIST = (
 'google.com',
 'hostname.example.com',
 'localhost:8000',
 '127.0.0.1:8080',
 '127.0.0.1:8000',
 '0.0.0.0:8080'
)
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
)
  • 使用vue-resource 请求登录后无法set-cookie到cookie
    因为前后端分离后不同域无法在cookie中存入cookie,解决办法以下:
  • vue 设置proxyTable
  • nginx做url重写(只实践了这个)
    • 修改nginx.conf
        location /api {
            rewrite /api/?(.*)$  /$1 break;
            proxy_pass http://127.0.0.1:8000/;
        }
      
  • csrf

你可能感兴趣的:(django vue nginx前后端分离(折腾))