1、下载Nginx
http://nginx.org/en/download.html
选择Stable version
2、windows环境安装及配置nginx
2.1 下载安装及配置
下载后解压到比如E盘
修改配置文件
E:\software\nginx-1.16.1\conf\nginx.conf
#user nobody; worker_processes 1;
#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;
#pid logs/nginx.pid;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
sendfile on; keepalive_timeout 65;
#gzip on;
#配置系统服务器Ip及端口 upstream a.com{ server 172.17.195.37:8091; server 172.17.195.37:8092; } server { listen 8090; server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / { root html; index index.html index.htm;
#配置代理 proxy_pass http://a.com; proxy_redirect default; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
}
server 172.17.195.37:8091; server 172.17.195.37:8092;
就是需要被代理的服务器,假设在这两台服务器部署了站点study
http://172.17.195.37:8091/study
http://172.17.195.37:8092/study
2.2 启动和关闭nginx
启动:
C:\Users\figo>e:
E:\>cd E:\software\nginx-1.16.1
E:\software\nginx-1.16.1>nginx
关闭:
打开cmd执行
taskkill /im nginx.exe /f
2.3 测试
浏览器打开http://localhost:8090/study 或者http://172.17.195.37:8090/study (本机ip172.17.195.37)
就会轮询访问
http://172.17.195.37:8091/study
http://172.17.195.37:8092/study
因为负载策略默认是轮询
3、linux环境配置安装及配置nginx
将压缩包上传到/usr/local 依然是直接命令: tar -zxvf nginx-1.16.1.tar.gz cd nginx-1.16.1 配置 其实在 nginx-1.16.1版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。 1.使用默认配置
./configure 2.自定义配置(不推荐)
./configure \ --prefix=/usr/local/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --pid-path=/usr/local/nginx/conf/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi 注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
编译安装 make make install
查看nginx在哪 whereis nginx /usr/local/nginx
配置同windows环境
启动 [root@localhost ~]# /usr/local/nginx/sbin/nginx 停止/重启 [root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop(quit、reload) 命令帮助 [root@localhost ~]# /usr/local/nginx/sbin/nginx -h 验证配置文件 [root@localhost ~]# /usr/local/nginx/sbin/nginx -t 配置文件 [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
4、负载均衡策略
4.1轮询
这种是默认的策略,把每个请求按顺序逐一分配到不同的server,如果server挂掉,能自动剔除。
upstream a.com{
server 127.0.0.1:4501; #真实服务器A
server 127.0.0.1:4502; #真实服务器B
}
4.2最少连接
把请求分配到连接数最少的server
upstream a.com{
least_conn;
server 127.0.0.1:4501; #真实服务器A
server 127.0.0.1:4502; #真实服务器B
}
4.3 权重
使用weight来指定server访问比率,weight默认是1。以下配置会是server2访问的比例是server1的两倍。
upstream a.com{
server 127.0.0.1:4501 weight=1; #真实服务器A
server 127.0.0.1:4502 weight=2; #真实服务器B
}
4.4 ip_hash
每个请求会按照访问ip的hash值分配,这样同一客户端连续的Web请求都会被分发到同一server进行处理,可以解决session的问题。如果server挂掉,能自动剔除。
upstream a.com{
ip_hash;
server 127.0.0.1:4501; #真实服务器A
server 127.0.0.1:4502; #真实服务器B
}