Nginx+ssl+node.js Run Environment Configuration tutorial _nginx

Source: Internet
Author: User
Tags nginx server ssl certificate

Nginx is a high-performance HTTP server, but also an efficient reverse proxy server. Unlike traditional servers, Nginx is an event-based asynchronous architecture with little memory footprint but good performance. If your Web application is based on Node.js, it is recommended that you consider using Nginx as a reverse proxy, because Nginx can provide static file services very efficiently. The main content of this article is to configure Nginx and SSL under different operating systems and to build a node.js operating environment.

Install Nginx

Assuming you have installed the Node.js on the server, let's install Nginx.

Installing Nginx on a Mac system

Use the Chown command to gain access to the/usr/local folder with the following command code:

Copy Code code as follows:

sudo chown-r ' username here '/usr/local

The next two lines of command will allow you to install the Nginx:
Copy Code code as follows:

Brew Link Pcre
Brew Install Nginx

Nginx after installation, you can use the following command to start the Nginx
Copy Code code as follows:

sudo nginx

Finally you can see the Nginx configuration file under the directory/usr/local/etc/nginx/nginx.conf.

Install Nginx on Ubuntu

If you use Ubuntu, you can install Nginx in the following ways:

Copy Code code as follows:

sudo apt-get update
sudo apt-get install Nginx

Nginx can be started automatically when the installation is complete.

To install Nginx under Windows

Windows version of Nginx can be downloaded here, then unzip the installation package into the specified directory, run the following code under the CMD command tool:

Copy Code code as follows:

Unzip Nginx-1.3.13.zip
CD nginx-1.3.13
Start Nginx

Similarly, the start Nginx command will allow Nginx to start complete.

Now that we have finished installing Ngnix, we are ready to configure the server.

Configuring Node.js Servers

First, let's create a simple node.js server where you can download the Express version of Node.js. After downloading the source code, unzip it to the DemoApp folder, and enter the following command to have the server start on Port 3000.

Copy Code code as follows:

NPM Install
Node bin/www
1
<H2 id= "Configuring-nginx" >configuring nginx<p>now let ' s open up Nginx config file. As I am on a Mac, I can just use nano to start editing the file:</p>
1
Nano/usr/local/etc/nginx/nginx.conf

If you want, you can also go directly to the folder with your favorite text editor to open the config file, you can find the server configuration node in the file, similar to the following code:
Copy Code code as follows:

server {
Listen 8080;
server_name localhost;
....
More config goes
}

Next we will do some of the server nodes to meet their own requirements of the configuration, we need to the site's static file request to Nginx processing, other file requests to the Node.js back-end server. We will replace the above server configuration node with the following code:
Copy Code code as follows:

server {
Listen 8080;
server_name localhost;
Location/{
Proxy_pass http://localhost:3000;
Proxy_http_version 1.1;
Proxy_set_header Upgrade $http _upgrade;
Proxy_set_header Connection ' upgrade ';
Proxy_set_header Host $host;
Proxy_cache_bypass $http _upgrade;
}
Location/public {
root/usr/local/var/www;
}
}

So we let Nginx in http://localhost:8080. On the monitor. The location/configuration node will tell Nginx to receive arbitrary requests, and location Configure the node we use Proxy_pass to specify the Node.js backend server for http://localhost:3000.

Now we're going to use another configuration node location/public to tell Nginx to process the static file request, where the location node has the root directory set to/usr/local/var/www. Of course, you can also change to other catalogs. In this way, when there is such a request http://localhost:8080/public/somepath/file.html, Nginx will be from/usr/local/var/www/public/somepath/ file.html read static files.

After modifying the configuration file, you need to restart the Nginx with the following code:

Mac:

Copy Code code as follows:

sudo nginx-s stop && sudo nginx

Ubuntu:
Copy Code code as follows:

sudo service nginx restart

Or
Copy Code code as follows:

Sudo/etc/init.d/nginx restart

Windows:
Copy Code code as follows:

Nginx-s Reload

Next we'll use Nginx instead of Node.js to provide CSS style files, node.js templates with the files below/public/stylesheets/style.css. Create a file named Style.css under the/usr/local/var/www/public/stylesheets folder, and Nginx will parse it correctly. For example, you can write the following code in a CSS file:
Copy Code code as follows:

Body {
padding:50px;
font:14px "Lucida Grande", Helvetica, Arial, Sans-serif;
}
A
Color: #00B7FF;
}

Then you can log in to http://localhost:8080 down and look at your Web apps, and you'll find that even though it's access to the Nginx server, the requests are processed via a real node.js backend server, and only CSS static files are handled by Nginx.

Creating SSL

Web products are long, you will find that you need to create SSL to protect sensitive information. Perhaps your first reaction will be the thought of requesting a Web site certificate from a certification authority, but you can also create a signing certificate. The only problem is that the browser prompts a warning that the certificate is not trustworthy, but as a local test, that's enough. Here is a tutorial on how to create your own signed SSL certificate, you can see.

When you have your own certificate, you can install SSL on the Nginx, the modified configuration file, the following code:

Copy Code code as follows:

server {
Listen 8080;
Listen 443 SSL;
server_name localhost;
Ssl_certificate/etc/nginx/ssl/server.crt
Ssl_certificate_key/etc/nginx/ssl/server.key
Location/{
Proxy_pass http://localhost:3000;
Proxy_http_version 1.1;
Proxy_set_header Upgrade $http _upgrade;
Proxy_set_header Connection ' upgrade ';
Proxy_set_header Host $host;
Proxy_cache_bypass $http _upgrade;
}
Location/public {
root/usr/local/var/www;
}
}

It's done! This will allow SSL to begin working when you visit the https://localhost:8080. Here we default to save the certificate in the/ETC/NGINX/SSL/SERVER.CRT directory. Keep the private key in the/etc/nginx/ssl/server.key directory, or you can change the saved directory.

Summarize

In this article, we learned how to use Nginx to reverse proxy for Node.js and configure SSL. Static file requests are processed by Nginx on the front end, which can greatly reduce stress for node.js back-end servers. Try it yourself and have any questions you can communicate in the comments.

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.