2015-07-12 141 views
0

我有一個運行在localhost:8080上的NodeJS服務器。這是一個HTTPS服務器,而不是HTTP。NGINX:將所有請求重定向到本地主機端口到https/SSL

因此訪問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的是越來越受我的節點/ ExpressJS服務器overrided不知何故?

+0

不能有兩個服務監聽同一個端口,所以其中一個服務器並沒有真正運行。順便說一句,重定向到https://不指定端口實際上指向端口443. – amenadiel

+0

哦,真的嗎?我對Nginx是完全陌生的,這種事情,你能提出一些代碼來解決它嗎? – Jascination

回答

-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')); 
}); 
+0

你的答案沒有正確格式化,你需要添加一些解釋,而不是僅僅粘貼一些代碼。你在這裏做了什麼,爲什麼? – Jascination

+0

另外'app.configure()'在Express 4.0中不存在 – Jascination

相關問題