首页 > 解决方案 > 使用 SSL 时 nginx、node 和 vue 出现 cors 错误

问题描述

我正在使用自签名证书和 nginx 让 https 在我的服务器上工作。没有 https 我不会出错。但是当使用 https 时,我突然收到一个 cors 错误(firefox)/ ssl 协议错误(chrome)。我在我的后端和我的 nginx vue 配置中启用了 cors。

在 app.js 中:

app.options('*', cors())
app.use(cors());
// Still using http module cause nginx is doing https stuff
const listener = app.listen(nconf.get('port'), () => console.log(`Ready on port ${listener.address().port}.`));

节点 nginx conf 看起来像:

 listen       443 ssl;
 listen       [::]:443 ssl http2;
 server_name  localhost;

 # point to ssl certificate path
 include snippets/bcknd/self-signed.conf;
 include snippets/bcknd/ssl-params-bck.conf;
 root /var/www/server/pvapp-server;

 location / {
      proxy_pass http://localhost:60702;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
      proxy_ssl_verify off;
 }

vue nginx 配置如下所示:

     listen       443 ssl http2;
     listen       [::]:443 ssl http2;
     server_name  inf-education-67.umwelt-campus.de;

     add_header 'Access-Control-Allow-Origin' '*';
     add_header 'Access-Control-Allow-Credentials' 'true';
     add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
     add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';

     # point to ssl certificate path
     include snippets/self-signed.conf;
     include snippets/ssl-params.conf;

     location / {
         # point to dist folder inside vue source code folder
         root /var/www/client/pvapp-client/dist;
         autoindex on;
         autoindex_exact_size off;
         index index.html index.htm;
         try_files $uri $uri/ /index.html;


     if ($request_method = 'POST') {
       add_header 'Access-Control-Allow-Origin' '*';
       add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
       add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
       add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
     }
  }

标签: node.jsnginx

解决方案


您收到 SSL 错误,myip:60702但您的代码proxy_pass http://localhost:60702显示该端口未运行 SSL 服务。它是纯 HTTP。

您需要向实际使用 HTTPS 的 URL 发出 HTTPS 请求。


推荐阅读