: 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.