nginx

nginx是一个高性能的HTTP和反向代理web服务器,同时也提供IMAP/POP3/SMTP服务

正向代理:

反向代理:

安装nginx

yum install -y openssl openssl-devel
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

nginx.conf

配置结构:

核心配置:

events {
    # 默认使用epoll
    use epoll;
    # 每个worker允许连接的客户端最大连接数
    worker_connections 10240;
}
http {
}
sendfile on;
tcp_nopush on;
# keepalive_timeout 0;
keepalive_timeout 65;
# 开启gzip压缩功能,目的:提高传输效率,节约带宽
gzip on;
# 限制最小压缩,小于1字节的文件不会压缩
gzip_min_length 1;
# 定义压缩级别(压缩比,文件越大压缩越多,但是cpu使用会越多)
gzip_comp_level 3;
# 定义压缩文件的类型
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-http-php image/gif image/png application/json;
location / {
    root /home;
}
* `=`:精确匹配
location = /img/test.png {
    root /home;
}
* `~*`:匹配正则表达式,不区分大小写
location ~* \.(GIF|jpg|png|jpeg) {
    root /home;
}
* `~`:匹配正则表达式,区分大小写
location ~ \.(GIF|jpg|png|jpeg) {
    root /home;
}
* `^~`:以某个字符路径开头
location ^~ /img {
    root /home;
}
# 允许跨域请求的域,*代表所有
add_header 'Access-Controller-Allow-Origin' *;
# 允许带上cookie请求
add_header 'Access-Controller-Allow-Credentials' 'true';
# 允许请求的方法
add_header 'Access-Controller-Allow-Methods' *;
# 允许请求的header
add_header 'Access-Controller-Allow-Headers' *;
# 对源站点验证
valid_referers *.test.com;
# 非法引入会进入下方判断
if ( $invalid_referer ) {
    return 404;
}

nginx常用命令

nginx日志切割

#!/bin/bash
LOG_PATH="/var/log/nginx"
RECORD_TIME=$(date -d "yesterday" +%Y-%m-%d+%H:%M)
PID=/var/run/nginx/nginx.pid
mv ${LOG_PATH}/access.log ${LOG_PATH}/access.${RECORD_TIME}.log
mv ${LOG_PATH}/error.log ${LOG_PATH}/error.${RECORD_TIME}.log
# 向nginx主进程发送信号,用于重新打开日志文件
kill -USR1 `cat $PID`
chmod +x cut_nginx_log.sh
yum install crontabs
*/1 * * * * /usr/local/nginx/sbin/cut_nginx_log.sh
service crond restart
* 附:常用定时任务命令
service crond start     // 启动服务
service crond stop     // 关闭服务
service crond restart     // 重启服务
service crond reload     // 重新载入配置
crontab -e               // 编辑任务
crontab -l              // 查看任务列表

nginx模块化体系

nginx负载均衡配置、常用策略、场景及特点

# 配置上游服务器
upstream backserver{
    # nginx默认轮询
    server 127.0.0.1:8080; 
    server 127.0.0.1:6060;
    server 127.0.0.1:7070;
}
server {
    listen              80;
    server_name         www.backserver.com;
    location / {
        proxy_pass http://backserver;
    }
}
# 配置上游服务器
upstream backserver{
    # weight参数默认为1
    server 127.0.0.1:8080 weight=1; 
    server 127.0.0.1:6060 weight=2;
    server 127.0.0.1:7070 weight=5;
}
server {
    listen              80;
    server_name         www.backserver.com;
    location / {
        proxy_pass http://backserver;
    }
}
# 配置上游服务器
upstream backserver{
    ip_hash;
    server 127.0.0.1:8080; 
    server 127.0.0.1:6060;
    server 127.0.0.1:7070;
}
server {
    listen              80;
    server_name         www.backserver.com;
    location / {
        proxy_pass http://backserver;
    }
}
# 配置上游服务器
upstream backserver{
    # $request_uri是nginx内置变量,uri
    hash $request_uri;
    server 127.0.0.1:8080; 
    server 127.0.0.1:6060;
    server 127.0.0.1:7070;
}
server {
    listen              80;
    server_name         www.backserver.com;
    location / {
        proxy_pass http://backserver;
    }
}
# 配置上游服务器
upstream backserver{
    # least_conn请求连接最少的服务
    least_conn;
    server 127.0.0.1:8080; 
    server 127.0.0.1:6060;
    server 127.0.0.1:7070;
}
server {
    listen              80;
    server_name         www.backserver.com;
    location / {
        proxy_pass http://backserver;
    }
}

