安装Nginx

1
yum install nginx

配置反向代理

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
$ vi /etc/nginx/nginx.conf
...
http {
...

upstream myCluster {
server 127.0.0.1:8080;
}

server {
server_name www.example.com;
location / {
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_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://myCluster;
proxy_redirect default;
proxy_connect_timeout 60s;
proxy_read_timeout 120s;
proxy_send_timeout 120s;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_max_temp_file_size 128m;
}
...
}
...
}

NodeJS获取客户真实IP

1
2
3
function getClientIp (req) {
return req.headers['x-forwarded-for'] || req.connection.remoteAddress;
}

目录代理

  • web1作为前端端服务器,访问地址是 http://192.168.1.1 ,要将 http://192.168.1.1/bbs 的请求交给web2。在web1的网站根目录下并没有bbs目录

  • web1配置location 方式一

    1
    2
    3
    location /bbs/ {
    proxy_pass http://192.168.1.2/; #有“/”
    }
  • web2作为后端web服务器,访问地址是 http://192.168.1.2

  • web1配置location 方式二 (location中不加 “/”)

    1
    2
    3
    location /bbs/ {
    proxy_pass http://192.168.1.2; #无“/”
    }