设置“ .htaccess”文件以强制HTTPS,但出现错误“ Cannot GET /index.php”

时间:2019-11-18 14:25:37

标签: node.js .htaccess https

我想通过app.js(索引文件)在routes文件夹中运行index.js文件。

app.js中的以下代码在本地运行良好,但是当我将HTTP强制为https时,在生产中出现错误。

我的错误:

    Cannot GET /index.php

我的app.js

    var createError = require('http-errors');
    var express = require('express');
    var path = require('path');
    var cookieParser = require('cookie-parser');
    var session = require('express-session');
    var logger = require('morgan');
    var cfenv = require('cfenv');
    var uuid = require("uuid4");
    var lti = require("ims-lti");

    var indexRouter = require('./routes/index');
    var usersRouter = require('./routes/users');

    var app = express();

    app.enable('trust proxy');

    const sessions = {};
    app.set('views', path.join(__dirname, 'views'));
    app.engine('html', require('ejs').renderFile);
    app.set('view engine', 'html');

    app.use(logger('dev'));
    app.use(express.json());
    app.use(express.urlencoded({ extended: false }));
    app.use(cookieParser());
    app.use(express.static(path.join(__dirname, 'public')));
    app.post("*", require("body-parser").urlencoded({extended: true}));

    app.use('/', indexRouter);
    app.use('/users', usersRouter);

    app.post("/get", (req, res) => {

    const moodleData = new lti.Provider("top", "secret");
    moodleData.valid_request(req, (err, isValid) => {

    const sessionID = uuid();
    sessions[sessionID] = moodleData;

    res.send(moodleData.body);
    });
    });

    const port = 3000;

    app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
    });

    app.listen(port, () => console.log(`Example app listening on port ${port}!`));
    module.exports = app;

我的.htaccess文件

    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*)?$ http://127.0.0.1:3000/$1 [P,L]

我不确定.htaccess文件中的错误吗?

1 个答案:

答案 0 :(得分:0)

您是否使用反向代理重定向到Nodejs服务器?

查看本文

[https://medium.com/@sumitnair89/configure-apache-with-node-js-application-on-aws-ubuntu-18-04-server-for-different-http-ports-4e6838c7357f][1]

基本上,您需要创建一个虚拟主机并告诉Apache

<VirtualHost *:80> 
 ...
 ServerName mySite
 ServerAlias www.abcd.com
 ...
 ...
 ...
 ProxyRequests Off
 ProxyPreserveHost On
 ProxyVia Full
 <Proxy *>
    Require all granted
 </Proxy>
<Location />
   ProxyPass http://127.0.0.1:3000
   ProxyPassReverse http://127.0.0.1:3000
 </Location>

</VirtualHost>

现在可以通过以下方式访问您的网站 www.abcd.com

启用HTTPS,并且您使用的是Linux尝试

[https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-18-04][1]