无法使用nginx在Express服务器上配置SSL
由于“过去的问题没有得到很好的接受而且我可能会被阻止”的风险是这里的问题:
- 我在GoDaddy上有域名(abc.co,我已经购买了SSL证书)
- 我有EC2实例(abc.co域指向此VM的弹性IP)
- 在上面的EC2实例中,我的角度应用程序(UI)正在运行。
- 我可以通过
- 在同一个VM上,我的节点js + express app正在运行。 (我可以调用像http://ip:3000/api/blah这样的API)
- 我已经使用nginx作为API的反向代理和角度应用程序的服务器。
- 从角度应用程序,我们调用像https://ip:3000/api/blah(otherwise这样的API,如果我们只使用http,我会收到混合内容错误)
现在,当我们从角度应用程序调用上面的API时,我们得到以下错误:
在mozilla上:跨源请求被阻止:同源策略不允许
在https://ip:3000/api/users/login/阅读远程资源
在chrome:ERR_CONNECTION_CLOSED
这是我在/ etc / nginx / sites-available / default的nginx文件
server{
#listen 80 default_server;
listen 443 ssl;
server_name abc.co;
#ssl off;
ssl_certificate /home/ubuntu/ssl_certs/abc.co.chained.crt;
ssl_certificate_key /home/ubuntu/ssl_certs/abc.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
root /var/www/html/tests;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
#reverse proxy
location /api {
rewrite ^/api/?(.*) /$1 break;
proxy_pass http://ip:3000;
}
}
server {
listen 80;
server_name abc.co;
return 301 $request_uri;
}
回答如下:
要避免跨源问题,只需使用以下网址访问API:
https://abc.co/api/blah
当您在不同的VM上运行API时,您需要了解有关Cross-origin resource sharing的更多信息
还有一个快递模块可以帮助你。
Cors middleware