nginx and node.js配合使用 helloworld

來源:互聯網
上載者:User

nginx是最好的反向 Proxy伺服器。

node.js是。。。 好吧 ,不介紹了,猛擊這裡

現在小介紹下怎麼用nginx和node.js配合使用。

先寫個helloworld.js

var http = require('http');http.createServer(function (request, response) {    response.writeHead(200, {'Content-Type': 'text/plain'});  response.end('hello world\n');}).listen(8000);console.log('Server running at http://127.0.0.1:8000/');

然後用node helloworld.js指令開啟,這樣跑在本地的機子的nodejs的程式就算開起來了,佔用的是8000連接埠,可自己修改。

接著,我們在nginx的vhost.conf裡面寫一個server

server {    listen80;    server_name taqing.me www.taqing.me;    location / {proxy_pass http://127.0.0.1:8000;    }}

將網站網域名稱設定好,然後連接埠設定為80,最後proxy_pass設定為http://127.0.0.1:8000,將所有從taqing.me:80的請求傳遞到nodejs程式去。

重啟nginx

訪問網域名稱,就可以了看到helloworld了。

雖然node.js本身就可以做伺服器是沒錯啦,比如welcome.js裡面設定為80連接埠就可以了。

但是一個機子跑多個網站,其他網站又是用別的伺服器,在80連接埠已經被佔用的情況下,是可以用代理到別的連接埠來處理的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.