当前位置: 首页 > news >正文

阿里云ubuntu14.04 Nginx反向代理Nodejs

Install Nginx

0.通过ssh命令行登陆阿里云服务器

ssh root@your_ip_address

1.下载源码包

 wget http://nginx.org/download/nginx-1.9.10.tar.gz

2.进入下载路径

$ ./configure
$ make
$ make install

其中configure文件是Nginx软件的自动脚本程序。运行configure自动脚本会完成两项工作:

一是检查环境,根据环境检查结果生成C代码;
二是生成编译代码需要的Makefile文件。

Start Nginx

安装后程序在usr/local下面,执行

$ ./sbin/nginx

查看是否启动成功

可以使用Linux自带的ps工具

ps -ef | grep nginx
root     14358     1  0 11:20 ?        00:00:00 nginx: master process ./sbin/nginx
nobody   14359 14358  0 11:20 ?        00:00:00 nginx: worker process
nobody   14360 14358  0 11:20 ?        00:00:00 nginx: worker process
nobody   14361 14358  0 11:20 ?        00:00:00 nginx: worker process
root     14433 14278  0 12:32 pts/1    00:00:00 grep --color=auto nginx

显示启动成功,包括一个主线程和三个worker线程。

配置nginx.conf

下面是整个nginx.conf配置文件

#user  nobody;
worker_processes  3;
.
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream nodejs__upstream {
        server 127.0.0.1:3000;
        eepalive 64;
    }
    server {
        listen       80;
        server_name  jianqunzhang.com www.jianqunzhang.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
               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://nodejs__upstream;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

其中主要增加修改如下配置

upstream nodejs__upstream {
    server 127.0.0.1:3000;
    keepalive 64;
}
server {
    listen       80;
    server_name  jianqunzhang.com www.jianqunzhang.com;
    
    #charset koi8-r;
    
    #access_log  logs/host.access.log  main;
    
    location / {
            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://nodejs__upstream;
}

其中proxy_set_header指令可以更改Nginx服务器接收到的客户端请求的请求头信息,然后将新的请求头发送给被代理的服务器。

在服务器启动nodejs,注意监听的端口与配置的nginx.conf要一致。本配置直接监听3000端口。
如果proxy_pass http://nodejs__upstream;在不设置http://时,要注意

upstream nodejs__upstream {
    server 127.0.0.1:3000;
    keepalive 64;
}

的server 要加上http://,如

upstream nodejs__upstream {
    server http://127.0.0.1:3000;
    keepalive 64;
}

启动nodejs后,访问所设置的域名即可。

Install PM2

PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

npm i pm2 -g

start app

pm2 start bin/www

到目前为止,整个项目算是跑起来了。记录一下同时也希望对小伙伴们有所帮助。

相关文章:

  • vSphere中CPU的设置选择及运行流程
  • iOS 系统状态栏 隐藏 或 显示
  • 页面常见效果js实现
  • Java GBK中文乱码问题分析
  • 巧用位运算求解二进制中1的个数
  • Linux Netcat 命令——网络工具中的瑞士军刀
  • python学习笔记-Day14 -js/dom/jquery
  • C++构造函数详解及显式调用构造函数
  • web应用中log4j应用--log4j.xml同时输出多种日志
  • apache的动态和静态
  • Navicat 结构同步常规设置讲解
  • hbase参考官方文档搭建完全分布式环境遇到的问题
  • 【设计模式】GoF设计模式学习总结
  • 毛毛雨的博客乐园—内容简介
  • Android布局— — —表格布局
  • .pyc 想到的一些问题
  • Javascript 原型链
  • js作用域和this的理解
  • MobX
  • Netty+SpringBoot+FastDFS+Html5实现聊天App(六)
  • nodejs实现webservice问题总结
  • vue总结
  • Zepto.js源码学习之二
  • 关于for循环的简单归纳
  • 基于游标的分页接口实现
  • 解析带emoji和链接的聊天系统消息
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 前端设计模式
  • 前嗅ForeSpider采集配置界面介绍
  • 使用 Docker 部署 Spring Boot项目
  • 算法系列——算法入门之递归分而治之思想的实现
  • 微服务入门【系列视频课程】
  • 微信小程序:实现悬浮返回和分享按钮
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • #etcd#安装时出错
  • (02)vite环境变量配置
  • (06)Hive——正则表达式
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (强烈推荐)移动端音视频从零到上手(上)
  • (转)程序员疫苗:代码注入
  • ..回顾17,展望18
  • .Net Web窗口页属性
  • .net 设置默认首页
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地中转一个自定义的弱事件(可让任意 CLR 事件成为弱事件)
  • .net连接MySQL的方法
  • .Net下使用 Geb.Video.FFMPEG 操作视频文件
  • .net项目IIS、VS 附加进程调试
  • .Net中ListT 泛型转成DataTable、DataSet
  • .NET中的十进制浮点类型,徐汇区网站设计
  • .pyc文件是什么?
  • .w文件怎么转成html文件,使用pandoc进行Word与Markdown文件转化
  • @Documented注解的作用
  • @html.ActionLink的几种参数格式
  • @Transaction注解失效的几种场景(附有示例代码)