nginx配置node.js反向代理

发表于:2017-09-07

找到nginx配置文件

/etc/nginx/conf.d/default.conf
// 需要include, 否则配置文件一般在 /etc/nginx/nginx.conf
vim /etc/nginx/conf.d/default.conf
upstream blog {
    server localhost:9001;      // node.js启动端口
    keepalive 64;
}

server {
    listen 80;
    server_name www.xiejiahe.com xiejiahe.com;
    access_log /var/log/nginx/xiejiahe.log;
    location / {
            proxy_pass      http://blog;
        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 "";
    }

}

重启nginx,

service nginx restart

访问 xiejiahe.com, OK


nginx配置静态资源,因为node.js不适合处理静态资源

server {
      listen      80;
      server_name  localhost;
      location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
          root   /home/www/html/;
                    # expires 如果静态资源不经常更新可以设置长一点,这样可以减少服务器的压力。 下面是7天
          expires      7d;
      }
}
Node.js
服务器