node.js - 使用 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';
}
}
解决方案
您收到 SSL 错误,myip:60702
但您的代码proxy_pass http://localhost:60702
显示该端口未运行 SSL 服务。它是纯 HTTP。
您需要向实际使用 HTTPS 的 URL 发出 HTTPS 请求。
推荐阅读
- xaml - 如果用户单击屏幕上的其他位置,如何隐藏堆栈布局
- regex - 查找和替换,两个字符之间
- python - 为什么使用 python 而不是 Flask-CLI 运行烧瓶应用程序会产生 ImportError?
- python - 是否有 R 或 Python 函数用于分隔非分隔字符串中的信息,其中信息会有所不同?
- typescript - 无法将严格为 false 的自由形式对象实体模型分配
- python - How to connect to Spark master in Swarm mode from host
- sql-server - 使用 SaveFileDialog vb.net 备份 SQL Server 数据库
- r - 使用向量更改多个列名
- python - 导入tensorflow时出现很多警告
- android - 导航组件 - 共享元素转换根本不起作用