共計 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 小編會繼續努力為大家帶來更多實用的文章!