UP | HOME

Nginx反向代理NodeJS

安装Nginx

$ yum install nginx

配置反向代理

$ 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

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

目录代理

location /bbs/ {
    proxy_pass http://192.168.1.2/;     #有“/”
}


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

Date: 2018-12-15 Sat 00:06

Author: shixiongfei

Created: 2020-09-05 Sat 14:40

Emacs 27.1 (Org mode 9.3)