编码随笔

node+express的nginx配置

    项目使用node+express,express配置了路由如下:
    router.get('/’, function(req, res, next) {
    res.render(‘index’);
    });
    router.get('/aaa’, function(req, res, next) {
    res.render(‘blog/aaa.html’);
    });
    
    nginx配置:
    upstream nodeweb-server {
    server localhost:3000;
    }
    server {
    listen 80;
    server_name www.mywebsite.com mywebsite.com;
    location / {
    root /;
    proxy_pass http://nodeweb-server;
    proxy_redirect off;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    }
    location ~ .*.(gif|jpg|jpeg|png|bmp|swf|js|css|woff|ttf|TTF|svg)$ {
    root /opt/NodeWeb/public/;
    if (-f $request_filename) {
    expires 100d;
    break;
    }
    }

    这样的话,就可以通过nginx反向代理node+express项目,并且很好的缓存静态资源,提高访问速度。

by will,2015

签名: