一、nginx
- 基本概念
- 占用内存少,高并发
- 正向代理(代理服务器和客户端处于同一个local area network内,客户端需要配置)
- 反向代理(hide原始服务器、expose代理服务器)
- 负载均衡
- 动静分离
二、nginx的安装、常用命令、配置
- nginx官网: http://nginx.org/
#确保防火墙是否开放80端口 如果没有
firewall-cmd --permanent --zone=public --add-port=80/tcp
#重新加载
firewall-cmd --reload
#查看列表
firewall-cmd --list-all
- 编译安装
-
安装包编译安装(相对麻烦)
安装PCRE库
#配置最新Yum源(epel源)
#如果没有使用yum安装
yum -y install gcc gcc-c++ autoconf automake
#安装PCRE库
#在ngnix目录下 cd /usr/local/tools/ngnix 下载
wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz
#解压
tar -zxvf openssl-1.0.1j.tar.gz
#进入并初始化 进行源码安装(源码的安装一般由3个步骤组成:配置(configure)、编译(make)、安装(make install))
cd openssl-1.0.1j
./config
make && make install
安装nginx
#查看是否安装
which nginx
nginx -v
#进入目录
cd /usr/local/tools/ngnix
#下载并解压nginx1.8版本
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar -zxvf nginx-1.8.0.tar.gz
#进入目录编译安装
cd nginx-1.8.0
./configure
make && make install
下载后安装包可自行删除,启动nginx
#查找nginx路径
whereis nginx
#启动nginx(在刚才./configure会提示启动路径) 启动后可仅输入ip在浏览器测试 nginx默认80端口
/usr/local/nginx/sbin/nginx
#重新加载 这边nginx如果是停止状态会报错
/usr/local/nginx/sbin/nginx -s reload
#停止
/usr/local/nginx/sbin/nginx -s stop
#强制停止
pkill nginx
#测试配置文件是否正常
/usr/local/nginx/sbin/nginx -t
#查看默认配置文件 (根据whereis nginx.conf查找)
cat nginx.conf
- yum安装
2.yum安装nginx(快捷)
#yum安装nginx
sudo yum install -y nginx
#启动nginx
sudo systemctl start nginx.service
#设置开机自启动
sudo systemctl enable nginx.service
#yum安装的nginx配置文件默认存放在 /etc/nginx/nginx.conf 查看
vi /etc/nginx/nginx.conf
#卸载
yum remove nginx
有epel源的时候并且用yum install nginx 安装就会显示Welcome to nginx on Fedora!,因为epel源就是Fedora维护的
- docker安装
3.使用Docker安装nginx
#前提是服务器装有docker服务 因为80端口已占用 使用81端口
docker run -p 81:80 --name nginx -d nginx:latest
#进入启动nginx镜像的容器
docker exec -it ef6a74b78b75 /bin/bash
#测试的注意81端口是否开启,如果是阿里云,腾讯云等注意安全组设置,为保证配置文件持久化(不会因为重启容器消失),可自行根据需要将容器内的配置文件或日志文件挂载在宿主机上
nginx配置文件三部分
- 全局块
user nginx;
worker_processes 1; #处理并发数量
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
- events块
events {
worker_connections 1024; #支持最大连接数
}
- http 全局块
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
include /etc/nginx/conf.d/*.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/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 {
# 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
# 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;
}
反向代理配置
#确保防火墙是否开放8080、80端口 如果没有
firewall-cmd --permanent --zone=public --add-port=8080/tcp
firewall-cmd --permanent --zone=public --add-port=80/tcp
#重新加载
firewall-cmd --reload
#查看列表
firewall-cmd --list-all
配置本地hosts文件:
域名 IP
www.long.com 192.168.1.136 #这是nginx反向代理主机的IP
http 全局块的server配置
server {
listen 80;
server_name 192.168.1.136;
location / {
proxy_pass http://127.0.0.1:8080; #这是后端服务器tomcat的IP,通过proxy_pass转发实现
}
}
访问地址:http://www.long.com:80 #首先寻找本地的hosts解析,如果本地没有解析,会去寻找其他DNS服务器帮助解析。
确定9001、8081、8082端口是对外开放的
server {
listen 9001;
server_name 192.168.1.136;
location ~ /edu/ {
proxy_pass http://127.0.0.1:8081; #这是后端服务器tomcat的IP,通过proxy_pass转发实现,匹配到edu就转发8081
}
location ~ /vod/ {
proxy_pass http://127.0.0.1:8082; #这是后端服务器tomcat的IP,通过proxy_pass转发实现,匹配到vod就转发8082
}
}
访问地址:http://www.long.com:80/edu
访问地址:http://www.long.com:80/vod
动静分离配置
server {
listen 80;
server_name 192.168.1.136; #nginx的IP
location /www/ {
root /data/;
}
location /image/ {
root /data/;
autoindex on; #列出内容
}
}
nginx高可用
keepalived软件
nginx性能优化--影响性能优化的指标
压测工具:ab、第三方工具(压测宝)
1、网络
网络的流量:带宽
网络丢包率:具体哪个环节丢包率高,网络不行,后端架构再好也没用
2、系统(监控工具观察指标)
硬盘是否损坏
磁盘的读写速率是否足够
系统负载是否很高
内存是否足够支持
系统是否足够稳定
3、服务
连接的优化
请求的优化
流量的限制
4、程序(开发相关)
接口的性能
处理的速度
程序执行的效率
5、数据库
是否存在慢查询
数据库的响应时间
数据库磁盘的读写性能
数据库是否需要加缓存
nginx优化---系统优化入口
文件句柄 :Linux一切皆文件,文件句柄简单理解为是一个索引,随着进程的调用频繁增加,系统默认是有限制,必须调整优化
查看文件描述符:ulimit -n 系统默认是1024
设置方式:系统全局性修改、用户局部性修改、进程局部性修改
vi /etc/security/limits.conf
#针对root用户
root soft nofile 65535
root hard nofile 65535
#针对所有用户
* soft nofile 25535
* hard nofile 25535
#针对nginx进程,配置在nginx.conf全局配置里
worker_rlimit_nofile 45535 #调整至1万以上,负荷较高建议2-3万以上
CPU亲和
较少进程之间的频繁切换,减少性能消耗
1、查看当前cpu物理状态
[root@www ~]# lscpu | grep "CPU(s)"
CPU(s): 4 #4个CPU ,比如24个核心
On-line CPU(s) list: 0-3
2、将nginx worker进程绑定到不同核心上,官方建议绑定的数量和cpu核心一致
方式1
worker_processes 2;
worker_cpu_affinity 000001 000010........
方式2(1.19版本之后)
worker_processes auto;
worker_cpu_affinity auto;
user nginx; #用户配置,最小权限为安全
error_log /var/log/nginx/error.log warn; #只要是warn就打印
events {
use epoll ; #网络io模型,epoll性能高
multi_accept on;#如果multi_accept被禁止了,nginx一个工作进程只能同时接受一个新的连接。否则,一个工作进程可以同时接受所有的
新连接
worker_connections 10240; #限制每个进程能处理多少连接请求,10240*worker_processes
}
http {
charset utf-8; #统一使用utf-8字符集
log_format main ............
access_log /var/log/nginx/access.log main ; #也可以每个server下,根据具体情况来决定是否需要打印日志,因为写日志会
占用磁盘的io ,会对性能有影响,是否开启,是写到内存还是磁盘,有待考虑
sendfile on ; #开启文件的高效传输
tcp_nopush on; #使用静态服务器建议打开
#使用动态资源服务器时建议打开,需要打开keepalived
tcp_nodelay on;
keepalive_timeout 65;
gizp on;
gizp_disable "MSIE [1-6]\. "; #IE6之前是不支持压缩功能的
gzip_http_version 1.1;
}
Virtal Server
include /etc/nginx/conf.d/*.conf #虚拟主机的位置
上面是通用配置,其他定向优化项:静态代理proxy_cache 、防盗链、防止跨域访问、浏览器的缓存、客户端的优化、file io、limit_conn_zone、socket
hash、fastcgi、gzip
文中的nginx高可用和负载均衡没有展开说明,后期补充