使用node和nginx|使用node和nginx部署网站服务

时间:2020-10-06  来源:nginx  阅读:

在之前的asp时代我们网站通常是使用windows的iis来架设,后来php时代,一般是启动php的服务,然后使用http server服务来做转发(反向代理),然而如今使用node时候如何处理呢?

众所周知,一个域名的直接访问是解析ip的80端口(浏览器会默认隐藏80端口),然而每一个node的进程又只能占用一个端口,那么当一个服务器(是指承载这些网站的机器,可能是windows、linux或者mac)上搭建的网站超过一个时,端口不够用乍破?那么就使用http server来代理吧~

使用nginx做反向代理

http server有很多,这里以nginx为例,谁让她好用呢
首先可以点这里看下 什么是反向代理 ,一般情况下是使用node监听某些端口,然后按域名(看自己需求)进行转换,比如:

a.com => 9000
b.com => 9001
c.com => 9002
...
那么来修改nginx的配置conf吧,推荐大家使用一个站点一个.conf文件,通过include来加载,比如:

nginx.conf文件是这样的:

confhttp {
    # 其他的配置


    # 加载所有conf目录下的配置文件 
    include conf/*.conf;
}
conf目录下的每一个.conf文件都是一个站点,比如a.com的代理到9000端口的配置大概是:

conf# 文件是:conf/a.com.conf
server {
    server_name a.com;
    listen 80;

    location / {
        # proxy_http_version 1.1;
        proxy_set_header Connection "";
        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_pass http://127.0.0.1:9000$request_uri;
        proxy_redirect off;
    }
}

现在启用你的nginx并启动你的node程序就可以使用a.com访问了

使用pm2管理node进程

nginx起动了,也可以监听多个域名了,可以运行多个node进程(服务)难道要开n个窗口,然后node index.js启动着吗?我们可以使用一些成熟的服务进程管理器,比如pm2,比如:

shell# a.com网站
# 由于a.com的程序index.js是监听了9000端口,我们启用这个服务的时候以服务名+端口信息来命令名称,这样可以使用`pm2 ls`的时候很清晰的查看
cd a.com && pm2 start index.js --name a.com-9000 -e err.log -o out.log

# b.com
cd b.com && pm2 start index.js --name a.com-9001 -e err.log -o out.log
使用pm2 ls查看已启动的服务,如:

┌─────────────────┬────┬──────┬───────┬────────┬─────────┬────────┬──────────────┬──────────┐
│ App name        │ id │ mode │ pid   │ status │ restart │ uptime │ memory       │ watching │
├─────────────────┼────┼──────┼───────┼────────┼─────────┼────────┼──────────────┼──────────┤
│ a.com-9000      │ 0  │ fork │ 5001  │ online │ 0       │ 27D    │ 200.082 MB   │ disabled │
│ b.com-9001      │ 1  │ fork │ 13484 │ online │ 9       │ 31h    │ 64.535 MB    │ disabled │
└─────────────────┴────┴──────┴───────┴────────┴─────────┴────────┴──────────────┴──────────┘
当然pm2远不止这些,她还可以守护进程、负载、多核等功能,具体请看 pm2说明

使用nginx托管静态资源

由于我们已经使用nginx代理node层的网站服务,那么我们可以优先使用nginx来托管静态资源,比如:

# 网站根目录
/wwwroot/a.com/

# 网站静态资源
/wwwroot/a.com/static/

# node程序

/wwwroot/a.com/lib/
那么可以把nginx的根目录设置到/wwwroot/a.com/static/,然后配置对未找到的url路径重写到node,这样可以高效的利用nginx,大概配置如:

confserver {
    listen       80;
    server_name     a.com;

    root /wwwroot/a.com/static/;

    # 如果文件不存在则重写到一个假文件上
    if ( !-f $request_filename ){
       rewrite (.*) /server.js;
    }

    # 代理一个假文件以来配合上面的不存在重写
    location = /server.js {
        #proxy_http_version 1.1;
        proxy_set_header Connection "";
        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_pass http://127.0.0.1:node端口$request_uri;
        proxy_redirect off;
    }
}

这样配置之后,当访问网站时如果访问的是已存在的静态文件,那么nginx会直接返回,如果文件不存在,则会代理到node,node层可以处理404的情况~

使用node和nginx|使用node和nginx部署网站服务

http://m.bbyears.com/caozuoxitong/102952.html

推荐访问:
相关阅读 猜你喜欢
本类排行 本类最新