NGINX:将我对localhost端口的所有请求重定向到https / SSL

时间:2015-07-12 01:23:15

标签: node.js ssl express nginx https

我在localhost:8080上运行了一个NodeJS服务器。它是HTTPS服务器,而不是HTTP。

因此,访问http://localhost8080时网站无效,仅适用于https://localhost:8080

我想使用NGINX将所有来自localhost:8080/whatever的流量重定向到https://localhost:8080/whatever

我试过了:

server {
    listen 8080;
    server_name 127.0.0.1;
    return 301 https://$server_name$request_uri;
}

无济于事 - 也许Nginx会以某种方式被我的Node / ExpressJS服务器凌驾?

1 个答案:

答案 0 :(得分:-2)

app.configure(function () {
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.configure('production', function () {
    app.use (function (req, res, next) {
      var schema = (req.headers['x-forwarded-proto'] || '').toLowerCase();
      if (schema === 'https') {
        next();
      } else {
        res.redirect('https://' + req.headers.host + req.url);
      }
    });
  });
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
  app.use(express.favicon(__dirname + '/public/favicon.ico'));
});