"Turn" NodeJS on Nginx: Handling static pages with Nginx reverse proxy

Source: Internet
Author: User
Tags nginx server nginx reverse proxy

Recently Ourjs backstage has migrated from pure node. js to Nginx+nodejs up, feel the performance has improved a lot, special share with you.

Nginx ("Engine X") is a high-performance HTTP and reverse proxy server, also a IMAP/POP3/SMTP proxy server. Nginx was developed by Igor Sysoev for the second rambler.ru site of Russian traffic, and the first public version 0.1.0 was released on October 4, 2004. It publishes the source code in the form of a BSD license, which is known for its stability, rich feature set, sample configuration files, and consumption of low system resources.

Although node. JS performs well, it really isn't his specialty to handle static transactions, such as gzip encoding, static files, HTTP caching, SSL processing, load balancing and reverse proxies, and multi-site proxies, all of which can be done via nginx to reduce the load on node. js and save you with Nginx's powerful cache Website to increase the load speed of the website.

Although node. JS also has some proxy modules such as Http-proxy can implement a server to set up multiple Web sites (each domain map to different nodejs process ports), but this basic work, in fact, should be given to Ngnix to complete.

Below we can look at a multi-site proxy example, Suppose you have a node. js process that is listening on port 8080, and you want to access the connection from domaina.com to a Web site that is serviced by node. JS and maps the connection from domainb.com into another static file service, you can use the following ngix.confg (for 1.44), the configuration is relatively simple, the general writing program should be able to understand, after you enter http://192.168.0.101, http://localhost will see different results.

#user  nobody;worker_processes  2;error_log  logs/error.log;events {    worker_connections  1024;} HTTP {    include       mime.types;    Default_type  Application/octet-stream;    Sendfile on        ;    Keepalive_timeout  ;    gzip on    ;    Gzip_min_length 1k;    Gzip_buffers    4 8k;    Gzip_http_version   1.1;    Gzip_types  text/plain application/x-javascript text/css application/xml;    Upstream Node_app {        server 127.0.0.1:8080;    }    server {        listen       ;        server_name  localhost;        Location/{            proxy_pass http://node_app;        }    }    # static Server    server {        listen       ;        server_name  192.168.0.101;        Location/{            root   D:\GitHub\areu\web;            Index  home.html;}}}    

Here is a more complex example, because the version is relatively stale and is for reference Using Nginx to Avoid NodeJS Load

To explain briefly, the role of each part

Indicates the port on which your site is running, because it supports HTTP/HTTPS so there are two ports:

HTTP {    ...    Upstream Silly_face_society_upstream {      server 127.0.0.1:61337;      Server 127.0.0.1:61338;      keepalive;    }    ...}

Static file blocker, will be images/js/img/css ... The address at the beginning is mapped to the site directory and served directly by Ngnix:

HTTP {    ...    server {        ...        Location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt| Favicon.ico) {          root/usr/local/silly_face_society/node/public;          Access_log off;          Expires Max;        }        ...    }}

Setting up the cache

HTTP {    ...    Proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;    proxy_temp_path/var/tmp;    ...}

Set up gzip compression

HTTP {    ...    gzip on;    Gzip_comp_level 6;    Gzip_vary on;    Gzip_min_length  ;    Gzip_proxied any;    Gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml Application/xml+rss Text/javascript;    Gzip_buffers 8k;    ...}

Finally, the non-static files are given to the NODEJS process to respond:

HTTP {    ...    server {        ...        Location/{          proxy_redirect off;          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   Connection "";          Proxy_http_version 1.1;          Proxy_pass         http://silly_face_society_upstream;        }        ...    }}



Share several nginx debug commands: Debian Linux

Install Nginx, use Apt-get can

Apt-get Install Nginx


Test Nginx configuration file


Restarting the Nginx server

/etc/init.d/nginx restart


Set a script to boot

sudo chmod 755/etc/init.d/foobarsudo update-rc.d foobar defaults     #开机时启动sudo update-rc.d-f foobar Remove #开机时不启动

Find/-name ' node '


You can use this script (Windows 2003 test is valid), sometimes you can't kill the Nginx under Windows.

taskkill/f/im nginx.exe > nul


Update
Ourjs has open Source: Https://github.com/newghost/ourjsOriginal Address: http://ourjs.com/detail/nodejs-on-nginx-%E4%BD%BF%E7%94%A8nginx%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86% E5%A4%84%E7%90%86%E9%9D%99%E6%80%81%E9%A1%B5%E9%9D%A2

"Turn" NodeJS on Nginx: Handling static pages with Nginx reverse proxy

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.