Deploying Web Services using node and Nginx

Source: Internet
Author: User

In the previous ASP times our site is usually used by Windows IIS to set up, later in the PHP era, is generally to start PHP services, and then use the HTTP Server service to do forwarding (reverse proxy), but now use node how to deal with it?

As we all know, the direct access to a domain name is the 80 port that resolves IP (browser will hide 80 port by default), but each node process can only occupy one port, then when a server (refers to the machine that hosts these sites, it may be windows, Linux or Mac) on more than one site, the port is not enough to break? Then use HTTP server to proxy it ~

Use Nginx to do reverse proxy

There are a lot of HTTP servers here, take Nginx for example, who makes her useful?
First you can click here to see what is the reverse proxy, typically using node to listen to some ports, and then by domain name (see their own needs) for conversion, such as:

A.com => 9000
B.Com => 9001
C.com => 9002
...
Then to modify the Nginx configuration conf, recommend that you use a site A. conf file, through include to load, such as:

The nginx.conf file is like this:

confhttp {
# Other Configurations


# Load configuration files in all conf directories
Include conf/*.conf;
}
The Conf directory of each. conf file is a site, such as a.com Agent to Port 9000 configuration is probably:

conf# file is: conf/a.com.conf
server {
server_name a.com;
Listen 80;

Location/{
# 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_pass Http://127.0.0.1:9000$request_uri;
Proxy_redirect off;
}
}

Now enable your nginx and start your node program to use a.com access

Managing node processes with PM2

Nginx start, you can also listen to multiple domain names, you can run multiple node processes (services) to open n windows, and then node index.js start? We can use some mature service process managers, such as PM2, for example:

shell# a.com website
# because a.com program Index.js is listening to 9000 ports, we enable this service with the service name + port information to the command name, so you can use ' PM2 ls ' when very clear view
CD a.com && pm2 start index.js--name a.com-9000-e err.log-o out.log

# B.Com
CD B.Com && pm2 start index.js--name a.com-9001-e err.log-o out.log
Use PM2 ls to view the services that have started, such as:

┌─────────────────┬────┬──────┬───────┬────────┬─────────┬────────┬──────────────┬──────────┐
│app name│id│mode│pid│status│restart│uptime│memory│watching│
├─────────────────┼────┼──────┼───────┼────────┼─────────┼────────┼──────────────┼──────────┤
│a.com-9000│0│fork│5001│online│0│27d│200.082 mb│disabled│
│b.com-9001│1│fork│13484│online│9│31h│64.535 mb│disabled│
└─────────────────┴────┴──────┴───────┴────────┴─────────┴────────┴──────────────┴──────────┘
Of course, PM2 far more than these, she can also protect the process, load, multi-core and other functions, specific please see PM2 description

Using Nginx to host static resources

Since we have used the Nginx proxy node layer's Web services, we can prioritize the use of Nginx to host static resources, such as:

# Web site root directory
/wwwroot/a.com/

# Web site Static resources
/wwwroot/a.com/static/

# node Program

/wwwroot/a.com/lib/
Then you can set the root directory of Nginx to/wwwroot/a.com/static/, and then configure the URL path that is not found to rewrite to node, so that the efficient use of nginx, presumably configuration such as:

Confserver {
Listen 80;
server_name a.com;

root/wwwroot/a.com/static/;

# If the file does not exist, rewrite to a fake file
if (!-f $request _filename) {
Rewrite (. *)/server.js;
}

# Agent with a fake file since the above does not exist rewrite
Location =/server.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_pass Http://127.0.0.1:node Port $request_uri;
Proxy_redirect off;
}
}

After this configuration, when visiting the Web site if you are accessing the existing static files, then Nginx will return directly, if the file does not exist, will be agents to the Node,node layer can handle 404 of the situation ~

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.