如何在centos7上使用apache在nodejs app上添加https支持

时间:2016-10-29 09:52:09

标签: node.js apache ssl centos

我在我的虚拟centos7机器上配置了httpd(apache)并添加了一个来自letsencrypt的ssl证书。我还将其配置为通过https协议传递所有请求。

问题

在我的服务器中,我创建了许多项目。其中一些是在php中,另一些在nodejs上。问题是,当我运行nodejs应用程序时,即使控制台消息显示它在我的端口(8080)上按预期运行,它也不会加载到我的浏览器上。

我期待什么

假设域名是example.com,项目文件夹名为blog,我想使用以下网址来访问它https://example.com/blog

Apache配置

ServerRoot "/etc/httpd"

Listen 80

Include conf.modules.d/*.conf

User apache
Group apache
ServerAdmin root@localhost

<Directory />
    AllowOverride none
    Require all denied
</Directory>

DocumentRoot "/var/www/html"

<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

# Further relax access to the default document root:
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted

    # SET STRICT TRANSPORT SECURITY
    Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
</Directory>

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>

EnableSendfile on

# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
IncludeOptional sites-enabled/*.conf


SSLCompression off
SSLProtocol All -SSLv2 -SSLv3
SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-GCM-SHA256:AES256+EDH:ECDHE-RSA-AES256-GCM-SHA384:ECDH$
SSLHonorCipherOrder On
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
Header always set X-Frame-Options DENY
Header always set X-Content-Type-Options nosniff
SSLUseStapling on
SSLStaplingCache "shmcb:logs/stapling-cache(150000)"
Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure

我的虚拟主机配置

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html
    ErrorLog /var/log/apache/example.com/error.log
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.example.com [OR]
    RewriteCond %{SERVER_NAME} =example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
    Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains;"

    SSLCertificateFile "/etc/letsencrypt/live/example.com/fullchain.pem"
    SSLCertificateKeyFile "/etc/letsencrypt/live/example.com/privkey.pem"

    Options -Indexes
</VirtualHost>

节点项目的index.js文件

/********************** APP DEPENDENCES AND CONFIGURES ************************/
// Include required modules
var compression = require('compression');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var MYSQL_db = require('./models/database/MYSQL');
var helper = require('./models/utils/functions');
var util = require('util');
require('./models/utils/extend_prototype');

// Include global configure file
var GLOBAL_VAR = require('./config/global');

// Include environmental specific configure file
switch (process.env.NODE_ENV) {
    case 'development':
        var ENV_VAR = require('./config/dev');
        app.locals.url_prefix = ENV_VAR.URL_PREFIX_PATH;
        break;
    case 'production':
        var ENV_VAR = require('./config/production');
        app.locals.url_prefix = ENV_VAR.URL_PREFIX_PATH;
        break;
    default:
        console.error("Unrecognized NODE_ENV: " + process.env.NODE_ENV);
        process.exit(1);
}

// Configure express static files and template language to use
app.set('views', __dirname + '/views');
app.set('view engine', 'pug');
app.use(express.static('public'));

// Configure the middlewares
app.use(compression());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());


// 'Can't set headers after they are sent', fix
app.use(function(req, res, next) {
    var _send = res.send;
    var sent = false;
    res.send = function(data) {
        if (sent) return;
        _send.bind(res)(data);
        sent = true;
    };
    next();
});

app.get(ENV_VAR.URL_PREFIX_PATH + '/', function(req, res) {
    // render the view
});


app.listen(GLOBAL_VAR.PORT, function() {

    console.log('Listening on port ' + GLOBAL_VAR.PORT + '!');
});

我的global.js和productions.js配置文件

// Global.js
module.exports = {
    PORT: 8080
}

// Production.js
module.exports = {
    URL_PREFIX_PATH: '/blog'
}

我使用以下命令运行项目:

NODE_ENV=production node /var/www/html/blog/app/app.js

如何在默认的443端口上访问页面?我试图在nodejs上添加https模块,将代理添加到apache,但它没有工作。

0 个答案:

没有答案