2016-12-14 10 views
5

में स्थिर फ़ाइलों को सेवा नहीं दे सकता है मेरे पास एक नोडजे ब्लॉग ऐप है। मैं nginx रिवर्स प्रॉक्सी का उपयोग कर इसे भी सेवा देता हूं। समस्या यह है कि मैं ऐप का उपयोग कर स्थिर फाइल लोड नहीं कर सकता। मैंने अपना ngix कॉन्फ़िगर किया है जैसा कि आप निम्न फ़ाइल में देख सकते हैं। लेकिन किसी कारण से जब मैं mysite.com/blog पर रीडायरेक्ट करता हूं, तो मैं स्थिर फाइल लोड नहीं कर सकता, लेकिन mysite में: पोर्ट/ब्लॉग मैं कर सकता हूं।नोडजेस और nginx

यहाँ 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 + '!'); 
}); 

हूँ मैं कुछ गलत तरीके से कॉन्फ़िगर किया गया ??

+0

पर फ़ाइलों हिट कर सकते हैं कि आप ऊपर 'स्थान /' 'स्थान/blog' ब्लॉक डालने की कोशिश की है? साथ ही, आप ब्लॉग के अंत में एक स्लैश पूट करना चाह सकते हैं: 'location/blog /' –

+0

आपकी 'error_log' क्या कहती है? – cnst

+0

यह आपकी मदद कर सकता है http://stackoverflow.com/questions/5009324/node-js-nginx-what-now?rq=1 – maheshiv

उत्तर

1

नमूना कोड

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

आप देख सकते हैं this

try_files $uri /$uri @nodejs;try_files $uri $uri/ @nodejs;

होना चाहिए और आप शायद स्थिर फ़ाइलों के लिए आपके server भीतर एक और location ब्लॉक चाहता हूँ।

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

Then आप localhost:8080/static/some_file.css