Nginx
下载
http://nginx.org/en/download.html
安装
Windows
解压即可
Linux
在线安装:
# 安装nginx
yum install -y nginx
# 启动nginx
systemctl start nginx
# 停止nginx
systemctl stop nginx
# 重启nginx
systemctl restart nginx
# 重载nginx
systemctl reload nginx
# 查看nginx服务端口
netstat -anpl | grep 'nginx'
yum方式安装的默认地址和配置的默认地址
# yum方式安装后默认配置文件的路径
/etc/nginx/nginx.conf
# nginx网站默认存放目录
/usr/share/nginx/html
# 网站默认主页路径
/usr/share/nginx/html/index.html
如果提示 没有可用软件包 nginx
添加CentOS 7 Nginx yum资源库
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
离线安装
在安装nginx前首先要确认系统中安装了gcc、pcre-devel、zlib-devel、openssl-devel。
例如gcc:
yum list installed | grep "gcc"
安装命令:
yum -y install gcc gcc-c++ pcre-devel zlib-devel openssl openssl-devel
安装Nginx
##进入nginx目录
cd nginx-1.9.9
## 配置
./configure --prefix=/usr/local/nginx --with-http_ssl_module
# make
make && make install
Nginx常用命令常用命令
# 查看版本:
nginx -v
# 查看nginx安装目录:
ps -ef | grep nginx
# 检查配置文件:
nginx -t
# 启动:
systemctl start nginx.service
# 停止:
systemctl stop nginx.service
nginx -s stop
nginx -s quit
#重启:
systemctl restart nginx.service
# 设置开机自启动:
systemctl enable nginx.service
# 停止开机自启动:
systemctl disable nginx.service
# 查看当前状态:
systemctl status nginx.service
# 查看所有已启动的服务:
systemctl list-units --type=service
常用配置
# 配置允许运行nginx服务器的用户和用户组
#user nobody;
## 配置允许nginx进程生成的worker process数量,一般与CPU核数相等
worker_processes 4;
## worker进程最大打开文件数,解决"too many open files"问题
#worker_rlimit_nofile 100000
## 运行错误日志存放路径
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
## pid文件存放路径和名称
pid logs/nginx.pid;
events {
## 事件驱动模型,select、poll、epoll等
use epoll;
## 最大连接数,直接决定并发处理能力
worker_connections 1024;
}
## http配置
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压缩,超过1024Kb才压缩
gzip on;
gzip_min_length 1024;
server {
listen 8000;
server_name localhost;
location / {
root /mnt;
}
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
## 在这里导入子配置文件!!!
include /usr/local/nginx/conf/conf.d/*.conf;
}
https://www.digitalocean.com/community/tools/nginx?global.app.lang=zhCN
http {
#设定mime类型,类型由mime.type文件定义
include /etc/nginx/mime.types;
default_type application/octet-stream;
#设定日志格式
access_log /var/log/nginx/access.log;
#设定负载均衡的服务器列表
upstream load_balance_server {
#weigth参数表示权值,权值越高被分配到的几率越大
server 192.168.1.11:80 weight=5;
server 192.168.1.12:80 weight=1;
server 192.168.1.13:80 weight=6;
}
#HTTP服务器
server {
#侦听80端口
listen 80;
#定义使用www.xx.com访问
server_name www.helloworld.com;
#对所有请求进行负载均衡请求
location / {
root /root; #定义服务器的默认网站根目录位置
index index.html index.htm; #定义首页索引文件的名称
proxy_pass http://load_balance_server ;#请求转向load_balance_server 定义的服务器列表
#以下是一些反向代理的配置(可选择性配置)
#proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
}
}
}
结构
- src目录:存放nginx的源代码
- docs目录:存放nginx的帮助文档
- html目录:默认网站文件
- contrib目录:存放其他机构或组织贡献的文档资料
- conf目录:存放nginx服务器的配置文件
- auto目录:存放大量脚本文件,和configure脚本程序相关
- configure目录:Nginx自动安装脚本,用于检查环境,生成编译代码的makefile相关文件
常用命令
nginx -s stop #快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。
nginx -s quit #平稳关闭Nginx,保存相关信息,有安排的结束web服务。
nginx -s reload #因改变了Nginx相关配置,需要重新加载配置而重载。
nginx -s reopen #重新打开日志文件。
nginx -c filename #为 Nginx 指定一个配置文件,来代替缺省的。
nginx -t #不运行,仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
nginx -v #显示 nginx 的版本。
nginx -V #显示 nginx 的版本,编译器版本和配置参数。
负载均衡
一般轮询负载均衡
upstream web_server {
server 192.168.124.1;
server 192.168.124.2;
}
server {
listen 80;
server_name location;
location / {
proxy_pass http://web_server;
}
}
加权负载均衡
upstream web_server {
server 192.168.124.1 weight=1;
server 192.168.124.2 weight=3;
}
server {
listen 80;
server_name location;
location / {
proxy_pass http://web_server;
}
}
常用状态参数
| 配置方式 | 说明 |
|---|---|
| max_fails | 允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream指令定义的错误 |
| fail_timeout | 在经历了max_fails次失败后,暂停服务的时间。且在实际应用中max_fails一般与fail _timeout一起使用 |
| backup | 预留的备份机器 |
| down | 表示当前的 server暂时不参与负载均衡 |
upstream web_server {
server 192.168.124.1 weight=1 max_fails=1 fail_timeout=2;
server 192.168.124.2 weight=3 max_fails=2 fail_timeout=2;
server 192.168.124.3 backup;
}
server {
listen 80;
server_name location;
location / {
proxy_pass http://web_server;
}
}
设置为 backup 的服务器,只有当其他所有的非 backup 机器出现故障或者忙碌的情况下,才会请求 backup服务器,因此这台服务器的压力最小。
IP_HASH负载均衡
upstream web_server {
ip_hash;
server 192.168.124.1;
server 192.168.124.2;
server 192.168.124.3 down;
}
server {
listen 80;
server_name location;
location / {
proxy_pass http://web_server;
}
}
对于一个暂时性宕机的服务器,可以使用down参数标识出来,这样在负载均衡时,就会忽略该服务器的分配。
问题
root和alias的区别?
alias在映射路径时不会追加location匹配的部分,而root会追加location匹配到的部分
Nginx Keepalived高可用方案
Keepalived内置了VRRP(Virtual Router Redundancy Protocol,虚拟路由冗余协议)功能,VRRP用于解决静态路由出现的单点故障问题,它通过IP多播的方式通信,当发现主路由故障时,通过选举策略将备用路由更换为主路由,从而继续提供服务。
Keepalived利用VRRP实现了将提供对外访问的IP地址(Virtual IP)自动在主服务器(Master)和备用服务器(Backup)之间切换,正常情况下Master使用Virtual IP提供对外访问,当Master故障时,其他正在监控Master的Backup会通过优先级(priority)机制竞争接管Virtual IP继续对外提供服务,其他落选的Backup会继续监控当前使用的Virtual IP服务器。
Keepalived
下载地址:Keepalived for Linux
安装
yum -y install keepalived
# 查看安装是否成功
rpm -qa keepalived
配置文件位于
cat /etc/keepalived/keepalived.conf
配置内容详解
global_defs {
notification_email { # keepalived服务宕机异常出现的时候,发送通知邮件 可以是多个
acassen@firewall.loc # 收件人邮箱1
failover@firewall.loc # 收件人邮箱2
sysadmin@firewall.loc # 收件人邮箱3
}
notification_email_from Alexandre.Cassen@firewall.loc #邮件发件人
smtp_ server 192.168.32.128 #主服务器的ip地址。邮件服务器地址
smtp_connect_timeout 30 # 超时时间
router_id LVS_DEVEL # 机器标识 局域网内唯一即可。 LVS_DEVEL这字段在/etc/hosts文件中看;通过它访问到主机
}
vrrp_script chk_http_ port {
script "/usr/local/src/nginx_check.sh" #检测脚本存放的路径
interval 2 # 检测脚本执行的间隔,即检测脚本每隔2s会自动执行一次
weight 2 #权重,如果这个脚本检测为真,服务器权重+2
}
vrrp_instance VI_1 { #配置一个虚拟路由,名为VI_1m
state MASTER # 指定keepalived的角色,MASTER为主,BACKUP为备。备份服务器上需将MASTER 改为BACKUP
interface ens33 # 通信端口 通过ip addr可以看到,根据自己的机器配置
virtual_router_id 51 # vrrp实例id keepalived集群的实例id必须一致,即主、备机的virtual_router_id必须相同
priority 100 #优先级,数值越大,获取处理请求的优先级越高。主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1 #心跳间隔,默认为1s。keepalived多机器集群 通过心跳检测当前服务器是否还正常工作,如果发送心跳没反应,备份服务器就会立刻接管;
authentication { # 服务器之间通信密码
auth type PASS #设置验证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信
auth pass 1111
}
virtual_ipaddress { # 自定义虚拟IP。自定义的虚拟ip得根据真实ip设置。比如真实ip是192.168.91.138,那么虚拟ip可以设置为192.168.91.139~255,前面三个数得一致
192.168.32.50 # 定义虚拟ip(VIP),可多设,每行一个
}
}