怎么为Node.js程序配置使用Nginx服务器(nginx,node.js,编程语言)

时间:2024-05-06 13:27:59 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

node.js是一个基于chrome javascript运行时建立的平台, 用于方便地搭建响应速度快、易于扩展的网络应用。node.js 使用事件驱动, 非阻塞i/o 模型而得以轻量和高效,非常适合在分布式设备上运行的数据密集型的实时应用,如实时聊天等等。然而对于gzip编码,静态文件,http缓存,ssl处理,负载平衡和反向代理等,都可以通过nginx来完成,从而减小node.js的负载,并通过nginx强大的缓存来节省网站的流量从而提高网站的加载速度。
流程图

怎么为Node.js程序配置使用Nginx服务器

nginx配置如下:

http{proxy_cache_path/var/cache/nginxlevels=1:2keys_zone=one:8mmax_size=3000minactive=600m;proxy_temp_path/var/tmp;includemime.types;default_typeapplication/octet-stream;sendfileon;keepalive_timeout65;gzipon;gzip_comp_level6;gzip_varyon;gzip_min_length1000;gzip_proxiedany;gzip_typestext/plaintext/htmltext/cssapplication/jsonapplication/x-javascripttext/xmlapplication/xmlapplication/xml+rsstext/javascript;gzip_buffers168k;ssl_certificate/some/location/sillyfacesociety.com.bundle.crt;ssl_certificate_key/some/location/sillyfacesociety.com.key;ssl_protocolssslv3tlsv1;ssl_ciphershigh:!anull:!md5;upstreamsilly_face_society_upstream{server127.0.0.1:61337;server127.0.0.1:61338;keepalive64;}server{listen80;listen443ssl;server_namesillyfacesociety.com;return301$scheme://www.sillyfacesociety.com$request_uri;}server{listen80;listen443ssl;server_namewww.sillyfacesociety.com;error_page502/errors/502.html;location~^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico){root/usr/local/silly_face_society/node/public;access_logoff;expiresmax;}location/errors{internal;alias/usr/local/silly_face_society/node/public/errors;}location/{proxy_redirectoff;proxy_set_headerx-real-ip$remote_addr;proxy_set_headerx-forwarded-for$proxy_add_x_forwarded_for;proxy_set_headerx-forwarded-proto$scheme;proxy_set_headerhost$http_host;proxy_set_headerx-nginx-proxytrue;proxy_set_headerconnection"";proxy_http_version1.1;proxy_cacheone;proxy_cache_keysfs$request_uri$scheme;proxy_passhttp://silly_face_society_upstream;}}}

配置段说明

http{...upstreamsilly_face_society_upstream{server127.0.0.1:61337;server127.0.0.1:61338;keepalive64;}...}

nginx负载均衡多个nodo.js实例。keepalive 64 指示nginx在任何时候保持最少64个http/ 1.1连接到代理服务器。如果有更多的流量nginx将打开更多的连接。

http{...server{...location/{proxy_redirectoff;proxy_set_headerx-real-ip$remote_addr;proxy_set_headerx-forwarded-for$proxy_add_x_forwarded_for;proxy_set_headerhost$http_host;proxy_set_headerx-nginx-proxytrue;...proxy_set_headerconnection"";proxy_http_version1.1;proxy_passhttp://silly_face_society_upstream;}...}}

将符合哪些的请求发送到代理上。nginx的匹配规则可以取看看前面的文章。
nginx处理静态内容

http{...server{...location~^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico){root/usr/local/silly_face_society/node/public;access_logoff;expiresmax;}...}}

设置缓存

http{...proxy_cache_path/var/cache/nginxlevels=1:2keys_zone=one:8mmax_size=3000minactive=600m;proxy_temp_path/var/tmp;...}http{server{...location/{...proxy_cacheone;proxy_cache_keysfs$request_uri$scheme;...}...}}

缓存是通过http头部来控制的。

helloworld
试验一下,我们来写个helloworld.js

varhttp=require('http');http.createserver(function(request,response){response.writehead(200,{'content-type':'text/plain'});response.end('helloworld\n');}).listen(61337);console.log('serverrunningathttp://127.0.0.1:61337/');

然后用node helloworld.js指令开启,这样跑在本地的机子的nodejs的程序就算开起来了,占用的是8000端口,可自己修改。

此时确定在nginx的vhost.conf里面的设置应有:

server{listen80;server_namejb51.net.jb51.net;location/{proxy_passhttp://127.0.0.1:61337;}}

将网站域名设置好,然后端口设置为80,最后proxy_pass设置为http://127.0.0.1:61337,将所有从jb51.net:80的请求传递到nodejs程序去。
重启nginx、访问域名,就可以了看到helloworld了。
虽然node.js本身就可以做服务器是没错啦,比如welcome.js里面设置为80端口就可以了。
但是一个机子跑多个网站,其他网站又是用别的服务器,在80端口已经被占用的情况下,是可以用代理到别的端口来处理的。

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:怎么为Node.js程序配置使用Nginx服务器的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:如何为高负载网络优化Nginx和Node.js下一篇:

27 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18