ubuntu/mac 设置nginx反向代理3000端口
nginx的配置文件在 /etc/nginx/nginx.conf
怎么安装nginx 这里就不赘述了 可以brew安装
或者在conf.d中新建一个demo.conf
写入以下内容
#本地http-server开启的server,命名为node-server,监听3000和3001端口
upstream node-server{
# weight表示权重,数值越大,表示被分配到这个server的几率越大,这里我们让其相等。
server 127.0.0.1:3000 weight=1;
server 127.0.0.1:3001 weight=1;
}
# NGINX 虚拟主机,监听80端口
server {
listen 80;
server_name test.com;
access_log /var/log/nginx/node-server;
# Gzip Compression
gzip on;
gzip_comp_level 6;
gzip_vary on;
gzip_min_length 1000;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_buffers 16 8k;
# 反向代理 node-server
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_pass http://node-server;
proxy_redirect off;
}
}
重启nginx
sudo nginx -s reload