linux上安装nginx比较简单:
前提是需要有gcc或者g++
1.yum需要的依赖
yum -y install openssl openssl-devel
2.解压pcre库、zlib库
tar -xvzf pcre-8.38.tar.gz tar -xvzf zlib-1.2.8.tar.gz
3.解压nginx并配置
tar -xvzf nginx-1.10.3.tar.gz
cd nginx-1.10.3
#nginx进程脚本启动目录
mkdir -p /xjh/server/nginx/sbin
#相关配置目录
mkdir -p /xjh/server/nginx/conf
#pid存放目录
mkdir -p /xjh/server/nginx/sysconf
#相关日志存放目录
mkdir -p /xjh/server/nginx/logs
./configure --sbin-path=/xjh/server/nginx/sbin/nginx --conf-path=/xjh/server/nginx/conf/nginx.conf --pid-path=/xjh/server/nginx/sysconf/nginx.pid --with-http_ssl_module --http-log-path=/xjh/server/nginx/logs/http-logs.log --error-log-path=/xjh/server/nginx/logs/error-logs.log --lock-path=/xjh/server/nginx/sysconf/nginx.lock --with-pcre=/xjh/server/pcre-8.39 --with-zlib=/xjh/server/zlib-1.2.8
4.configure配置没有异常
make -j 3 开3个线程执行编译
make执行完
make install
5.配置nginx.conf
若现在有三个主机上部署了tomcat服务或者其他的提供了服务的应用程序,都占用了各自主机的8090端口,现在配置nginx负载均衡,使得请求通过nginx负载转发到另外的三台主机上处理http请求
在http{}区域内配置
upstream http_service { server 192.168.1.102:8090 weight=1; server 192.168.1.103:8090 weight=2; server 192.168.1.104:8090 weight=3; }
#weight 是权重值越大,该主机上承受的请求就越多
在server{}中配置
location /httpserver { proxy_pass http://http_service; proxy_redirect default; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
最后通过http访问的服务的url可以为 curl -d "k=v" http://192.168.1.101/httpserver