鄭元傑
鄭元傑

Reputation: 1647

How can I use nginx to improve nodejs performance

I am learning how to build up web front-end and backend by myself.
I have read some blogs about using nginx to improve nodejs performance.
But I still have some question , I hope some people could help me!

Serve static file 1. I wana build up a pure doc web(like pebble time dev doc). My plan is using angularjs. Each time user click one item, using ajax to load the specific html resource. As far as I know, Nginx could server static files very well. Can I use Nginx and no nodejs backend here?

2.Build up normal web , including sign in , sign out , session and so on. Some people said that it is really bad to serve and render page by nodejs itself. However, I should check session in each user request. Is it any possible that nodejs check session and then nginx response html file?
(Front-end is angularjs also.) such as

app.get('/about', function (req, res)
{
    response.writeHeader(200, {"Content-Type": "text/html"});  
    response.write(html);  // serve by nginx
    response.end();  
});

Upvotes: 1

Views: 540

Answers (1)

num8er
num8er

Reputation: 19372

I'll be straight.

Better practice to have folder where You have nodejs app with public folder, and tell nginx to look only to public folder for some extensions, but for other requests to pass to nodejs

You nginx host file:

server {
    listen 80;

    server_name yourdomain.com www.yourdomain.com;

    index index.html index.htm;

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm|mp3)$ {
        root /home/yourdomain/public; # or any path to public folder
        expires 30d;
        add_header Pragma public;
        add_header Cache-Control "public";
    }

    # all other requests goes to :8000
    location / {
        log_not_found off;
        access_log off;
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Have folder structure similar to this

enter image description here

Upvotes: 1

Related Questions