【问题标题】:cors error with nginx, node and vue when using SSL使用 SSL 时 nginx、node 和 vue 出现 cors 错误
【发布时间】:2021-05-28 16:06:59
【问题描述】:

我正在使用自签名证书和 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}.`));

node 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';
     }
  }

【问题讨论】:

  • “我突然收到一个 cors 错误”——这到底是什么意思?
  • "ssl 协议错误" — 到底是什么意思?
  • @Quentin 错误是那个模糊的myip:60702/user/login:1 Failed to load resource: net::ERR_SSL_PROTOCOL_ERROR or Cross-source (cross-origin) request blocked: The same-source rule prohibits reading the external resource at https://myip:60702/user/login. (Reason: CORS request failed).
  • 直接请求网址。在地址栏中输入它。它可能会为您提供有关 SSL 错误的更多信息。
  • @Quentin Postman 给我:Error: write EPROTO 1284230808:error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER:../../third_party/boringssl/src/ssl/tls_record.cc:242: 或更清晰的 curl:curl: (35) error:1408F10B:SSL routines:ssl3_get_record:wrong version number

标签: node.js nginx


【解决方案1】:

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

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

【讨论】:

  • 如果我在 axios 中将后端调用改回 http,我会得到:Mixed Content: The page at 'https://inf-education-67.umwelt-campus.de/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint
  • 这就是为什么我说“向实际使用 HTTPS 的 URL 发出 HTTPS 请求”而不是“完全绕过你的 nginx 服务器并直接从节点请求”
  • 所以我需要更改axios中的baseURL?从 nginx 请求时我需要使用什么 URL?
  • 你的 nginx 服务器的 URL 是什么?大概您已经在使用它来加载原始 HTML 文档了。
  • 在 server_name 中使用标准 URL,如 https://inf-education-67... 给我一个 404。抱歉这些愚蠢的问题,但我对这个 nginx 东西完全陌生。
猜你喜欢
  • 2017-11-30
  • 2021-03-07
  • 2019-02-02
  • 2023-01-12
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 2020-10-04
  • 2021-03-26
相关资源
最近更新 更多