繁体   English   中英

无法在nodejs和nginx中提供静态文件

[英]Can't serve static files in nodejs and nginx

我有一个nodejs博客应用程序。 我也使用nginx反向代理服务它。 问题是我无法使用该应用程序加载静态文件。 我在以下文件中看到了我配置的ngix。 但由于某种原因,当我重定向到mysite.com/blog我无法加载静态文件,但在mysite:port / blog我可以。

这是nginx.config文件:

server {
    listen       *:80;

    server_name  www.georgegkas.discrete.gr georgegkas.discrete.gr;

    root   /var/www/georgegkas.discrete.gr/html;
    index  index.php index.html index.htm;


    location / {
        try_files $uri $uri/ =404;
    }

    location ^~ /web-class.gr/ {
        try_files $uri $uri/ =404;
        if (!-e $request_filename){
            rewrite ^(.*)$ /index.html break;
        }
    }

    location /blog {
        root /var/www/georgegkas.discrete.gr/html/GeorgeGks-Blog/app/public;
        try_files $uri $uri/ @nodejs_blog;
        expires max;
        access_log off;
    }

    location @nodejs_blog {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8081;


    }
}

我的app.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());

// Set global app variables
var LAST_RECEIVED_POST_ID = 0;

// Database connection
var mysql = new MYSQL_db({
    host: ENV_VAR.MYSQL.host,
    user: ENV_VAR.MYSQL.user,
    password: ENV_VAR.MYSQL.password,
    database: ENV_VAR.MYSQL.database
});

// '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 DEFINED MIDDLEWARES *******************************/

// -- Index Middleware --
// 1. Get Feature post
// 2. Get other posts by date
// 3. Update LAST_RECEIVED_POST_ID
// 4. Request blog admin profile
// 5. Render the index page
app.get(ENV_VAR.URL_PREFIX_PATH + '/', function(req, res) {
    mysql.select_post('featured', function(err, featured_post) {
        if (err) throw err;
        mysql.select_post({
            status: 'published',
            limit: 10
        }, function(err, posts_res) {
            if (err) throw err;
            var posts = [];
            if (posts_res.length > 0 || featured_post.length > 0) {
                posts = helper.prepare_index_post_data(posts_res, featured_post[0]);
                LAST_RECEIVED_POST_ID = posts[posts.length - 1].post_ID;
            }
            mysql.select_author({
                role: 'admin',
                email: GLOBAL_VAR.ADMIN
            }, function(err, author_res) {
                if (err) throw err;
                res.render('index', {
                    _POST_LIST: posts,
                    _ADMIN_AVATAR: author_res[0].author_avatar
                });
            });
        });
    });
});


/*********************  HANDLE COMMON HTTP ERRORS *****************************/
app.get('/404', function(req, res, next) {
    next();
});

app.get('/403', function(req, res, next) {
    var err = new Error('You have no permission to enter that area.');
    err.status = 403;
    next(err);
});

app.get('/500', function(req, res, next) {
    next(new Error('keyboard cat!'));
});

app.use(function(req, res, next) {
    res.status(404);
    res.render('errors/404');
});

/***********************  START THE APP  **************************************/
app.listen(GLOBAL_VAR.PORT, function() {
    console.log('The Wall personal blog by George G. Gkasdrogkas.');
    console.log('Listening on port ' + GLOBAL_VAR.PORT + '!');
});

我错误配置了什么?

示例代码

upstream nodejs {
    server localhost:3000;
}

server {
    listen 8080;
    server_name localhost;
    root ~/workspace/test/app;

    location / {
        try_files $uri $uri/ @nodejs;
    }

    location @nodejs {
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_pass http://nodejs;
        proxy_set_header Host $host ; 
        proxy_set_header X-Real-IP $remote_addr; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

你可以检查一下

try_files $uri /$uri @nodejs; 应该是try_files $uri $uri/ @nodejs;

并且您可能希望server中的另一个location块用于静态文件。

location /static {
  alias /path/to/static/files;
}

然后你可以在localhost:8080/static/some_file.css点击文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM