2016-12-14 3 views
5

Ich habe eine Nodejs Blog App. Ich benutze es auch mit Nginx Reverse Proxy. Das Problem ist, dass ich statische Dateien mit der App nicht laden kann. Ich habe meinen ngix so konfiguriert, wie Sie in der folgenden Datei sehen können. Aber aus irgendeinem Grund, wenn ich auf mysite.com/blog umleiten kann ich die statischen Dateien nicht laden, aber in mysite: port/blog kann ich.Kann statische Dateien in Nodejs und Nginx nicht liefern

hier ist die nginx.config Datei:

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; 


    } 
} 

meine app.js Datei:

/********************** 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 + '!'); 
}); 

Bin ich etwas falsch konfiguriert ??

+0

Haben Sie den 'Standort/blog' Block über' Lage/'zu setzen versucht? Sie können auch einen Schrägstrich am Ende des Blogs einfügen: 'location/blog /' –

+0

Was sagt Ihr 'error_log'? – cnst

+0

Dies kann Ihnen helfen http://stackoverflow.com/questions/5009324/node-js-nginx-what-now?rq=1 – maheshiv

Antwort

1

Beispielcode

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; 
    } 
} 

Sie this

try_files $uri /$uri @nodejs; sollte wohl try_files $uri $uri/ @nodejs;

sein überprüfen in Ihrem server für die statischen Dateien location Block ein anderes wollen, und Sie werden.

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

Then Sie Dateien auf localhost:8080/static/some_file.css treffen kann

Verwandte Themen