歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> nginx結合node.js安裝使用

nginx結合node.js安裝使用

日期:2017/3/1 17:20:50   编辑:關於Linux
最近想給團隊做個Web站點,本來想用Apache的,後來想到現在好像流行nginx,那就學習一下nginx吧。

http://nginx.org/ 點擊download下載windows版本,我下的是1.1.14版,壓縮包只有1.08M,真是小啊。我把它解壓到 D:\Developer\nginx-1.1.14下,雙擊nginx.exe,彈出一個黑屏就消失了。查看進程

\

啟動兩個進程

浏覽器輸入http://localhost/

\

OK可以使用

不過打開關閉太麻煩了,還要手動關進程,有沒有像apache那樣的托盤就好了

google一下,還真有,軟件叫nginxtray 官網 http://nginxtray.codeplex.com/ 是codeplex的開源項目,我下的1.0版

我下載後解壓到D:\Developer\NginxTray 1.0

雙擊執行 NginxTray.exe,托盤多個圖標

\

提供打開、關閉和重啟功能,不過使用前需要配置一下nginx的位置

點擊settings

\

Nginx Directory 是nginx的解壓目錄,我的是D:\Developer\nginx-1.1.14,注意:替換時要小心,有一部分被Nginx Directory標簽遮住了,我就搞了半天

現在啟動關閉好方便了。

D:\Developer\nginx-1.1.14\conf\nginx.conf 是主要配置文件有點像httpd.conf,結合node.js使用時需要修改該文件,在server {下添加

location /node {
proxy_pass http://127.0.0.1:1337;
}

node,js例子example.js文件如下:




var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/'); 在命令行執行node example.js


重啟nginx

在地址欄輸入http://www.2cto.com /node

\

OK結合使用成功

摘自 dellheng的專欄

Copyright © Linux教程網 All Rights Reserved