关于前端:nginx跨域配置

6次阅读

共计 514 个字符,预计需要花费 2 分钟才能阅读完成。

应用场景

  • 我的项目采纳前后端拆散模式,前端需独自解决跨域

跨域配置

  1. 关上根目录下的 conf/nginx.conf 文件
  2. 在 server 配置跨域转发解决

  location / {
      root   html;
      index  index.html index.htm;
      proxy_pass yuming.com; #须要拜访的接口域名
  }
  add_header Access-Control-Allow-Origin *; #设置跨域域名
  add_header Access-Control-Allow-Credentials true; #携带 cookie

  if ($request_method = 'OPTIONS') { #拦挡 options 申请
      return 200;
  }

  #应用 `http://localhost/ 接口地址 ` 拜访

nginx 常用命令

  • nginx 启动命令 start nginx(举荐) / nginx.exe(用这个命令 cmd 窗口就不能完结了,只能重开)
  • nginx 进行命令 nginx -s stop(当应用 nginx.exe 命令启动时,应用这个有效)
  • nginx 重启命令 nginx -s reload(重载 nginx 命令,当你扭转了 nginx 配置信息并须要从新载入这些配置时能够应用此命令重载 nginx)
正文完
 0