Nginx下载: https://pan.baidu.com/s/1BzdzdKDr179FNOVaifWAgw
Pcre下载:https://pan.baidu.com/s/1aVbo8f-87XceLeWou-Q_lw
1)下载Nginx和Pcre上传到服务器/usr/local/src目录下
2)安装脚本:
cd /usr/local/src unzip nginx.zip; rm -rf nginx.zip; unzip pcre-8.40.zip cd pcre-8.40 ./configure --prefix=/usr/local/pcre make make install cd ../ tar -xvzf nginx-1.13.5.tar.gz cd nginx-1.13.5 ./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.40 make make install ls
安装完毕,启动./nginx
注意:--with-pcre=/usr/local/src/pcre-8.40 是源码目录。
安装完nginx后,我们预先配置下tomcat服务代理:
vi /usr/local/nginx/config/nginx.conf ,将内容替换为以下内容:
#user nobody; worker_processes 4; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 10240; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; client_max_body_size 100M; client_body_buffer_size 1024k; include loadbalancing.conf; }
:wq! 保存并退出。然后在同级目录下新建loadbalancing.conf
vi loadbalancing.conf,复制以下内容:
#负载均衡配置 map $zone $loadbalancing { MasterSp server1; UserQuery server1; CpQuery server1; adimage server1; pincheService server2; } upstream server1 { server 127.0.0.1:8081; server 127.0.0.1:8082; #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 ip_hash; #按后端服务器的响应时间来分配请求,响应时间短的优先分配。 #fair; } proxy_cache_path /home/ncache levels=1:2 keys_zone=ncache:20m max_size=50g inactive=30d; server { listen 80; server_name test.com; charset utf-8; proxy_ignore_client_abort on; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location = /favicon.ico { root html; } #websocket访问配置 location ~ /ws{ proxy_pass http://127.0.0.1:8083; # WebScoket Support proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #带命令字地址访问 location ~ / { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_set_header X-Frame-Options SAMEORIGIN; proxy_connect_timeout 1800; proxy_read_timeout 1800; proxy_send_timeout 1800; if ( $request_uri ~ ^/(\w*) ) { set $zone $1; proxy_pass http://$loadbalancing; } } location = / { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_connect_timeout 1800; proxy_read_timeout 1800; proxy_send_timeout 1800; set $zone $request_uri; proxy_pass http://$loadbalancing; } #静态页面 location ^~ /test/ { root /home/project/web; #test静态资源文件夹放在此目录下 } }注意修改 upstream server1 节点下的平台访问ip和端口
然后重启ngnix /usr/local/nginx/bin/./nginx -s reload