简体   繁体   中英

cors error with nginx, node and vue when using SSL

I'm using self-signed certs and nginx to get https to work on my server. Without https I get no error. But when using https I suddenly get a cors error (firefox) / ssl protocol error (chrome). I enabled cors in my backend and in my nginx vue config.

In 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 looks like:

 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 config looks like:

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

You are getting an SSL error from myip:60702 but your code says proxy_pass http://localhost:60702 so that port isn't running an SSL service. It is plain HTTP.

You need to make the HTTPS request to the URL which actually uses HTTPS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM