Node.js + Nginx - දැන් කුමක් ද?


1011

මම මගේ සේවාදායකයේ Node.js සහ Nginx සකස් කර ඇත්තෙමි. දැන් මට එය භාවිතා කිරීමට අවශ්‍යයි, නමුත්, මම ආරම්භ කිරීමට පෙර ප්‍රශ්න 2 ක් ඇත:

  1. ඔවුන් එකට වැඩ කළ යුත්තේ කෙසේද? ඉල්ලීම් හැසිරවිය යුත්තේ කෙසේද?
  2. Node.js සේවාදායකයක් සඳහා සංකල්ප 2 ක් ඇත, ඒවා වඩා හොඳය:

    ඒ. අවශ්‍ය සෑම වෙබ් අඩවියකටම වෙනම HTTP සේවාදායකයක් සාදන්න. වැඩසටහන ආරම්භයේදී සියලුම ජාවාස්ක්‍රිප්ට් කේත පටවන්න, එවිට කේතය එක් වරක් අර්ථ නිරූපණය කෙරේ.

    බී. සියලුම Node.js ඉල්ලීම් හසුරුවන තනි Node.js සේවාදායකයක් සාදන්න. මෙය ඉල්ලූ ලිපිගොනු කියවන අතර ඒවායේ අන්තර්ගතය ඉවත් කරයි. එබැවින් එක් එක් ඉල්ලීම මත ලිපිගොනු අර්ථ නිරූපණය කරනු ලැබේ, නමුත් සේවාදායක තර්කනය වඩාත් සරල ය.

Node.js නිවැරදිව භාවිතා කරන්නේ කෙසේදැයි මට පැහැදිලි නැත.

Answers:


1317

Nginx ඉදිරිපස සේවාදායකයක් ලෙස ක්‍රියා කරයි, මේ අවස්ථාවේ දී ඉල්ලීම් node.js සේවාදායකයක් වෙත යොමු කරයි. එබැවින් ඔබ නෝඩ් සඳහා nginx වින්‍යාස ගොනුවක් සැකසිය යුතුය.

මගේ උබුන්ටු පෙට්ටියේ මා කර ඇත්තේ මෙයයි:

ගොනුව නිර්මාණය yourdomain.comදී /etc/nginx/sites-available/:

vim /etc/nginx/sites-available/yourdomain.com

එහි ඔබට එවැනි දෙයක් තිබිය යුතුය:

# the IP(s) on which your node server is running. I chose port 3000.
upstream app_yourdomain {
    server 127.0.0.1:3000;
    keepalive 8;
}

# the nginx server instance
server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.com www.yourdomain.com;
    access_log /var/log/nginx/yourdomain.com.log;

    # pass the request to the node.js server with the correct headers
    # and much more can be added, see nginx config options
    location / {
      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_pass http://app_yourdomain/;
      proxy_redirect off;
    }
 }

වෙබ්සොකට් ඉල්ලීම් ද හැසිරවීමට ඔබට nginx (> = 1.3.13) අවශ්‍ය නම්, location /කොටසේ පහත දැක්වෙන පේළි එක් කරන්න :

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

ඔබ මෙම සැකසුම ලබා ගත් පසු ඉහත වින්‍යාස ගොනුවේ අර්ථ දක්වා ඇති වෙබ් අඩවිය සක්‍රීය කළ යුතුය:

cd /etc/nginx/sites-enabled/ 
ln -s /etc/nginx/sites-available/yourdomain.com yourdomain.com

ඔබගේ නෝඩ් සේවාදායක යෙදුම නිර්මාණය කර /var/www/yourdomain/app.jsඑය ක්‍රියාත්මක කරන්නlocalhost:3000

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');

සින්ටැක්ස් වැරදි සඳහා පරීක්ෂණය:

nginx -t

Nginx නැවත ආරම්භ කරන්න:

sudo /etc/init.d/nginx restart

අවසාන වශයෙන් නෝඩ් සේවාදායකය ආරම්භ කරන්න:

cd /var/www/yourdomain/ && node app.js

දැන් ඔබ ඔබේ ඩොමේන්.කොම් හි "හෙලෝ වර්ල්ඩ්" දැකිය යුතුය

නෝඩ් සේවාදායකය ආරම්භ කිරීම සම්බන්ධයෙන් අවසාන සටහනක්: ඔබ නෝඩ් ඩීමන් සඳහා යම් ආකාරයක අධීක්ෂණ පද්ධතියක් භාවිතා කළ යුතුය. භයානක නැහැ මත් වර්ජකයන්ගේ හා monit සමග node එකක් මතම ඊට අදාල පිළිබඳ නිබන්ධනය .


11
ලිපියට ස්තූතියි, ඉහත සේවාදායකය සඳහා nginx cache node.js ප්‍රතිචාර දක්වනු ඇත, නැතහොත් සෑම අවස්ථාවකදීම ඒවා නැවත ක්‍රියාත්මක කරන්න.
දෙහි

79
ඔබට කළ නොහැකි වීමට හේතුවක් location / { proxy_pass http://127.0.0.1:3000; }තිබේද? ඔබට සම්පූර්ණ upstreamවින්‍යාස බිට් අවශ්‍ය වන්නේ ඇයි ?
රොබින් වින්ස්ලෝ

20
+1, පොදු ප්‍රශ්නයකට ඉතා සරල හා සරල පිළිතුර; node සහ nginx භාවිතා කරමින් අතථ්‍ය ධාරක සැකසීමට කැමති අයට විශිෂ්ටයි. ඔබට මග හැරුණු එකම දෙය නම් බහුවිධ තල්මසුන්ට සේවය කිරීම සඳහා nginx-in-front-node එක වඩාත් සුදුසු වන්නේ මන්ද යන්නට ගුණාත්මක පිළිතුරකි (අසන්නාගේ දෙවන ප්‍රශ්නය).
පෝල් ඩි ඕස්ට්

34
බර පැටවීම සඳහා සේවාදායකයන් සඳහා තවත් සේවාදායකයන් එක් කිරීමට ඔබට අවශ්‍ය නම් රොබින් වින්ස්ලෝ.
ජෝඕ ඩා සිල්වා

76
මෙම (ඉතා ප්‍රයෝජනවත්) පිළිතුරෙන් nginx හි එක් රසය, පෙරනිමියෙන් එන sites-enabledසහ sites-availableඇතුළත නාමාවලි වලට යොමු වන බව සැලකිල්ලට ගත යුතුය /etc/nginx. මෙම නාමාවලි දෙක නොමැතිව ඔබේ අනුවාදය පැමිණියේ නම්, ඒ conf.dවෙනුවට තනි නාමාවලියක් ඇත. එවැනි අවස්ථාවකදී, මෙම උපදෙස් අනුගමනය කිරීමෙන් කිසිදු බලපෑමක් සිදු නොවනු ඇත, පෙරනිමිය වෙනුවට යොමු කිරීම includeසඳහා ගොනුව තුළ ඇති ප්‍රකාශය වෙනස් නොකරන්න . තේරුමක් ඇති බලාපොරොත්තුව. එම ප්‍රකාශය ඇතුළත දුටු විගස එය ස්වයං පැහැදිලි කිරීමක් විය යුතුය . nginx.confsites-enabledconf.dincludenginx.conf
හමුවීම

170

ඔබට nginx සමඟ බහු වසමක් සැකසිය හැකිය, බහු node.js ක්‍රියාවලි වෙත යොමු කරයි.

මේවා සාක්ෂාත් කර ගැනීම සඳහා උදාහරණයක් ලෙස:

  • domain1.com -> සිට Node.js ක්‍රියාවලිය දේශීයව ක්‍රියාත්මක වේ http://127.0.0.1:4000
  • domain2.com -> සිට Node.js ක්‍රියාවලිය දේශීයව ක්‍රියාත්මක වේ http://127.0.0.1:5000

ඔබගේ යෙදුම් කේතයේ යෙදුම් ඉල්ලීම්වලට සවන් දීමට මෙම වරායන් (4000 සහ 5000) භාවිතා කළ යුතුය.

/ etc / nginx / sites-enable / domain1

server {
    listen 80;
    listen [::]:80;
    server_name domain1.com;
    access_log /var/log/nginx/domain1.access.log;
    location / {
        proxy_pass    http://127.0.0.1:4000/;
    }
}

/ Etc / nginx / sites-enable / domain2 තුළ

server {
    listen 80;
    listen [::]:80;
    server_name domain2.com;
    access_log /var/log/nginx/domain2.access.log;
    location / {
        proxy_pass    http://127.0.0.1:5000/;
    }
}

5
මම ඔබේ ප්‍රොක්සි_පාස් ක්‍රමය භාවිතා කරමි, නමුත් කිසියම් හේතුවක් http://example.comනිසා ස්වයංක්‍රීයව එය ලැබෙනු 302ඇත http://www.example.com. ඇයි ඒ?
ක්‍රිස්ටියන්

ඔබට Cloudflare හෝ ඊට සමාන යමක් තිබේද? ඉහත වින්‍යාසය කිසිසේත් හරවා යැවිය යුතු නොවේ.
ozzieisaacs

1
Rist ක්‍රිස්ටියන් proxy_set_header Host $hostHTTP 302 යළි හරවා යැවීම වළක්වා ගැනීම සඳහා ඔබ එකතු කළ යුතුය .
අයිවන් ෂැට්ස්කි

AnIvanShatsky - බහු උප වසම් සමඟ බහු වරායන් වින්‍යාසගත කරන්නේ කෙසේද සහ වෙනත් වසමක වෙනත් වරායන් වලක්වා ගන්නේ කෙසේද යන්න ඔබට උදව් කළ හැකිද? Nginx v 1.14.1
151291

61

එක් සේවාදායක වින්‍යාසය තුළ ඔබට යෙදුම් සඳහා විවිධ යූආර්එල් තිබිය හැකිය:

  • yourdomain.com/app1/* -> සිට Node.js ක්‍රියාවලිය දේශීයව ක්‍රියාත්මක වේ http://127.0.0.1:3000
  • yourdomain.com/app2/* -> සිට Node.js ක්‍රියාවලිය දේශීයව ක්‍රියාත්මක වේ http://127.0.0.1:4000

දී / etc / විකිපීඩියා / වෙබ් අඩවි සක්රීය / yourdomain :

server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.com;

    location ^~ /app1/{
        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_pass    http://127.0.0.1:3000/;
    }

    location ^~ /app2/{
        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_pass    http://127.0.0.1:4000/;
    }
}

Nginx නැවත ආරම්භ කරන්න:

sudo service nginx restart

යෙදුම් ආරම්භ කිරීම.

node app1.js

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello from app1!\n');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');

node app2.js

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello from app2!\n');
}).listen(4000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:4000/');

3
විවෘත මූලාශ්‍ර ප්‍රජා අනුවාදය නොමිලේ වන නමුත් ඒවා නොමිලේ නොමැති වෙනත් අංග සමඟ අනුවාදයක් ඇත. nginx.com/products/feature-matrix
0x8BADF00D

මගේ නොදැනුවත්කම ගැන කණගාටුයි. මේ ආකාරයෙන් සේවය කිරීමේ අරමුණ, ප්‍රතිලාභ මොනවාද? ඔබට කිසියම් උදාහරණයක් හෝ භාවිතයක් තිබේද? කලින්ම ස්තූතියි.
මවුරෝ අගුවිලර්

2
AuMauroAguilar ඔබට එක් සේවාදායකයක 2 node.js යෙදුමක් අවශ්‍ය නම් ඔබට යෝජිත ක්‍රමයෙන් (විවිධ වරායන් භාවිතා කරමින්) ඔවුන්ට සේවය කළ හැකිය. මගේ අවස්ථා වලදී එය වෙනස් පරීක්ෂණ යෙදුම් දෙකක් විය.
0x8BADF00D

හරි, නමුත් යෙදුම් 2 ක් සහ තනි එකක් ධාවනය කිරීම අතර ඇති වෙනස කුමක්ද? මම අදහස් කළේ, ඒවා එකම අරමුණක් සඳහා අදහස් කළේ නම් ලැබෙන ප්‍රතිලාභ මොනවාද?
මවුරෝ අගුවිලර්

2
AuMauroAguilar, ඔබට ඒවා තනි එකකින් ධාවනය කළ හැකි අතර එය එක් ව්‍යාපෘතියක කොටසක් විය හැකි අතර එකම අරමුණක් තිබේ නම් කිසිදු ප්‍රතිලාභයක් නොමැත. නමුත් ඔබට විවිධ ව්‍යාපෘති 2 ක් විවිධ අරමුණු සහිතව සහ එක් සේවාදායකයක විවිධ වින්‍යාසයන් සමඟ ක්‍රියාත්මක කිරීමට අවශ්‍ය නම් මෙම වින්‍යාසය භාවිතා කිරීමෙන් ඔබට ප්‍රතිලාභ ලැබේ.
0x8BADF00D

35

මම ස්වාධීන Node Express යෙදුම් Nginx හරහා ප්‍රොක්සි කරමි.

මේ අනුව නව යෙදුම් පහසුවෙන් සවි කළ හැකි අතර එකම සේවාදායකයේ වෙනත් දේවල් විවිධ ස්ථානවල ධාවනය කළ හැකිය.

Nginx වින්‍යාස කිරීමේ උදාහරණය සමඟ මගේ සැකසුම පිළිබඳ වැඩි විස්තර මෙන්න:

එක් වෙබ් සේවාදායකයක Nginx සමඟ උප ෆෝල්ඩර වල බහු නෝඩ් යෙදුම් යොදන්න

ඔබගේ යෙදුම localhost සිට අන්තර්ජාලයට ගෙනයාමට අවශ්‍ය වූ විට Node සමඟ දේවල් ව්‍යාකූල වේ.

නෝඩ් යෙදවීම සඳහා පොදු ප්‍රවේශයක් නොමැත.

ගූගල් හට මෙම මාතෘකාව පිළිබඳ ලිපි ටොන් ගණනක් සොයා ගත හැකි නමුත් මට අවශ්‍ය සැකසුම සඳහා නිසි විසඳුමක් සෙවීමට මම මහත් පරිශ්‍රමයක් දැරීය.

මූලික වශයෙන්, මට වෙබ් සේවාදායකයක් ඇති අතර යෙදුම් කේතයට කිසිදු වින්‍යාස පරායත්තතාවයක් හඳුන්වා නොදී නෝඩ් යෙදුම් උප ෆෝල්ඩර වලට (එනම් http: // myhost / demo / pet-project / ) සවි කිරීමට මට අවශ්‍යය .

ඒ සමඟම බ්ලොග් වැනි වෙනත් දේවල් එකම වෙබ් සේවාදායකයක් තුළ ක්‍රියාත්මක වීමට මට අවශ්‍යය.

සරලයි වගේ නේද? පෙනෙන විදිහට නැහැ.

වෙබයේ බොහෝ උදාහරණ වල යෙදුම් එක්කෝ වරාය 80 මත ධාවනය වේ, නැතහොත් එන්ජින්ක්ස් විසින් මූලයට ප්‍රොක්සි කර ඇත.

මෙම ප්‍රවේශයන් දෙකම ඇතැම් භාවිත අවස්ථා සඳහා වලංගු වුවද, ඒවා මගේ සරල නමුත් ටිකක් විදේශීය නිර්ණායක සපුරාලන්නේ නැත.

මා විසින්ම Nginx වින්‍යාසය නිර්මාණය කළේ එබැවිනි.

upstream pet_project {
  server localhost:3000;
}

server {
  listen 80;
  listen [::]:80;
  server_name frontend;

  location /demo/pet-project {
    alias /opt/demo/pet-project/public/;
    try_files $uri $uri/ @pet-project;
  }

  location @pet-project {
    rewrite /demo/pet-project(.*) $1 break;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $proxy_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://pet_project;
    proxy_redirect http://pet_project/ /demo/pet-project/;
  }
}

මෙම උදාහරණයෙන් ඔබට 3000 වරායේ ධාවනය වන මගේ සුරතල් ව්‍යාපෘති නෝඩ් යෙදුම http: // myhost / demo / pet-project වෙත සවි කර ඇති බව ඔබට පෙනෙනු ඇත. .

ඉල්ලුම් කරන ලද සම්පත ස්ථිතික ගොනුවක්ද / opt / demo / pet-project / public / වෙතින් ලබා ගත හැකිදැයි පළමු Nginx පරික්ෂා කරයි. එසේ නම් එය ඉතා කාර්යක්ෂම ලෙස එය සේවය කරයි, එබැවින් අපට Connect වැනි අතිරික්ත ස්ථරයක් අවශ්‍ය නොවේ. ස්ථිතික මිඩ්ල්වෙයාර්.

එවිට අනෙක් සියලුම ඉල්ලීම් නැවත ලියනු ලබන අතර සුරතල් ව්‍යාපෘති ව්‍යාපෘති නෝඩ් වෙත යොමු කරනු ලැබේ යෙදුමට , එබැවින් නෝඩ් යෙදුමට එය සැබවින්ම සවිකර ඇත්තේ කොතැනදැයි දැන ගැනීමට අවශ්‍ය නොවන අතර එමඟින් වින්‍යාසය මඟින් ඕනෑම තැනකට ගෙන යා හැකිය.

ස්ථාන ශීර්ෂය නිසියාකාරව හැසිරවීමට proxy_redirect අත්‍යවශ්‍ය වේ. ඔබ res.redirect () භාවිතා කරන්නේ නම් මෙය අතිශයින් වැදගත් වේඔබගේ Node යෙදුමේ වේ.

විවිධ වරායන්හි ධාවනය වන බහු නෝඩ් යෙදුම් සඳහා ඔබට මෙම සැකසුම පහසුවෙන් අනුකරණය කළ හැකි අතර වෙනත් අරමුණු සඳහා තවත් ස්ථාන හසුරුවන්නන් එක් කළ හැකිය.

සිට: http://skovalyov.blogspot.dk/2012/07/deploy-multiple-node-applications-on.html



සබැඳිය පමණක් පිළිතුර… ඔබ බ්ලොග් අඩවිය අතුරුදහන් වුවහොත් කරුණාකර ඔබේ පිළිතුරේ අදාළ කොටස් සාරාංශ කළ හැකිද?
කයිසර්

11

Nginx වින්‍යාසය සමඟ Node.js.

$ sudo nano /etc/nginx/sites-available/subdomain.your_domain.com

පහත දැක්වෙන වින්‍යාසය එක් කරන්න, එවිට අපි “subdomain.your_domain.com” වෙතින් පැමිණෙන විට Nginx සේවාදායකයෙන් 3000 ගමනාගමනය වෙත යොමු කරයි.

upstream subdomain.your_domain.com {
  server 127.0.0.1:3000;
}
server {
  listen 80;
  listen [::]:80;
  server_name subdomain.your_domain.com;
  access_log /var/log/nginx/subdomain.your_domain.access.log;
  error_log /var/log/nginx/subdomain.your_domain.error.log debug;
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://subdomain.your_domain.com;
    proxy_redirect off;
  }
}

9

ඔබේ ප්‍රශ්නයට පිළිතුරු 2:

මම විකල්පය භාවිතා කරමි b කරන්නේ එය අඩු සම්පත් ප්‍රමාණයක් පරිභෝජනය කරන බැවිනි. 'a' විකල්පය සමඟ, සෑම සේවාදායකයෙක්ම සේවාදායකයාට විශාල මතකයක් පරිභෝජනය කිරීමට හේතු වන අතර, ඔබට අවශ්‍ය සියලුම ලිපිගොනු පූරණය වේ (මම php ට කැමති වුවද, මෙය එහි ඇති එක් ගැටලුවකි). 'B' විකල්පය සමඟින් ඔබට ඔබේ පුස්තකාල (නැවත භාවිතා කළ හැකි කේතය) පූරණය කර ඒවා සියළුම සේවාදායක ඉල්ලීම් අතර බෙදා ගත හැකිය.

නමුත් ඔබ සතුව බහු හර තිබේ නම් ඒවා සියල්ලම භාවිතා කිරීම සඳහා node.js tweak කළ යුතුය.


2
සම්පත් ඔබේ වැදගත්ම ප්‍රශ්නය නම් (නොහැක්කකි) මෙම උපදෙස් අනුගමනය කරන්න. (අ) සහ (ආ) අතර විවිධ සම්මුතීන් තිබේ. වෙබ් අඩවි වඩාත් ස්වාධීන වීමට ඔබ කැමති නම් විකල්පය (අ) වඩා හොඳය. උදා: අඩවි නැවත ආරම්භ කිරීම හෝ නඩත්තු කිරීම, ඩීබී සම්බන්ධතා, කේත පදනම, පුස්තකාල පරායත්තතා, සේවාදායකයන් අතර චලනය වන අඩවි යනාදිය.
රොබෝ කැට්

8

මම ගිතුබ් හි ගබඩාවක් සාදා ඔබට ක්ලෝන කළ හැකි, නොපැහැදිලි-නෝඩ්-එන්ජින්ක්ස්-බොයිලර් ප්ලේට්

මූලික වශයෙන් node.js යෙදුම /var/www/nodeappවේ

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(4570, '127.0.0.1');

console.log('Node Server running at 127.0.0.1:4570/');

සහ nginx වින්‍යාසය /etc/nginx/sites-available/වේ

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/nodeapp;
        index index.html index.htm;

        server_name localhost;

        location / {
          proxy_pass http://127.0.0.1:4570;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
        }
}

6

ස්ථිතික ලිපිගොනු nginx විසින් සපයනු ලබන නාමාවලියකට ජනනය කිරීමට ඔබට node.js භාවිතා කළ හැකිය. ඇත්ත වශයෙන්ම, ඔබේ වෙබ් අඩවියේ සමහර ගතික කොටස් නෝඩ් මගින් ද, සමහරක් එන්ජින්ක්ස් (ස්ථිතික) මගින් ද සේවය කළ හැකිය.

ඔවුන්ගෙන් සමහරක් nginx මගින් සේවය කිරීමෙන් ඔබේ ක්‍රියාකාරිත්වය වැඩි වේ ..


5

ප්‍රතිලෝම ප්‍රොක්සියක් ලෙස ක්‍රියා කරන Nginx විසින් අපට පහසුවෙන් Nodejs යෙදුමක් සැකසිය හැකිය.
පහත වින්‍යාසය උපකල්පනය කරන්නේ NodeJS යෙදුම 127.0.0.1:8080,

  server{
     server_name domain.com sub.domain.com; # multiple domains

     location /{ 
      proxy_pass http://127.0.0.1:8080;  
      proxy_set_header Host $host;
      proxy_pass_request_headers on;  
     }

     location /static/{
       alias /absolute/path/to/static/files; # nginx will handle js/css
     }
   } 

ඉහත සැකසුම තුළ ඔබගේ Nodejs යෙදුම,

  • HTTP_HOSTප්‍රතිචාරය සැපයීම සඳහා ඔබට වසම් විශේෂිත තර්කනය යෙදිය හැකි ශීර්ෂයක් ලබා ගන්න . '
  • ඔබගේ යෙදුම කළමනාකරණය කළ යුත්තේ pm2 වැනි ක්‍රියාවලි කළමණාකරුවෙකු හෝ තත්වයන් හැසිරවීම / සොකට් හෝ සම්පත් නැවත භාවිතා කිරීම සඳහා අධීක්ෂක විසිනි.

  • සැකසුම වැනි නිෂ්පාදන දෝෂ ලබා ගැනීම සඳහා දෝෂයක් වාර්තා සේවාව වූ දමිල්වානි බ්රිතාන්යයෙන් හෝ rollbar

සටහන: ඔබට වසම් විශේෂිත ඉල්ලීම් මාර්ග භාර දීම සඳහා තර්කනය සැකසිය හැකිය, සී express ්‍රගාමී යෙදුම සඳහා මිඩ්ල්වෙයාර් එකක් සාදන්න


1
PM2 භාවිතා කිරීමට තවත් හේතුවක් නම්, ඔබට කවචයෙන් පිටවීමෙන් පසු ඔබේ යෙදුම 'සදහටම' ධාවනය කළ හැකි අතර, ඔබට කවදා හෝ ඔබේ සේවාදායකය නැවත ආරම්භ කිරීමට අවශ්‍ය නම් එය ස්වයංක්‍රීයව ආරම්භ කරන්න, බලන්න: pm2.keymetrics.io/docs/usage/startup
SeanQuinn781

4

ව්‍යාපෘති කළමණාකරුවෙකු මෙන් ක්‍රියා කරන ප්‍රතිලෝම ප්‍රොක්සි සේවාදායකයක් ලෙස Nginx හට ක්‍රියා කළ හැකිය. එය ඉල්ලීමක් ලැබුණු විට එය විශ්ලේෂණය කර ඉල්ලීම ඉහළට (ව්‍යාපෘති සාමාජිකයින්) වෙත යොමු කරයි. ඉල්ලීමක් වින්‍යාසගත කර ඇති ආකාරය මත පදනම්ව Nginx ක්‍රම දෙකක් හැසිරවිය හැකිය.

  • ඉල්ලීමට සේවය කරන්න
  • ඉල්ලීම වෙනත් සේවාදායකයකට යොමු කරන්න

    server{
     server_name mydomain.com sub.mydomain.com;
    
     location /{ 
      proxy_pass http://127.0.0.1:8000;  
      proxy_set_header Host $host;
      proxy_pass_request_headers on;  
     }
    
     location /static/{
       alias /my/static/files/path;
     }

    }

ඉල්ලීම සේවාදායකය කරන්න

මෙම වින්‍යාසය සමඟ, ඉල්ලීම් url වන mydomain.com/static/myjs.jsවිට එය myjs.jsගොනුව /my/static/files/pathෆෝල්ඩරයේ යවයි. ස්ථිතික ලිපිගොනු සේවය කිරීම සඳහා ඔබ nginx වින්‍යාස කරන විට, එය ඉල්ලීමම හසුරුවයි.

ඉල්ලීම වෙනත් සේවාදායකයකට යොමු කරන්න

ඉල්ලීම් url mydomain.com/dothisnginx වන විට ඉල්ලීම http://127.0.0.1:8000 වෙත යොමු කරයි. Localhost 8000 වරායේ ක්‍රියාත්මක වන සේවාවට ඉල්ලීම ලැබෙනු ඇති අතර ප්‍රතිචාරය nginx වෙත ලබා දෙන අතර nginx සේවාදායකයාට ප්‍රතිචාරය ලබා දෙයි.

ඔබ වරායේ node.js සේවාදායකය ක්‍රියාත්මක කරන විට 8000 nginx ඉල්ලීම node.js වෙත යොමු කරයි. Node.js තර්කනය ලියන්න සහ ඉල්ලීම හසුරුවන්න. ඔබේ නෝඩ්ජස් සේවාදායකය එන්ජින්එක්ස් සේවාදායකය පිටුපස ධාවනය වන්නේ එයයි.

ඔබට nodejs හැර වෙනත් සේවාවන් ක්‍රියාත්මක කිරීමට අවශ්‍ය නම් විවිධ වරායන් මත ජැන්ගෝ, ෆ්ලැස්ක්, පීඑච්පී වැනි වෙනත් සේවාවක් ධාවනය කර එය එන්ජින්එක්ස් හි වින්‍යාස කරන්න.


1

ඔබට එක් එක් ක්ෂුද්‍ර සේවා මාධ්‍යයන් කළමනාකරණය කර එය ක්‍රියාත්මක කිරීමට අවශ්‍ය නම් ඔබට pm2 භාවිතයෙන් nodejs ධාවනය කළ හැකිය. වරාය තුළ නෝඩ් ක්‍රියාත්මක වන අතර එම වරාය nginx හි වින්‍යාස කරන්න (/etc/nginx/sites-enabled/domain.com)

server{
    listen 80;
    server_name domain.com www.domain.com;

  location / {
     return 403;
  }
    location /url {
        proxy_pass http://localhost:51967/info;
    }
}

Ping භාවිතා කරමින් localhost ක්‍රියාත්මක වේද නැද්ද යන්න පරීක්ෂා කරන්න.

සහ

Create one single Node.js server which handles all Node.js requests. This reads the requested files and evals their contents. So the files are interpreted on each request, but the server logic is much simpler.

මෙය හොඳම වන අතර ඔබ කී පරිදි පහසුය


1

Nginx සහ Nodejs සමඟ ඇති හොඳම හා සරල සැකසුම නම් ප්‍රොක්සි_ප්‍රොටොකෝල් සක්‍රීය කර ඇති HTTP සහ TCP පැටවුම් ශේෂයක් ලෙස Nginx භාවිතා කිරීමයි. මෙම සන්දර්භය තුළ, එන්ජීඑක්ස් වෙත එන ඉල්ලීම් නෝඩ්ජ් වෙත ප්‍රොක්සි කිරීමට හැකි වන අතර, එස්එස්එල් සම්බන්ධතා බැකෙන්ඩ් එන්ජින්එක්ස් සේවාදායකයට (ය) අවසන් කිරීමට මිස ප්‍රොක්සි සේවාදායකයටම නොවේ. (SSL-PassThrough)

මගේ මතය අනුව, සියලුම වෙබ් යෙදුම් ආරක්ෂිත පරිසරයන් භාවිතා කරන බැවින් (හෝ විය යුතුය) SSL නොවන උදාහරණ ලබා දීමෙන් පලක් නැත.

/Etc/nginx/nginx.conf හි ප්‍රොක්සි සේවාදායකය සඳහා උදාහරණ වින්‍යාසය

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
  upstream webserver-http {
    server 192.168.1.4; #use a host port instead if using docker
    server 192.168.1.5; #use a host port instead if using docker
  }
  upstream nodejs-http {
    server 192.168.1.4:8080; #nodejs listening port
    server 192.168.1.5:8080; #nodejs listening port
  }
  server {
    server_name example.com;
    location / {
      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-Forwarded-Host $server_name;
      proxy_set_header Connection "";
      add_header       X-Upstream $upstream_addr;
      proxy_redirect     off;
      proxy_connect_timeout  300;
      proxy_http_version 1.1;
      proxy_buffers 16 16k;
      proxy_buffer_size 16k;
      proxy_cache_background_update on;
      proxy_pass http://webserver-http$request_uri;
    }
  }
  server {
    server_name node.example.com;
    location / {
      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-Forwarded-Host $server_name;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
      add_header       X-Upstream $upstream_addr;
      proxy_redirect     off;
      proxy_connect_timeout  300;
      proxy_http_version 1.1;
      proxy_buffers 16 16k;
      proxy_buffer_size 16k;
      proxy_cache_background_update on;
      proxy_pass http://nodejs-http$request_uri;
    }
  }
}
stream {
  upstream webserver-https {
    server 192.168.1.4:443; #use a host port instead if using docker
    server 192.168.1.5:443; #use a host port instead if using docker
  }

  server {
    proxy_protocol on;
    tcp_nodelay on;
    listen 443;
    proxy_pass webserver-https;
  }
  log_format proxy 'Protocol: $protocol - $status $bytes_sent $bytes_received $session_time';
  access_log  /var/log/nginx/access.log proxy;
  error_log /var/log/nginx/error.log debug;
}

දැන්, අපි බැකෙන්ඩ් වෙබ් සේවාදායකය හසුරුවමු. /etc/nginx/nginx.conf :

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
load_module /etc/nginx/modules/ngx_http_geoip2_module.so; # GeoIP2
events {
    worker_connections  1024;
}
http {
    variables_hash_bucket_size 64;
    variables_hash_max_size 2048;
    server_tokens off;
    sendfile    on;
    tcp_nopush  on;
    tcp_nodelay on;
    autoindex off;
    keepalive_timeout  30;
    types_hash_bucket_size 256;
    client_max_body_size 100m;
    server_names_hash_bucket_size 256;
    include         mime.types;
    default_type    application/octet-stream;
    index  index.php index.html index.htm;
    # GeoIP2
    log_format  main    'Proxy Protocol Address: [$proxy_protocol_addr] '
                        '"$request" $remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';

    # GeoIP2
    log_format  main_geo    'Original Client Address: [$realip_remote_addr]- Proxy Protocol Address: [$proxy_protocol_addr] '
                            'Proxy Protocol Server Address:$proxy_protocol_server_addr - '
                            '"$request" $remote_addr - $remote_user [$time_local] "$request" '
                            '$status $body_bytes_sent "$http_referer" '
                            '$geoip2_data_country_iso $geoip2_data_country_name';

    access_log  /var/log/nginx/access.log  main_geo; # GeoIP2
#===================== GEOIP2 =====================#
    geoip2 /usr/share/geoip/GeoLite2-Country.mmdb {
        $geoip2_metadata_country_build  metadata build_epoch;
        $geoip2_data_country_geonameid  country geoname_id;
        $geoip2_data_country_iso        country iso_code;
        $geoip2_data_country_name       country names en;
        $geoip2_data_country_is_eu      country is_in_european_union;
    }
    #geoip2 /usr/share/geoip/GeoLite2-City.mmdb {
    #   $geoip2_data_city_name city names en;
    #   $geoip2_data_city_geonameid city geoname_id;
    #   $geoip2_data_continent_code continent code;
    #   $geoip2_data_continent_geonameid continent geoname_id;
    #   $geoip2_data_continent_name continent names en;
    #   $geoip2_data_location_accuracyradius location accuracy_radius;
    #   $geoip2_data_location_latitude location latitude;
    #   $geoip2_data_location_longitude location longitude;
    #   $geoip2_data_location_metrocode location metro_code;
    #   $geoip2_data_location_timezone location time_zone;
    #   $geoip2_data_postal_code postal code;
    #   $geoip2_data_rcountry_geonameid registered_country geoname_id;
    #   $geoip2_data_rcountry_iso registered_country iso_code;
    #   $geoip2_data_rcountry_name registered_country names en;
    #   $geoip2_data_rcountry_is_eu registered_country is_in_european_union;
    #   $geoip2_data_region_geonameid subdivisions 0 geoname_id;
    #   $geoip2_data_region_iso subdivisions 0 iso_code;
    #   $geoip2_data_region_name subdivisions 0 names en;
   #}

#=================Basic Compression=================#
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/css text/xml text/plain application/javascript image/jpeg image/png image/gif image/x-icon image/svg+xml image/webp application/font-woff application/json application/vnd.ms-fontobject application/vnd.ms-powerpoint;
    gzip_static on;

    include /etc/nginx/sites-enabled/example.com-https.conf;
}

දැන්, අපි මෙම SSL සමඟ අථත්ය ධාරකය වින්‍යාස කරමු සහ /etc/nginx/sites-available/example.com-https.conf හි ප්‍රොක්සි_ප්‍රොටොකෝල් සක්‍රීය වින්‍යාසය :

server {
    real_ip_header proxy_protocol;
    set_real_ip_from 192.168.1.1; #proxy server ip address
    #set_real_ip_from proxy; #proxy container hostname if you are using docker
    server_name 192.168.1.4; #Your current server ip address. It will redirect to the domain name.
    listen 80;
    listen 443 ssl http2;
    listen [::]:80;
    listen [::]:443 ssl http2;
    ssl_certificate     /etc/nginx/certs/example.com.crt;
    ssl_certificate_key /etc/nginx/certs/example.com.key;
    ssl_dhparam /etc/nginx/ssl/dhparam.pem;
    return 301 https://example.com$request_uri;
}
server {
    real_ip_header proxy_protocol;
    set_real_ip_from 192.168.1.1; #proxy server ip address
    #set_real_ip_from proxy; #proxy container hostname if you are using docker
    server_name  example.com;
    listen       *:80;
    return 301   https://example.com$request_uri;
}
server {
    real_ip_header proxy_protocol;
    set_real_ip_from 192.168.1.1; #proxy server ip address
    #set_real_ip_from proxy; #proxy container hostname if you are using docker
    server_name www.example.com;
    listen 80;
    listen 443 http2;
    listen [::]:80;
    listen [::]:443 ssl http2 ;
    ssl_certificate     /etc/nginx/certs/example.com.crt;
    ssl_certificate_key /etc/nginx/certs/example.com.key;
    ssl_dhparam /etc/nginx/ssl/dhparam.pem;
    return 301 https://example.com$request_uri;
}
server {
    real_ip_header proxy_protocol;
    set_real_ip_from 192.168.1.1; #proxy server ip address
    #set_real_ip_from proxy; #proxy container hostname if you are using docker
    server_name example.com;
    listen 443 proxy_protocol ssl http2;
    listen [::]:443 proxy_protocol ssl http2;
    root /var/www/html;
    charset UTF-8;
    add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header Referrer-Policy no-referrer;
    ssl_prefer_server_ciphers on;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
    ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;
    keepalive_timeout   70;
    ssl_buffer_size 1400;
    ssl_dhparam /etc/nginx/ssl/dhparam.pem;
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=86400;
    resolver_timeout 10;
    ssl_certificate     /etc/nginx/certs/example.com.crt;
    ssl_certificate_key /etc/nginx/certs/example.com.key;
    ssl_trusted_certificate /etc/nginx/certs/example.com.crt;
location ~* \.(jpg|jpe?g|gif|png|ico|cur|gz|svgz|mp4|ogg|ogv|webm|htc|css|js|otf|eot|svg|ttf|woff|woff2)(\?ver=[0-9.]+)?$ {
    expires modified 1M;
    add_header Access-Control-Allow-Origin '*';
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    access_log off;
    }
    location ~ /.well-known { #For issuing LetsEncrypt Certificates
        allow all;
    }
location / {
    index index.php;
    try_files $uri $uri/ /index.php?$args;
    }
error_page  404    /404.php;

location ~ \.php$ {
    try_files       $uri =404;
    fastcgi_index   index.php;
    fastcgi_pass    unix:/tmp/php7-fpm.sock;
    #fastcgi_pass    php-container-hostname:9000; (if using docker)
    fastcgi_pass_request_headers on;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_intercept_errors on;
    fastcgi_ignore_client_abort off;
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_request_buffering on;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    include fastcgi_params;
}
location = /robots.txt {
    access_log off;
    log_not_found off;
    }
location ~ /\. {
    deny  all;
    access_log off;
    log_not_found off;
    }
}

අවසාන වශයෙන්, nodejs වෙබ් සේවාදායක 2 ක නියැදියක් : පළමු සේවාදායකය:

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello From Nodejs\n');
}).listen(8080, "192.168.1.4");
console.log('Server running at http://192.168.1.4:8080/');

දෙවන සේවාදායකය:

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello From Nodejs\n');
}).listen(8080, "192.168.1.5");
console.log('Server running at http://192.168.1.5:8080/');

දැන් සෑම දෙයක්ම පරිපූර්ණව වැඩ කළ යුතු අතර බර සමබර විය යුතුය.

නැවත මම ගැන ලියූ අතර ඒ Docker දී TCP බර balancer ලෙස විකිපීඩියා පිහිටුවීමට කොහොමද . ඔබ ඩෝකර් භාවිතා කරන්නේ දැයි පරීක්ෂා කරන්න.

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.