這篇文章主要介紹“Node.js中怎么配置和使用Nginx服務(wù)器”,在日常操作中,相信很多人在Node.js中怎么配置和使用Nginx服務(wù)器問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Node.js中怎么配置和使用Nginx服務(wù)器”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
10年積累的網(wǎng)站設(shè)計(jì)、做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)制作后付款的網(wǎng)站建設(shè)流程,更有遷安免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
node.js是一個(gè)基于chrome javascript運(yùn)行時(shí)建立的平臺(tái), 用于方便地搭建響應(yīng)速度快、易于擴(kuò)展的網(wǎng)絡(luò)應(yīng)用。node.js 使用事件驅(qū)動(dòng), 非阻塞i/o 模型而得以輕量和高效,非常適合在分布式設(shè)備上運(yùn)行的數(shù)據(jù)密集型的實(shí)時(shí)應(yīng)用,如實(shí)時(shí)聊天等等。然而對(duì)于gzip編碼,靜態(tài)文件,http緩存,ssl處理,負(fù)載平衡和反向代理等,都可以通過nginx來完成,從而減小node.js的負(fù)載,并通過nginx強(qiáng)大的緩存來節(jié)省網(wǎng)站的流量從而提高網(wǎng)站的加載速度。
流程圖
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負(fù)載均衡多個(gè)nodo.js實(shí)例。keepalive 64 指示nginx在任何時(shí)候保持最少64個(gè)http/ 1.1連接到代理服務(wù)器。如果有更多的流量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; } ... } }
將符合哪些的請(qǐng)求發(fā)送到代理上。nginx的匹配規(guī)則可以取看看前面的文章。
nginx處理靜態(tài)內(nèi)容
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; } ... } }
設(shè)置緩存
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 { server { ... location / { ... proxy_cache one; proxy_cache_key sfs$request_uri$scheme; ... } ... } }
緩存是通過http頭部來控制的。
helloworld
試驗(yàn)一下,我們來寫個(gè)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指令開啟,這樣跑在本地的機(jī)子的nodejs的程序就算開起來了,占用的是8000端口,可自己修改。
此時(shí)確定在nginx的vhost.conf里面的設(shè)置應(yīng)有:
server { listen 80; server_name jb51.net.jb51.net; location / { proxy_pass http://127.0.0.1:61337; } }
將網(wǎng)站域名設(shè)置好,然后端口設(shè)置為80,最后proxy_pass設(shè)置為http://127.0.0.1:61337,將所有從jb51.net:80的請(qǐng)求傳遞到nodejs程序去。
重啟nginx、訪問域名,就可以了看到helloworld了。
雖然node.js本身就可以做服務(wù)器是沒錯(cuò)啦,比如welcome.js里面設(shè)置為80端口就可以了。
但是一個(gè)機(jī)子跑多個(gè)網(wǎng)站,其他網(wǎng)站又是用別的服務(wù)器,在80端口已經(jīng)被占用的情況下,是可以用代理到別的端口來處理的。
到此,關(guān)于“Node.js中怎么配置和使用Nginx服務(wù)器”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
標(biāo)題名稱:Node.js中怎么配置和使用Nginx服務(wù)器
文章網(wǎng)址:http://jinyejixie.com/article48/gpcpep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、關(guān)鍵詞優(yōu)化、軟件開發(fā)、、云服務(wù)器、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)