Use nodejs instead of nginx for reverse proxy

Source: Internet
Author: User
: This article mainly introduces how to use nodejs to replace nginx for reverse proxy. if you are interested in the PHP Tutorial, refer to it.
Var http =Require('Http'), httpProxy =Require('Http-proxy'); // create a proxy Proxy Server object var proxy = httpProxy. createProxyServer ({}); // capture abnormal proxy. on ('error', function (err, req, res) {res. writeHead (500, {'content-type': 'Text/plain '}); res. end ('something went wrong. and we are reporting a custom error message. ') ;}); // Create an HTTP port 80 server, that is, the common method for creating an HTTP server for Node. // Call proxy in each request. create your M server and just call 'proxy. web () 'to proxy // a web request to the target passed in the options // also you can use 'proxy. ws () 'to proxy a websockets request // var server =Require('Http '). createServer (function (req, res) {// You can define here your custom logic to handle the request // and then proxy the request. // var host = req. url; // host = url. parse (host); host = host. host; console. log ("host:" + req. headers. host); console. log ("client ip:" + (req. headers ['X-forwarded-for '] | req. connection. remoteAddress); proxy. web (req, res, {target: 'http: // localhost: 100', xfwd: 'true'}) ;}); console. log ("listening on port 80") server. listen (80 );

The above describes how to use nodejs to replace nginx for reverse proxy, including The require content, and hope to be helpful to friends who are interested in PHP tutorials.

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.