1.nginx概念
百度,主要三个作用:反向代理、负载均衡、动静分离
2.nginx安装、命令、配置文件
下载网址,选择最后的稳定版本即可(最好在linux版本获取最大性能):
http://nginx.org/en/download.html
安装依赖,安装流程自行百度安利:
pcre
openssl
zlib
关闭防火墙或配置开放端口号
1)查看防火墙
firewall-cmd --list-all
2)防火墙设置添加端口号
firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd --add-port=80/tcp --permanent
3)重启防火墙
firewall-cmd --reload
=================================================================
命令介绍,目录位于 usr/local/nginx/sbin
1)查看版本
./nginx -v
2)启动
./nginx
3)停止
./nginx -s stop
4)重加载
./nginx -s reload
=================================================================
配置文件目录位于 usr/local/nginx/sbin/conf
主要包括三个块:全局块、events块、http块,http块下又包括http全局块、server块等
添加nginx服务
(1)在/usr/lib/systemd/system下添加nginx.service文件,内容如下:
[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -c -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=default.target
(2)添加权限
chmod 755 /usr/lib/systemd/system/nginx.service
(3)使用系统命令来操作Nginx服务
启动:systemctl start nginx
停止:systemctl stop nginx
重启:systemctl restart nginx
重新加载配置文件:systemctl reload nginx
查看nginx状态:systemctl status nginx
开机启动:systemctl enable nginx
Nginx命令配置到系统环境
(1)修改/etc/profile文件
vim /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin
(2)立即生效
source /etc/profile
(3)执行nginx命令
nginx -V
3.反向代理
1) http/server/location下添加反向代理
proxy_pass http://ip:port;
2) 匹配URL的反向代理
location ~ /api/ {
proxy_pass http://ip:port;
}
注:location有多个,实现多个代理
location匹配规则如下:
4.负载均衡
1)http下添加配置
upstream myserver {
ip_hash;
server ip:port weight=1;
server ip:port weight=2;
}
2)location配置
location {
proxy_pass http://myserver;
proxy_connect_timeout 10;
}
3)负载均衡策略:
轮询(默认)、权重 weight、固定访客 ip_hash、响应时间优先 fair
5.动静分离
静态页面代理配置
location /www/ {
root /data/;
index index.html index.htm;
auto_index on;
}
6.nginx配置高可用集群
需要配合keepalived使用
安装keepalived: yun install keepalived -y
查看是否安装: rpm -q -a keepalived
启动: systemctl start keepalived.service
停止: systemctl stop keepalived.service
加入开机启动: systemctl enable nginx
# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
33124998@qq.com
}
notification_email_from 33122323@qq.com
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL #路由配置同etc/hosts里注册的域名
}
vrrp_script_http_port {
script "/usr/local/src/nginx_check.sh"
interval 2 #(检测脚本执行的间隔时间)
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33 #同ip a看到的网卡
virtual_router_id 51 #主从的virtual_router_id须一致
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #虚拟IP地址
192.168.1.110/24 dev ens33 label ens33:1
}
}
# vim /usr/local/src/nginx_check.sh
#!/bin/bash
A=`ps -C nginx -no-header | wc -l`
if [$A -eq 0]; then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ]; then
killall keepalived
fi
fi
7.nginx原理
注:1)可以 有一个master多个worker,每个worker是独立的线程,挂掉一个不影响其他的worker
2)worker数量一般配置与CPU数目一致,但在window系统中该配置无效,争
3)worker连接数worker-connections,为双数,访问静态资源占用2个连接数,访问动态资源占用4个连接数,每个worker的最大并发数为 worker-processes * worker-connections / 4
worker-processes 4
worker-connections 1024
8.浏览器缓存
浏览器缓存都在响应头中 ETag Last-modified
指令 expires [modified] time 或 expires epoch|max|off;
add_header Cache-control:no-cache;
跨域问题:协议、域名(IP)、端口一致就是同源,否则为跨域
解决跨域要添加两个头信息:Access-Control-Allow-Origin、Access-Control-Allow-Methods
写法如下:
add_headers Access-Control-Allow-Origin http://ip:port;
add_headers Access-Control-Allow-Methods GET,POST,PUT,DELETE;
资源防盗链 Referer
valid_referers none|blocked|server_names|string...
例:
valid_referers none blocked www.baidu.com
if ($invalid_referer) {
return 403;
}
精细化防盗使用模块:ngx_http_accesskey_module
9.文件传输处理
sendfile on; #增加传输文件效率
tcp_nopush on; #提升网络包效率
tcp_nodelay on; #该指令在keep-alive开启下生效,来提高网络包传输的实时性
上两个参数看上去冲突,但ngnix高版本能支持缓存区满立即发送,在最后一包未满时实时发送
gzip #文件压缩方式
ngix_http_gzip_module模块
ngix_http_gzip_static_module模块,读取与文件前缀一样+.gz的文件,需要先手动压缩文件
gzip on; #网络包压缩功能开启,会消耗nginx所在服务器的计算能力