我有一个设置,我需要nginx将位置转发到运行Express Server的localhost上的特定端口(8082)。正确处理了html页面,但是直接在pubic中找到的静态文件返回“无法获取”。
Nginx服务器块:
server {
# SSL configuration
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl on;
ssl_certificate /etc/ssl/certs/cert.pem;
ssl_certificate_key /etc/ssl/private/cert.pem;
server_name domain.com www.domain.com;
location ~* ^/directory/public/(.*) {
proxy_pass http://localhost:8082/public/$1;
}
location /directory {
proxy_pass http://localhost:8082/directory;
}
}
快速设置:
var express = require('express');
var app = express();
var path = require('path');
var config = require("./config");
var port = 8082;
// set the view engine to ejs
app.set('view engine','ejs');
app.set('views',__dirname + '/src');
app.use('/public',express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({
extended: true
}));
// index page
app.get('/domain',function (req,res) {
res.render('views/pages/index',config.domain);
});
app.listen(port);
console.log(port + ' is the magic port');