Nginx
1、安装
-
解压缩:
tar zxvf [文件名] -
安装依赖
#yum install -y gcc pcre pcre-devel zlib zlib-devel yum install -y gcc yum install -y pcre pcre-devel yum install -y zlib zlib-devel -
#检查依赖 参数--prefix可选,用来设置安装路径,安装路劲默认是/usr/local/nginx ./configure #./configure --prefix=/usr/local/nginx
4. ```shell
make #编译
make install #安装
#完成后,在 /usr/local/下出现nginx目录
2、原始命令
#启动
./nginx
#查看版本号
./nginx -v
#快速关闭nginx
./nginx -s stop
#优雅关闭nginx:待完成所有已经接收到连接请求后再关闭
./nginx -s quit
#重新加载配置文件
./nginx -s reload
#测试配置文件是否正确
./nginx -t
#查看Nginx进程状态
ps -ef | grep nginx
3、加入service服务
-
创建文件:
vim /usr/lib/systemd/system/nginx.service -
编辑文件:注意路劲
/usr/local/nginx[Unit] Description=nginx - high performance web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target -
执行
systemctl daemon-reload重新加载配置文件,只有重新加载后,配置才能生效
4、系统服务命令
# 设置开机启动
systemctl enable nginx.service
# 启动nginx
systemctl start nginx.service
#service nginx start
# 重新启动nginx
systemctl restart nginx.service
#service nginx restart
# 停止ngixn
systemctl stop nginx.service
#service nginx stop
5、配置文件
#默认为1,表示开启一个业务进程,业务进程数量一般和CPU核心数一致,可以发挥最大性能
worker_processes 1;
events {
#单个业务进程可以接受的连接数
worker_connections 1024;
}
http {
#引入其他的配置文件,可以用多个include引入多个文件
include mime.types;
#如果mime类型没有匹配上,默认使用二进制流的方式传输
default_type application/octet-stream;
#使用Linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝
sendfile on;
keepalive_timeout 65;
#虚拟主机,可以存在多个,以server_name和listen共同确定唯一值
server {
listen 80;
#域名或者主机名,可以有多个,之间用空格隔开,且支持通配符例如 *.tanmujin.com
server_name localhost 127.0.0.1 www.tanmujin.com;
#访问静态资源,对于http://www.tanmujin.com/xxoo/index.html 来说,uri是/xxoo/index.html
location / {
#uri匹配成功后,work进程去哪一个目录下找所需的资源文件(相对于nginx主目录,在这里就是/usr/local/nginx/html)
root html;
#默认页
index index.html index.htm;
#是否自动显示目录索引
autoindex on;
}
location ~*/(js|css|img) {
root html/static;
index index.html index.htm;
autoindex on;
}
#请求转发,例如http://www.tanmujin.com/openAPI/getXXX
location /openAPI {
#proxy_pass和root不能同时出现
#1、将请求转发给http://127.0.0.1:8081
proxy_pass http://127.0.0.1:8081
#2、将请求转发给负载均衡服务器组
#proxy_pass http://devServerGroup
}
#当服务器端发生500 502 503 504错误的时候,跳转到http://www.tanmujin.com/50x.html页面
error_page 500 502 503 504 /50x.html;
#匹配uri:/50x.html
location = /50x.html {
#到/html目录下寻找50x.html
root html;
}
}
#定义负载均衡服务器组,组名称为devServerGroup
upstream devServerGroup{
#1、轮询
server 192.168.0.1:8080;
server 192.168.0.2:8080;
server 192.168.0.3:8080;
#2、权重:weight
#server 192.168.0.1:8080 weight=8;
#server 192.168.0.2:8080 weight=1;
#server 192.168.0.3:8080 weight=1;
#3、ip_hash:根据IP的hash值分配服务器,能保证同一个客户端每次请求所到达的是同一台服务器
#4、根据服务器的响应时间:fail
}
}
server_name
#1、可以在同一server_name中配置多个域名(以空格隔开)
server_name aaa.tanmujin.com bbb.tanmujin.com;
#2、完整匹配
server_name www.tanmujin.com;
#3、通配符匹配:以下可以匹配aaa.tanmujin.com, bbb.tanmujin.com, ccc.tanmujin.com等等
server_name *.tanmujin.com;
#4、通配符结束匹配:以下可以匹配www.tanmujin.com, www.tanmujin.net, www.tanmujin.org等等
server_name www.tanmujin.*;
#5、正则匹配:“~”表示使用正则匹配,正则以“^”开头,“$”结尾
server_name ~^[0-9]+\.tanmujin\.com$;
location
匹配请求路径,语法:location [=|~|~*|^~] /uri/ {…}
| 匹配符 | 匹配规则 | 优先级 |
|---|---|---|
| = | 精确匹配 | 1 |
| ^~ | 以某个字符串开头 | 2 |
| ~ | 区分大小写的正则匹配 | 3 |
| ~* | 不区分大小写的正则匹配 | 3 |
| / | 通用匹配,任何请求都会匹配到 | 4 |
SSL证书
server{
listen 443 ssl;
server_name www.tanmujin.com;
ssl_certificate /data/cert/server.crt;
ssl_certificate_key /data/cert/server.key;
}
URLRewrite
rewrite是实现URL重写的关键指令,根据regex部分内容,重定向到replacement,结尾是flag标记
rewrite <regex> <replacement> <flag>
rewrite ^/([0-9]+).html$ /index.html?pageNum=$1 break;
#flag标记:
#last:本条规则匹配完成后,继续向下匹配新的URL规则
#break:本条规则匹配完成就终止,不再匹配任何规则
#redirect:返回302重定向,浏览器地址栏会显示跳转后的URL地址
#permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
防盗链
位置:http.server.location,表示该location开启盗链检测
valid_referers none | blocked | server_names
valid_referers none 192.168.10.1 www.tanmujin.com
#none:检测Referer头不存在的情况
#blocked:检测Referer头被防火墙或代理服务器删除或伪装的情况,这种情况该头的值不以“http://”或“https://”开头
#server_names:设置一个或多个域名或IP,检测Referer的值是否是这些中的一个
location ~*/(js|css|img) {
valid_referers 192.169.10.1;
if(!invalid_referer){
#返回403状态码(如果server有配置403状态码对应的页面,会跳转到该页面)
return 403;
#直接返回页面,nginx会根据该URI再次匹配对应的location,最后返回静态资源
#return /403.html;
#通过URLRewrite返回图片
#rewrite ^/ /img/no.png break;
}
root html/static;
index index.html index.htm;
autoindex off;
}
http请求转https
server{
listen 80;
server_name www.tanmujin.com;
return 301 https://$server_name$request_uri;
}
6、高可用
配置高可用的准备工作
-
准备两台服务器
-
在两台服务器安装Nginx
-
在两台服务器安装keepalived
#安装依赖 yum -y install opssl-devel yum -y install keepalived #启动 keepalived systemctl start keepalived.service #查看 keepalived 进程的状态 ps -ef | grep keepalived -
修改keepalived配置文件:
/etc/keepalived/keepalived.conf-
修改
global_defsglobal_defs{ ... router_id HOST_NAME #访问到主机 } #HOST_NAME在/etc/hosts文件中查看或新增一行: 127.0.0.1 自定义主机名 -
修改
vrrp_script chk_http_portvrrp_script chk_http_port{ script "/usr/local/src/nginx_check.sh" #脚本所在的位置 interval 2 #脚本执行的时间间隔,单位:秒 weight 2 #权重值,设置当前服务器的权重 } -
修改
vrrp_instance VI_1vrrp_instance VI_1{ state BACKUP #主服务器MASTER,备份服务器BACKUP interface ens33 #绑定的网卡 virtual_router_id 51 #主,备机的virtual_router_id必须相同 priority 90 #主,备机取不同的优先级,一般主机取100,备机小于100 advert_int 1 #每隔一定时间发送一次心跳,服务器是否可用,单位:秒 authentication{ auth_type PASS #权限校验方式:密码 auth_pass 1234 #权限密码:1234 } virtual_ipaddress{ 192.168.17.50 #定义keepalived虚拟IP,使用此IP来访问nginx集群 } }
-
-
监测脚本:
/usr/local/src/nginx_check.sh,如果不配置监测脚本,则Keepalived只关注自身是否存活,无法观测Nginx的状态#! /bin/bash #检测nginx是否启动了 A=`ps -C nginx -no-header | wc - 1` if [ $A -eq 0];then #如果nginx没有启动就启动nginx /usr/local/nginx/sbin/nginx #通过Nginx的启动脚本来重启nginx sleep 2 if [`ps -C nginx --no-header| wc -1` -eq 0 ];then #如果nginx重启失败,则下面就会停掉keepalived服务,进行VIP转移 killall keepalived fi fi -
分别启动两台服务器上的nginx和keepalived
- 先启动nginx
- 再启动keepalived

浙公网安备 33010602011771号