Use helloworld with nginx and node. js

Source: Internet
Author: User

Nginx is the best reverse proxy server.

Node. JS is... Okay, don't introduce it. Click here.

This section describes how to use nginx and node. js together.

Write helloworld. js first

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 /');

Then use the node helloworld. js command to enable it. In this way, the node. js program running on the local machine can be started up and occupies port 8000. You can modify it yourself.

Next, we will write a server in nginx vhost. conf.

Server {listen80; SERVER_NAME indexing. Me www. indexing. Me; Location/{proxy_pass http: // 127.0.0.1: 8000 ;}}

Set the website domain name, set the port to 80, and finally set proxy_pass to http: // 127.0.0.1: 8000, and pass all requests from the processing ing. Me: 80 to the nodejs program.

Restart nginx

Access the domain name and you will see helloworld.

Although node. js itself can be used as a server, for example, set port 80 in welcome. js.

However, when a host runs multiple websites and other websites use other servers, port 80 can be processed by proxy on other ports.

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.