Node.js中怎么配置和使用Nginx服务器

64次阅读
没有评论

共计 3901 个字符,预计需要花费 10 分钟才能阅读完成。

这篇文章主要介绍“Node.js 中怎么配置和使用 Nginx 服务器”,在日常操作中,相信很多人在 Node.js 中怎么配置和使用 Nginx 服务器问题上存在疑惑,丸趣 TV 小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Node.js 中怎么配置和使用 Nginx 服务器”的疑惑有所帮助!接下来,请跟着丸趣 TV 小编一起来学习吧!

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

nginx 配置如下:

 http {
 proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;
 proxy_temp_path /var/tmp;
 include mime.types;
 default_type application/octet-stream;
 sendfile on;
 keepalive_timeout 65;
 
 gzip on;
 gzip_comp_level 6;
 gzip_vary on;
 gzip_min_length 1000;
 gzip_proxied any;
 gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
 gzip_buffers 16 8k;
 
 ssl_certificate /some/location/sillyfacesociety.com.bundle.crt;
 ssl_certificate_key /some/location/sillyfacesociety.com.key;
 ssl_protocols sslv3 tlsv1;
 ssl_ciphers high:!anull:!md5;
 
 upstream silly_face_society_upstream {
 server 127.0.0.1:61337;
 server 127.0.0.1:61338;
 keepalive 64;
 }
 
 server {
 listen 80;
 listen 443 ssl;
 
 server_name sillyfacesociety.com;
 return 301 $scheme://www.sillyfacesociety.com$request_uri;
 }
 
 server {
 listen 80;
 listen 443 ssl;
 
 server_name www.sillyfacesociety.com;
 
 error_page 502 /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_log off;
 expires max;
 }
 
 location /errors {
 internal;
 alias /usr/local/silly_face_society/node/public/errors;
 }
 
 location / {
 proxy_redirect off;
 proxy_set_header x-real-ip $remote_addr;
 proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
 proxy_set_header x-forwarded-proto $scheme;
 proxy_set_header host $http_host;
 proxy_set_header x-nginx-proxy true;
 proxy_set_header connection  
 proxy_http_version 1.1;
 proxy_cache one;
 proxy_cache_key sfs$request_uri$scheme;
 proxy_pass http://silly_face_society_upstream;
 }
 }
}

配置段说明

http {
 ...
 upstream silly_face_society_upstream {
 server 127.0.0.1:61337;
 server 127.0.0.1:61338;
 keepalive 64;
 }
 ...
}

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

http {
 ...
 server {
 ...
 location / {
 proxy_redirect off;
 proxy_set_header x-real-ip $remote_addr;
 proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
 proxy_set_header host $http_host;
 proxy_set_header x-nginx-proxy true;
 ...
 proxy_set_header connection  
 proxy_http_version 1.1;
 proxy_pass http://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_log off;
 expires max;
 }
 ...
 }
}

设置缓存

http {
 ...
 proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;
 proxy_temp_path /var/tmp;
 ...

 } }

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

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

var http = require( http  
 
 
http.createserver(function (request, response) { 
 
 response.writehead(200, { content-type :  text/plain  
 response.end(hello world\n  }).listen(61337); 
 
 
console.log(server running at http://127.0.0.1:61337/

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

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

server { 
 listen 80; 
 server_name jb51.net.jb51.net; 
 location / { 
 proxy_pass http://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 端口已经被占用的情况下,是可以用代理到别的端口来处理的。

到此,关于“Node.js 中怎么配置和使用 Nginx 服务器”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注丸趣 TV 网站,丸趣 TV 小编会继续努力为大家带来更多实用的文章!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-13发表,共计3901字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)