Windows Server,nginx installation, configuration, implementation of Web projects running Nodejs backend, and error analysis and workarounds

Source: Internet
Author: User
Tags ftp to server centos server

If the system on the backend of Nodejs, source code on GitHub, Https://github.com/saucxs/nodeJSBlog, if you feel can, please please star and fork project

Project Address: http://www.mwcxs.top/

Next you will see the following sections:

First, install Nginx

Second, set Nginx as Windows service

Third, set Nginx as a Windows service

Iv. uploading project files to server-specific locations

V. deploy NODEJS on a Windows server using NSSM

Six, but the external network can not access

Before the Linux server, get through the win server, feel that the Linux server is the best choice for the server, choose Ubuntu System, or the most CentOS server is better than the win server, configuration is more simple, forcing grid higher, but there is a win server

When you can play a play, encounter pits, stepping on a step, fill in, you will reap a lot.

Go down to the chase.

First, install Nginx

Download the Windows Nginx (Http://nginx.org/download/nginx-1.12.2.zip) and unzip to the location you want to place (C:\nginx)

second, set Nginx as Windows service

Need to use the "Windows Service Wrapper" gadget, Project address: HTTPS://GITHUB.COM/KOHSUKE/WINSW

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

After downloading the tool, place it in the Nginx installation directory and rename it to Nginx-service.exe, creating the configuration file Nginx-service.xml (the name is the same as the tool name).

Here is the contents of the Nginx-service.xml file:

<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>

Run the following command in CMD to install the Windows service

C:\nginx\nginx-service.exe Install

In the service, the browser can be accessed normally, localhost.

Attention:

(1) Uninstall service

C:\nginx\nginx-service.exe Uninstall

(2) View system services

On the command line, enter

Services.msc

(3) Start the Service (command line)

net start Nginx

(4) Close the Service (command line)

net stop Nginx

Third, set Nginx as a Windows service

Configuration item nginx Core configuration file is 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 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; RootC:\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; }}   }

Remember not to use Notepad to edit the Conf file, it is recommended to use notepad++, because I use Notepad on the server to modify, resulting in the service unexpectedly stop, can not start the service. The error is as follows:

1, super invincible wrong to tolerate the operating habits-using Notepad to modify the configuration file caused by the 1067 error

For this reason modification. The 1067 error generated by the Conf suffix configuration file is devastating, as long as you save the configuration file with Notepad editor, no matter how you try to start it, editing it again with other software is not valid until you overwrite the original file.

Recommended source Code editing Tool Notepad6.3.3 Simplified Chinese Green Edition edit configuration file

Reference: http://www.upupw.net/bug/n61.html

The problem was solved. Modify the configuration items locally using notepad++, and then upload the FTP to the server.

Iv. Uploading project files to server-specific locations

On FTP to server specified place

V. Deploy NODEJS on a Windows server using NSSM

Under normal circumstances:

It is recommended to use PM2 on the server to manage the node. JS service to keep the system running properly.
Edit and save Pm2.json under the root directory

{"  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 ": {}}]}          

Note: Note: CWD is the path to the project on the server

Then normal is to start the service using

PM2 Start Pm2.json

On Linux, you can easily deploy NODEJS applications using forever or PM2. But under Windows it was a hassle, and PM2 clearly said that support for Linux & Macos,forever looks like a lot of problems in Windows:

Why use PM2 instead of using NPM start directly, as in the local environment.

Because the server is Windows, so it's cumbersome.

Let's start with a simpler NSSM today. NSSM will monitor the node service you installed, and if node is dead, NSSM will restart it automatically.

NSSM Installation and use

Currently the latest version is 2.23 (), unzip after download, according to your system select 32-bit and 64-bit version, directly in the nssm.exe directory Run command line, enter nssw install + your service name, for example:

NSSM Install Nodejsblog

In the Path Select your installed Node.exe, Startup directory Select your node app's directory, Argument enter your startup file, such as run on my desktop index.js (in the Startup directory directory execution node index.js ):

Click Install Service:

Then run:

NSSM Start Nodejsblog

Attention:

Executed in the directory where the Nssm.exe resides

(1) Uninstall (delete) the service

NSSM Remove Service Name

(2) Stop the service

A Go to Service list, manual restart, pause

One is the command line: NSSM Uninstall service Name

Attention:

NSSM start <servicename><servicename><servicename>

Service has started:

Can be viewed on the server's browser

Six, but the external network can not access

External network access, the system reported Nginx 504 Gateway time-out,windows Server extranet access to the Web is not a workaround

Only to find that the firewall of its own server does not have the Web 80 port open.

(1) New rule, select Port

(2) Select TCP, and a specific local port

(3) Select Allow connection

(4) Use this rule to select all

(5) Name and description

After this setting

The external network can access Nginx's configured Nodejs backend system. Welcome visit: Http://www.mwcxs.top

If you configure the Nodejs service on the win server, you can message the Exchange

Windows Server,nginx installation, configuration, implementation of Web projects running Nodejs backend, and error analysis and workarounds

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.