设置nginx ssl nodejs + express socket.io angularjs

时间:2016-08-11 07:11:51

标签: node.js ssl nginx socket.io

我在backend上构建了nodejs + express,在前端构建了angularjs并在nginx下运行。 这是我对nginx的设置

  server {
      listen 82;
      server_name example.com www.example.com;
      access_log /var/log/nginx/access.log;
      error_log /var/log/nginx/error.log;

      location / {
        rewrite     ^   https://$server_name$request_uri? permanent;
      }

  }

和ssl的这条路线443

 server {
      listen 443;
      ssl on;
      access_log  /var/log/nginx/access.log;
      error_log /var/log/nginx/error.log;
      ssl_certificate /home/ubuntu/config.com.ssl/ssl.crt;
      ssl_certificate_key /home/ubuntu/config.com.ssl/ssl.key;
      server_name example.com www.example.com;

      location / {
          proxy_pass https://someIP:8085;

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


      location ~ ^/(scripts.*js|styles|images) {
          gzip_static on;
          expires 1y;
          add_header Cache-Control public;
          add_header ETag "";

          break;
      }
      location /socket.io {
          proxy_pass https://someIP:3001;
          #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;
      }

      location /api {
          proxy_pass https://someIP:3001;

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

      location /public {
          proxy_pass https://someIP:3001;

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

并在后端我像这样设置服务器

   var app = require('../app');
   var debug = require('debug')('tlevi:server');
   var fs = require('fs');
   var http = require('http');
   var https = require('https')
   var options = {
      key: fs.readFileSync('./configssl/ssl.key'),
      cert: fs.readFileSync('./configssl/ssl.crt')
  }
  /**
   * Get port from environment and store in Express.
   */

  var port = normalizePort(process.env.PORT || '3001');
  app.set('port', port);

  /**
   * Create HTTP server.
   */

  //var server = http.createServer(app);
  var server = https.createServer(options, app);
  /**var server = https.createServer(app, options, (req, res) => {
    res.writeHead(200);
    res.end('hello world\n ssl');
  }).listen(3000);
  */
  var io = app.io;
  io.attach(server);

  /**
   * Listen on provided port, on all network interfaces.
   */

  server.listen(port);
  server.on('error', onError);
  server.on('listening', onListening);

服务器运行顺畅如魅力,但在客户端有502坏网关(但ssl运行良好)。 somethimes客户端可以运行,但可以从客户端到服务器激活API。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

哇迟到的回复:listen 82;应为listen 80;

默认的http请求端口是80.这可以解释为什么ssl正在工作。