1.安装依赖的包
yum -y install pcre-devel openssl openssl-devel
pcre-devel (PCRE库是一组使用与Perl 5相同的语法和语义来实现正则表达式模式匹配的函数。)
openssl(OpenSSL是一个开源项目,为传输层安全(TLS)和安全套接字层(SSL)协议提供了强大的商业级和全功能工具包。 它也是通用加密库。)
openssl-devel(OpenSSL是支持加密技术的工具包。 openssl-devel包包含开发支持各种加密算法和协议的应用程序所需的包含文件。)
2.安装nginx
第一步:从http://nginx.org/download/上下载相应的版本
(或者wget http://nginx.org/download/nginx-1.12.0.tar.gz直接在Linux上用命令下载)
第二步:解压 tar -zxvf nginx-1.12.0.tar.gz 到指定喜欢的目录
第三步:设置一下配置信息 ./configure --prefix=/usr/local/nginx ,这样一些配置文件,日志,执行文件都放到/usr/local/nginx下
第四步:
make 编译 (make的过程是把各种语言写的源码文件,变成可执行文件和各种库文件)
make install 安装 (make install是把这些编译出来的可执行文件和库文件复制到合适的地方)
cd到/usr/local/nginx/ 目录 /sbin/nginx 就能够启动了
默认的访问端口是80,访问ip会出现Welcome to nginx的字样,安装成功了,但是没什么用。
conf目录底下的nginx.conf才是关键。
3.nginx.conf
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;
//重点配置
server {
//默认向外暴露的端口
listen 80;
//暴露的域名
server_name localhost;
//默认编码
charset utf-8;
#access_log logs/host.access.log main;
//配置静态资源
location / {
root /nh/appdir/html/fe_cms/ad_operator/;
index /index.html;
#error_page 404 /syserror/404.html; //地址“/”下错误跳转的页面
#access_log off; //可配置是否不记录日志
}
//配置接口,容器的访问地址
location /operator {
proxy_pass http://127.0.0.1:8080;
}
location ~ \.(gif|jpg|png)$ {
root /data/images;
}
//全局404错误页面
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#全局错误
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
//如果有多个配置 加载vhosts路径下的所有conf
include vhosts/*.conf;
}
配置完 nginx -s reload 重启,效果就有了。
nginx -s stop 快速的关闭
nginx -s quit 滑溜的关闭
nginx -s reload 重新加载配置文件
nginx -s reopen 重启log文件
mine.types
当web服务器收到静态的资源文件请求时,依据请求文件的后缀名在服务器的MIME配置文件中找到对应的MIME Type,
再根据MIME Type设置HTTP Response的Content-Type,然后浏览器根据Content-Type的值处理文件。
3.反向代理
upstream c-ads {
#ip_hash;
server 192.168.247.128:8080 weight=10 max_fails=3 fail_timeout=10;
server 192.168.247.129:8080 weight=10 max_fails=3 fail_timeout=10;
}
server {
listen 80;
server_name 192.168.247.129;
location / {
#include proxy.conf;
proxy_pass http://c-ads;
}
}
负载均衡的策略
nginx 的 upstream目前支持 4 种方式的分配 :
1)轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2)weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
2)ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
3)fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
4)url_hash(第三方)