windows server,nginx安裝,配置,運行nodeJS後端的web項目的實現,以及錯誤分析及解決方案

來源:互聯網
上載者:User

標籤:rewrite   暫停   小工具   art   hub   點擊   conf   site   product   

如果對nodeJS的後端的系統,原始碼在github上,https://github.com/saucxs/nodeJSBlog ,如果覺得可以,請請starfork項目

項目地址:http://www.mwcxs.top/

 

接下來你會看到以下部分:

一、安裝nginx

二、將Nginx設定為Windows服務

三、將Nginx設定為Windows服務

四、 將專案檔上傳到伺服器指定的地方

五、使用nssm在windows伺服器上部署nodeJS

六、但是外網訪問不了

 

 之前弄過linux伺服器,弄過win伺服器,感覺linux伺服器作為伺服器才是最佳的選擇,選擇ubuntu系統,或者centos最為伺服器也比win伺服器好,配置更簡單,逼格更高,但是有出現win伺服器

的時候也可以玩一玩,遇到坑,踩一踩,填一填,就會收穫很多。

下面進入到正題。

一、安裝nginx

下載windows版nginx (http://nginx.org/download/nginx-1.12.2.zip),之後解壓到需要放置的位置(C:\nginx)

 

 

二、將Nginx設定為Windows服務

需要藉助"Windows Service Wrapper"小工具,項目地址: https://github.com/kohsuke/winsw

 

: http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/1.18/winsw-1.18-bin.exe

 

下載該工具後,將其放在 Nginx安裝目錄下,並重新命名為nginx-service.exe,建立設定檔nginx-service.xml(名字要和工具名一樣)。

下面是nginx-service.xml檔案內容:

<service>  <id>nginx</id>  <name>Nginx Service</name>  <description>High Performance Nginx Service</description>  <logpath>D:\xampp\nginx\logs</logpath>  <log mode="roll-by-size">    <sizeThreshold>10240</sizeThreshold>    <keepFiles>8</keepFiles>  </log>  <executable>D:\xampp\nginx\nginx.exe</executable>  <startarguments>-p D:\xampp\nginx</startarguments>  <stopexecutable>D:\xampp\nginx\nginx.exe</stopexecutable>  <stoparguments>-p D:\xampp\nginx -s stop</stoparguments></service>

 

在cmd中運行如下命令安裝windows服務

C:\nginx\nginx-service.exe install

在服務中,瀏覽器可以正常訪問,localhost。

 

注意:

(1)卸載服務

C:\nginx\nginx-service.exe uninstall

(2)查看系統服務

在命令列中輸入

services.msc

(3)啟動服務(命令列)

net start nginx

(4)關閉服務(命令列)

net stop nginx

 

三、將Nginx設定為Windows服務

 配置項Nginx核心設定檔是nginx.conf

worker_processes  1;events {    worker_connections  1024;}http{   include       mime.types;     default_type  application/octet-stream;     sendfile on;     tcp_nopush on;     tcp_nodelay on;     keepalive_timeout 60;     client_header_buffer_size 4k;     open_file_cache max=51200 inactive=20s;     open_file_cache_valid 30s;     open_file_cache_min_uses 1;     types_hash_max_size 2048;     client_max_body_size 10m;     gzip_vary on;     gzip_proxied any;     gzip_comp_level 6;     gzip_buffers 16 8k;     gzip_http_version 1.1;     gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;     gzip on;     gzip_disable "msie6";server {    listen 80;    server_name www.mwcxs.top;    root C:\Work\liblog\www;    set $node_port 8361;    index index.js index.html index.htm;    if ( -f $request_filename/index.html ){        rewrite (.*) $1/index.html break;    }    if ( !-f $request_filename ){        rewrite (.*) /index.js;    }    location = /index.js {        proxy_http_version 1.1;        proxy_set_header Connection "";        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 Upgrade $http_upgrade;        proxy_set_header Connection "upgrade";        proxy_pass http://127.0.0.1:$node_port$request_uri;        proxy_redirect off;    }    location = /production.js {        deny all;    }    location = /testing.js {        deny all;    }    location ~ /static/ {        etag         on;        expires      max;    }}   }

 

記住一定不要用記事本編輯conf檔案,推薦使用notepad++,因為本人就是使用記事本在伺服器上修改,導致了服務進行意外停止,啟動不了服務。報錯如所示:

1、超級無敵大錯特錯不能容忍的操作習慣-用記事本修改設定檔引起的1067錯誤

由於這種原因修改.conf為尾碼的設定檔產生的1067錯誤是毀滅性的,只要你用記事本編輯儲存了設定檔,不管你怎麼嘗試都無法啟動,用其它軟體再次編輯儲存也無效,直到你拿原始檔案覆蓋為止。

推薦用原始碼編輯利器Notepad6.3.3簡體中文綠色版編輯設定檔

參考:http://www.upupw.net/bug/n61.html

問題才得以解決。先在本地使用notepad++修改配置項,再ftp上傳到伺服器。

 

 四、將專案檔上傳到伺服器指定的地方

通過ftp上到伺服器指定地方

 

五、使用nssm在windows伺服器上部署nodeJS

正常情況下:

在伺服器上推薦使用 pm2 來管理 Node.js 服務,來保證系統正常運行。
編輯並儲存根目錄下的pm2.json 

{  "apps": [{    "name": "liblog",    "script": "www/production.js",    "cwd": "C:/Work/liblog",    "max_memory_restart": "1G",    "autorestart": true,    "node_args": [],    "args": [],    "instances": "max",    "exec_mode": "cluster",    "env": {    }  }]}

注意:注意:cwd為項目在伺服器上的路徑

然後正常是啟動服務使用

pm2 start pm2.json

在Linux上,可以輕鬆的使用forever或者pm2來部署nodejs應用。但是在windows下就麻煩了,pm2明確的說支援Linux & MacOS,forever在windows下貌似問題多多:

為什麼要使用pm2,而不是像本地環境一樣,直接使用npm start。

 

因為伺服器是windows的,所以很麻煩的。

今天先說下比較簡單的nssm。nssm會監控你安裝的node服務,如果node掛了,nssm會自動重啟它。

 

nssm安裝使用

目前最新版的是2.23(),下載之後解壓,根據你的系統選擇32位和64位的版本,直接在nssm.exe 所在目錄運行命令列,輸入nssw install +你的服務名,例如:

nssm install NodeJSBlog

 

Path 中選擇你安裝的node.exe,Startup directory 選擇你的node應用的目錄,Argument 輸入你的開機檔案,例如在我案頭上運行index.js (在Startup directory目錄執行node index.js ):

 

 

 點擊Install Service:

 

之後運行:

nssm start NodeJSBlog

注意:

在nssm.exe所在的目錄執行

(1)卸載(刪除)該服務

nssm remove 服務名

(2)停止該服務

一種去服務列表中,手動重啟,暫停

一種是命令列:nssm uninstall 服務名

 

 

注意:

nssm start <servicename>nssm stop <servicename>nssm restart <servicename>

 

 服務已經啟動:

可以在伺服器的瀏覽器上查看

 

 六、但是外網訪問不了

 外網訪問,系統報nginx 504 Gateway Time-out,Windows伺服器外網無法訪問web的解決方案

才發現是自己伺服器的防火牆沒有開啟web的80 連接埠。

 (1)建立規則,選擇連接埠

 

(2)選擇tcp,以及特定的本地連接埠

(3)選擇允許串連

 

(4)使用該規則選擇全部

(5)名稱以及描述

這樣設定之後

 

外網就可以訪問nginx的配置的nodeJS後端系統。歡迎訪問:http://www.mwcxs.top

 

如果在win伺服器上配置nodeJS服務,可以留言交流

windows server,nginx安裝,配置,運行nodeJS後端的web項目的實現,以及錯誤分析及解決方案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.