负载均衡配置参数

upstream指令参数

# 配置上游服务器
upstream backserver{
    # max_conns默认为0,不限制连接数
    server 127.0.0.1:8080 max_conns=2; 
    server 127.0.0.1:6060 max_conns=2;
    server 127.0.0.1:7070 max_conns=2;
}
server {
    listen              80;
    server_name         www.backserver.com;
    location / {
        proxy_pass http://backserver;
    }
}
# 配置上游服务器
upstream backserver{
    # slow_start默认为0,不开启
    server 127.0.0.1:8080 weight=2 slow_start=60s; 
    server 127.0.0.1:6060 weight=2;
    server 127.0.0.1:7070 weight=2;
}
server {
    listen              80;
    server_name         www.backserver.com;
    location / {
        proxy_pass http://backserver;
    }
}
# 配置上游服务器
upstream backserver{
    # down标识8080节点不可用
    server 127.0.0.1:8080 down; 
    server 127.0.0.1:6060 weight=1;
    server 127.0.0.1:7070 weight=1;
}
server {
    listen              80;
    server_name         www.backserver.com;
    location / {
        proxy_pass http://backserver;
    }
}
# 配置上游服务器
upstream backserver{
    # backup标识8080节点为备用机
    server 127.0.0.1:8080 backup; 
    server 127.0.0.1:6060 weight=1;
    server 127.0.0.1:7070 weight=1;
}
server {
    listen              80;
    server_name         www.backserver.com;
    location / {
        proxy_pass http://backserver;
    }
}
# 配置上游服务器
upstream backserver{
    # 在10秒内请求8080失败达到2次,则认为8080已经挂了,
    # 随后再过10秒,这10秒内不会由新的请求到达8080节点上,
    # 10秒后会再有新请求尝试连接挂掉的8080,如果还是失败,
    # 重复上一个过程,知道回复
    server 127.0.0.1:8080 max_fails=2 fail_timeout=10s; 
    server 127.0.0.1:6060 weight=1;
    server 127.0.0.1:7070 weight=1;
}
server {
    listen              80;
    server_name         www.backserver.com;
    location / {
        proxy_pass http://backserver;
    }
}
# 配置上游服务器
upstream backserver{
    server 127.0.0.1:8080 max_fails=2 fail_timeout=10s; 
    server 127.0.0.1:6060 weight=1;
    server 127.0.0.1:7070 weight=1;
    # 设置长连接处理的数量
    keepalive 32;
}
server {
    listen              80;
    server_name         www.backserver.com;
    location / {
        proxy_pass http://backserver;
        # 设置长连接http版本为1.1
        proxy_http_version 1.1;
        # 清楚connection header信息
        proxy_set_header Connection "";
    }
}

nginx缓存

# proxy_cache_path 设置缓存目录
#       keys_zone  设置共享内存以及占用空间大小
#       max_size   设置缓存大小
#       inactive   超过此时间则被清理
#       use_temp_path 临时目录,使用后会影响nginx性能(关闭临时目录)
proxy_cache_path /usr/local/nginx/upstream_cache keys_zone=mycache:5m max_size=1g inactive=1m use_temp_path=off;
server {
    # 启用缓存和keys_zone一致
    proxy_cache mycache;
    # 针对200和304状态码缓存时间为8小时
    proxy_cache_valid 200 304 8h;
    location / {
        proxy_pass http://home;
    }
}
* 用户访问仍然会产生请求流量
* 控制浏览器缓存:
# expires [time] expires @[time] 
location /files {
    alias /home;
    # expires过期时间
    # 10秒间隔
    # expires 10s;
    # 晚上10点半过期
    # expires @22h30m;
    # 1小时钱缓存失效
    # expires -1h;
    # no cache
    # expires epoch;
    # 默认值(浏览器默认缓存时间)
    # expires off;
    # 永不过期
    expires max;
}

使用nginx配置HTTPS域名证书

upstream backserver {
    server 127.0.0.1:8080;
}
server {
    listen 443;
    server_name www.backserver.com;
    # 开启ssl
    ssl on;
    # 配置ssl证书
    ssl_certificate [证书路径];
    # 配置s证书密钥
    ssl_certificate_key [证书密钥路径];
    # ssl会话cache
    ssl_session_cache shared:SSL:1m;
    # ssl会话超时时间
    ssl_session_timeout 5m;
    # 配置加密套件,写法遵循openssl标准
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    location / {
        proxy_pass http://backserver;
    }
}

发表评论

发表
Table of Contents