Nginx实现反向代理 Node.js

公司有项目前端是用node.js进行服务器渲染,然后再返回给浏览器,进而解决单页面的SEO问题。项目部署的时候,使用Nginx反向代理Node.js。具体的步骤如下:

(Nginx、Node.js的安装和基本配置直接跳过)

首先我们要在nginx.cnf文件中的http节点打开下面的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
http {
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 /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;

# 打开这一行的配置
include /etc/nginx/conf.d/*.conf;
}

然后每个域名的配置文件就放到这个目录/etc/nginx/conf.d/下,文件后缀以conf结束。

  1. 第一种方式,这种简单:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
server {
listen 80 ;
server_name localhost;
root /xxx/xxx/hxxydexx/;

#set $my_server_name $scheme://$server_name;

#if ( $my_server_name != https://$server_name ) {
# rewrite ^ https://$server_name$request_uri? permanent;
#}

error_log /var/log/nginx/hyde_error.log error;
access_log /var/log/nginx/hyde_accss.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_http_version 1.1;
proxy_set_header Connection "";

# 不需要考虑到负载的,就无需配置upstream节点。
proxy_pass http://127.0.0.1:3000;
}

error_page 404 /404.html;
location = /xxx/xxx/40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /xxx/xxx/50x.html {
}
}

2.第二种方式,考虑到负载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
upstream node {
server 127.0.0.1:3000;
}
server {
listen 80 ;
server_name localhost;
root /xxx/xxx/hxxydexx/;

#set $my_server_name $scheme://$server_name;

#if ( $my_server_name != https://$server_name ) {
# rewrite ^ https://$server_name$request_uri? permanent;
#}

error_log /var/log/nginx/hyde_error.log error;
access_log /var/log/nginx/hyde_accss.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_http_version 1.1;
proxy_set_header Connection "";

# 配置upstream节点
proxy_pass http://node;
}

error_page 404 /404.html;
location = /xxx/xxx/40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /xxx/xxx/50x.html {
}
}

然后重启或者重新载入nginx的配置文件即可。命令如下:

1
2
3
4
5
6
7
8
#检查nginx配置文件中语法是否正确
nginx -t

#重启nginx
service nginx restart

#重载配置文件
nginx -s reload

注意问题:
上面可能会出现下面的问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:884:11)
at Server._listen2 (net.js:1022:14)
at listen (net.js:1044:10)
at Server.listen (net.js:1110:5)
at Object.<anonymous> (folderName/app.js:33:24)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)

这个其实是Node.js服务多开端口被占用导致的报错,出现这种问题,可以使用Node.js项目管理工具pm2,或者使用netstat -anop进行查看端口被那个进程占用,然后杀掉重启服务!

附上Nginx的负载均衡策略:

  • 轮询(默认)
    每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

    1
    2
    3
    4
    upstream backserver { 
    server 192.168.0.14;
    server 192.168.0.15;
    }
  • 指定权重
    指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

    1
    2
    3
    4
    upstream backserver { 
    server 192.168.0.14 weight=10;
    server 192.168.0.15 weight=10;
    }
  • IP绑定 ip_hash
    每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    upstream backserver { 
    ip_hash;
    server 192.168.0.14:88;
    server 192.168.0.15:80;
    }
    ```

    - fair(第三方)
    按后端服务器的响应时间来分配请求,响应时间短的优先分配。
    ```bash
    upstream backserver {
    server 192.168.0.14:88;
    server 192.168.0.15:80;
    fair;
    }
    ```

    - url_hash(第三方)
    按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
    ```bash
    upstream backserver {

    server squid1:3128;
    server squid2:3128;

    hash $request_uri;
    hash_method crc32;
    }
  • 作者: Sam
  • 发布时间: 2018-08-07 23:25:53
  • 最后更新: 2019-12-09 23:03:26
  • 文章链接: https://ydstudios.gitee.io/post/45183e9d.html
  • 版权声明: 本网所有文章除特别声明外, 禁止未经授权转载,违者依法追究相关法律责任!