Learn how to setup an Nginx proxy server that sits in front of a Node.js app. You can use a proxy to control the flow of frontend requests hitting your app, as well as to achieve better performance and scalability. We'll create a sample Node.js app and configure a default Nginx configuration file to proxy web requests.

Create a node app:

// Load the http module to create an http server.
var http = require('http'); // Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Welcome to Node.js!\n");
}); // Listen on port 3000, IP defaults to 127.0.0.1
server.listen(3000); // Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:3000/");

The app running on port 3000.

Run the node app:

node index.js

See the output:

curl localhost:3000

We will see:

Welcome to Node.js!

Now, start the nginx:

service nginx start

Varifiy nginx started:

curl localhost // by default nginx running on port 80

We will see the default nginx html.

Modify the nginx config in '/etc/nginx/sites/enabled/default':

server {
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:3000;
}
}

Restart nginx:

service nginx reload

now do:

curl localhsot

Previously we saw the default nginx page, now we should see our node app's output. That means our nginx poxy works, it successfully redirect localhost:80 to localhost:3000.

[Node] Setup an Nginx Proxy for a Node.js App的更多相关文章

  1. Linux服务部署Yapi项目(安装Node Mongdb Git Nginx等)

    Linux服务部署Yapi 一,介绍与需求 1,我的安装环境:CentOS7+Node10.13.0+MongoDB4.0.10. 2,首先安装wget,用于下载node等其他工具 yum insta ...

  2. linux 下安装node 并使用nginx做域名绑定

    #1 ,home目录下 下载nodejs安装包,解压 并修改文件夹名称 wget https://nodejs.org/dist/v8.11.4/node-v8.11.4-linux-x64.tar. ...

  3. Oracle Online Patching报错"This is not a RAC setup. OPatch cannot determine the local node name"

    Oracle Online Patching报错"This is not a RAC setup. OPatch cannot determine the local node name&q ...

  4. 29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node OJ: https://oj.leetcode.com/problems/populating-next-rig ...

  5. nginx proxy超时报错 upstream timed out (110: Connec...

    环境介绍 服务器:centos6.4服务:nginx proxy 问题描述: 然后查找  /opt/usr/nginx/1.4.0/logs  错误 error.log日志提示如下 2015/01/0 ...

  6. Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...

  7. node源码详解(四) —— js代码如何调用C++的函数

    本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/nodesource4 本博客同步在https://cnodejs.o ...

  8. How to Setup a Private Proxy Server on EC2 in Under 10 Minutes

    How to Setup a Private Proxy Server on EC2 in Under 10 Minutes I’ve been slacking a bit with regular ...

  9. Nginx+proxy实现简单的负载均衡

    环境说明:操作系统centos6.6 64位web操纵系统是:web1=192.168.10.10(LAMP) web2=192.168.10.11(LNMP),这里只是测试nginx实现负载均衡效果 ...

随机推荐

  1. Android实现QQ分享及注意事项

    一.获取APPID和帮助文档 在前面我介绍了关于Android中微信分享的文章< Android实现微信分享及注意事项>这一篇文章来看看关于QQ分享. 可以参看新手引导和接入说明:http ...

  2. 分享js寄生组合模式继承

    function person(){ this.name = 'taobao'; this.showMess = function(){ return this.name; } } person.pr ...

  3. C/C++(数据结构栈的实现)

    栈的实现 特点FILO(先进后出) 假设栈的空间为8 top == 0 不能出栈,已到栈底 top == 8 不能入栈,已到栈顶 top始终指向一个待插入的位置 push操作,1.写入数据,2.top ...

  4. 程序中为什么会使用while(0)

    https://blog.csdn.net/u012062760/article/details/46446207 关于while(0)实际上是用来宏定义的,这样的宏定义可以避免调用的时候出错. 如下 ...

  5. nodejs基础部分(一)

    前言 业余时间充实自我,入手学习了解一下传说中纯事件驱动/非阻塞的js架构 --nodejs 好记性不如烂笔头,本系列随笔用于整理记录学习nodejs过程中的心得 目录 nodejs简介 nodejs ...

  6. iOS报错 -pie can only be used when targeting iOS 4.2 or later

    近期,使用师兄的project时.突然报错之前没发现这个错误.信息例如以下: ld: -pie can only be used when targeting iOS 4.2 or later cla ...

  7. GBX的Graph(最短路)

    Problem B: Graph Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 1  Solved: 1 [cid=1000&pid=1&am ...

  8. java三元表达式编程规范问题

    package day01; public class Program { public static void main(String[] args) {        // TODO Auto-g ...

  9. Day6下午题解1

    预计分数:100+?+30=130+? 实际分数:100+25+30=155 T1 https://www.luogu.org/problem/show?pid=T15920 DP裸题,用dp[i][ ...

  10. MariaDB 安装 (YUM)

    在CentOS 7.0安装MariaDB的数据库,在这里记录下安装过程,以便以后查看. 1. 安装MariaDB 安装命令 yum -y install mariadb mariadb-server ...