首页域名资讯 正文

nginx与node.js结合使用

2025-03-02 9 0条评论

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

流程图

nginx配置如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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.trustauth.cn$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;          }      } }

配置段说明

1 2 3 4 5 6 7 8 9 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将打开更多的连接。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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处理静态内容

1 2 3 4 5 6 7 8 9 10 11 12 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 ;          }          . . .      } }

设置缓存

1 2 3 4 5 6 http {      . . .      proxy_cache_path    / var / cache / nginx levels = 1 : 2 keys_zone = one : 8m max_size = 3000m inactive = 600m ;      proxy_temp_path / var / tmp ;      . . . }
1 2 3 4 5 6 7 8 9 10 11 12 http {    server {      . . .      location / {            . . .            proxy_cache one ;            proxy_cache_key sfs $ request_uri $ scheme ;            . . .      }      . . .    } }

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

 

文章转载来自:trustauth.cn

文章版权及转载声明

本文作者:亿网 网址:https://edns.com/ask/post/150564.html 发布于 2025-03-02
文章转载或复制请以超链接形式并注明出处。