系统准备
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 系统基本库安装
yum -y install gcc gcc -c ++ autoconf pcre pcre -devel make automake
# 基本工具
yum -y install wget httpd -tools vim
# 调试目录初始化
cd /opt ; mkdir app download logs work backup
# 查看iptables是否关闭
iptables -L
# 如未关闭,执行
iptables -F
# 保险起见
iptables -t nat -L
iptables -t nat -F
# 查看SELinux是否开启
getenforce
# 如未关闭,执行
setenforce 0
Nginx安装
# 官网稳定版链接
http : //nginx.org/en/linux_packages.html#stable
# 新建/etc/yum.repos.d/nginx.repo,按照官网的提示这里将OS替换成centos, OSRELEASE替换成7
[ nginx ]
name =nginx repo
baseurl =http : //nginx.org/packages/centos/7/$basearch/
gpgcheck =0
enabled =1
#必要时重建缓存可使用
yum clean all
yum makecache
查看yum安装的内容
安装目录主要文件讲解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Nginx日志轮转配置
/etc /logrotate . d /nginx
# Nginx主配置文件
/etc /nginx /nginx . conf
/etc /nginx /conf . d
/etc /nginx /conf . d /default . conf
# Nginx的cgi相关配置
/etc /nginx /fastcgi_params
/etc /nginx /scgi_params
/etc /nginx /uwsgi_params
# 编码转换映射转化文件
/etc /nginx /koi -utf
/etc /nginx /koi -win
/etc /nginx /win -utf
# 设置http协议的Content-Type与扩展名对应关系
/etc /nginx /mime . types
# 用于配置系统守护进程管理器管理方式
/etc /sysconfig /nginx
/etc /sysconfig /nginx -debug
/usr /lib /systemd /system /nginx -debug . service
/usr /lib /systemd /system /nginx . service
# Nginx模块目录
/etc /nginx /modules
/usr /lib64 /nginx /modules
# Nginx服务启动管理的终端命令
/usr /sbin /nginx
/usr /sbin /nginx -debug
# Nginx的手册和帮助文件
/usr /share /doc /nginx -1.12.1
/usr /share /doc /nginx -1.12.1 /COPYRIGHT
/usr /share /man /man8 /nginx . 8.gz
# Nginx的缓存目录
/var /cache /nginx
# Nginx的日志目录
/var /log /nginx
nginx -V查看编译时使用的参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 安装目标目录或路径
--prefix =/etc /nginx
--sbin -path =/usr /sbin /nginx
--modules -path =/usr /lib64 /nginx /modules
--conf -path =/etc /nginx /nginx . conf
--error -log -path =/var /log /nginx /error . log
--http -log -path =/var /log /nginx /access . log
--pid -path =/var /run /nginx . pid
--lock -path =/var /run /nginx . lock
# 执行对应模块时,Nginx所保留的临时性文件
--http -client -body -temp -path =/var /cache /nginx /client_temp
--http -proxy -temp -path =/var /cache /nginx /proxy_temp
--http -fastcgi -temp -path =/var /cache /nginx /fastcgi_temp
--http -uwsgi -temp -path =/var /cache /nginx /uwsgi_temp
--http -scgi -temp -path =/var /cache /nginx /scgi_temp
# 设定Nginx进程启动的用户和用户组
--user =nginx
--group =nginx
# 设置额外的参数将被添加到CFLAGS变量
--with -cc -opt =. . .
# 设置附加的参数,链接系统库
--with -ld -opt =. . .
日志
error_log
access_log
可能过log_format 来进行格式的定义
日志参数:
http://nginx.org/en/docs/http/ngx_http_core_module.html#variables
常用配置参数(同样可通过nginx -V查看,以–with开头)
1. stub_status
# Nginx客户端状态
--with -http_stub_status_module
比如在/etc /nginx /conf . d /default . conf 下加入
location /mystatus {
stub_status ;
}
重新加载配置文件后访问http://your.ip.address/mystatus即可获得类似如下信息
Active connections : 2
server accepts handled requests
2 2 1
Reading : 0 Writing : 1 Waiting : 1
2. random_index
# 目录中选择一个随机主页
--with -http_random_index_module
# 对/etc/nginx/conf.d/default.conf文件做类似如下修改
location / {
root /usr /share /nginx /html ;
#index index.html index.htm;
random_index on ;
}
此时再访问网站首页,会随机展示/usr/share/nginx/html下的html文件
3. sub_filter
# http内容替换
--with -http_sub_module
# 内容替换
sub_filter string replacement ;
# 判断是否有更新(缓存场景)
sub_filter_last_modified on | off ;
# 默认on, 匹配替换第一个,off替换所有
sub_filter_once on | off ;
# 可放置在http, server, location中
4. 连接、请求限制
limit_conn_module 连接 频率限制
limit_conn_zone可在http中设置,limit_conn可在http, server, location中设置
limit_conn_zone key zone=name:size;
limit_conn zone number
limit_req_module 请求 频率限制
limit_req_zone可在http中设置,limit_req可在http, server, location中设置
# key可使用变量如b i n a r y r e m o t e a d d r 代 表 针 对 I P 进 行 限 制 , binary_remote_addr比$remote_addr字节数少因而在这里使用,zone用于自定义名称供limit_req使用,rate代表指定时间内的请求数量如1r/s
limit_req_zone key zone =name:size rate =rate;
# burst代表将指定的请求数量放到下一秒执行, nodelay设置有无延时
limit_req zone =name [burst =number] [nodelay ];
# 示例代码/etc/nginx/conf.d/default.conf
limit_conn_zone $ binary_remote_addr zone =conn_zone : 1m ;
limit_req_zone $ binary_remote_addr zone =req_zone : 1m rate =1r /s ;
server {
. . .
location / {
. . .
#sub_filter 'Alan' 'ALAN';
limit_conn conn_zone 1 ;
#limit_req zone=req_zone burst=3 nodelay;
#limit_req zone=req_zone burst=3;
#limit_req zone=req_zone;
}
访问控制
基于IP 的访问控制 http_access_module
#CIDR指网段,如192.168.1.0/24,unix:为socket,可在http, server, location, limit_except中进行设置
allow address | CIDR | unix: | all;
deny address | CIDR | unix: | all;
# 如仅允许某个或某网段IP
location / {
. . .
allow 192.168.1.1 ;
deny all ;
}
# 限制某个或某网段IP访问
location / {
. . .
deny 192.168.1.0 、24 ;
allow all ;
}
局限性:IP地址可能会发生变化,也可能通过代理来进行访问(ip_addr仅传递代理后的IP, http_x_forwarded_for会传递客户端和代理IP,但VPN有可能不遵循此协议)
基于用户登录 认证 http_auth_basic_module
可在http, server, location, limit_except中进行设置
auth_basic string | off ; #输入提示文字
auth_basic_user_file file ; #可能过htpasswd来生成该文件
htpasswd -c . /file_name username #执行后输入密码(需安装httpd-tools)即可生成文件
# openssl passwd -crypt mypassword
这种方法相对低效,企业方案可采用
Nginx结合LUA实现验证或Nginx和LDAP打通(需安装nignx-auth-ldap模块)
静态资源WEB服务
HTML,CSS,JS; JPEG,GIF,PNG; FLV,MPEG; TEXT等文件(CDN)
可在http, server, location, if in location中设置:
sendfile on | off
扩展:–with-file-aio异步文件读取
可在http, server, location中设置,用于提高网络包的传输效率,需开启sendfile:
tcp_nopush on | off;
可在http, server, location中设置,在keepalive下用于提高网络包的传输实时性:
tcp_nodelay on | off;
压缩, 可在http, server, location, if in location中设置:
gzip on | off;
默认压缩比为1,通过修改level来进行设置(压缩比越高,性能消耗越大),可在http, server, location中设置:
gzip_comp_level level;
控制http协议版本,主流和默认的为1.1,可在http, server, location中设置:
gzip_http_version 1.0 | 1.1;
http_gzip_static_module
http_gunzip_module #用于解决对gzip不支持的浏览器的问题,不常用
浏览器校验过期机制:
Expires, Cache-control(max-age)
协议中Etag头信息校验
Last-Modified头信息校验
示例配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
server {
. . .
sendfile on ;
location ~ . *\ . ( jpg | gif | png ) $ {
gzip on ;
gzip_http_version 1.1 ;
# Compression level (1-9).
gzip_comp_level 1 ;
# gzip_min_length 256;
# gzip_proxied any;
# gzip_vary on;
gzip_types text /plain application /javascript application /x -javascript text /css application /xml text /javascript application /x -http -php image /jpeg image /gif image /png ;
root /opt /app /code /images ;
}
location ~ . *\ . ( txt | xml ) $ {
gzip on ;
gzip_http_version 1.1 ;
gzip_comp_level 1 ;
gzip_types text /plain application /javascript application /x -javascript text /css application /xml text /javascript application /x -http -php image /jpeg image /gif image /png ;
root /opt /app /code /doc ;
}
location ~ . *\ . ( htm | html ) $ {
# 跨域访问
add_header Access -Control -Allow -Origin domain . com ;
add_header Access -Control -Allow -Methods GET , POST , DELETE , PUT , OPTIONS ;
expires 24h ;
root /opt /app /code ;
}
location ~ ^/download {
gzip_static on ;
tcp_nopush on ;
root /opt /app /code ;
}
}
referer防盗链
# 图片防盗链,server_names为当前域名
location ~ * \ . ( gif | jpg | png | bmp ) $ {
valid_referers none blocked * . abc . com server_names ~ \ . google \ . ~ \ . baidu \ . ;
if ( $ invalid_referer ) {
return 403 ;
}
}
# 测试方法
curl -e "http://www.baidu.com" -I http : //alanhou.org/test.jpg
Nginx代理服务
反向代理示例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
location / {
# 可以使用include proxy_params;(将相关配置放到proxy_params文件中)
proxy_pass http : //127.0.0.1:8080;
proxy_redirect default ;
proxy_set_header Host $ http_host ;
proxy_set_header X -Real -IP $ remote_addr ;
proxy_connect_timeout 30 ;
proxy_send_timeout 60 ;
proxy_read_timeout 60 ;
proxy_buffer_size 32k ;
proxy_buffering on ;
proxy_buffers 4 128k ;
proxy_busy_buffers_size 256k ;
proxy_max_temp_file_size 256k ;
}
正向代理示例代码
http {
resolver 8.8.8.8 ;
server {
listen 8088 ;
location / {
proxy_pass http : //h t t p h o s t request_uri;
}
}
}
负载均衡调度器SLB
示例代码:
upstream mysevers {
#least_conn; #转发请求到连接最少的服务器上
#ip_hash; #同一IP的访问同一台服务器
hash $ request_uri ; #针对url或自定义变量进行哈希,以使相同用户相同页面访问到的是同一台服务器
#server 192.168.1.11 weight=5; #设置轮询权重,默认轮询
server 192.168.1.11 ;
server 192.168.1.12 ;
server 192.168.1.13 backup ; #down, backup, max_fails, fail_timeout, max_conns
}
server {
. . .
location / {
proxy_pass http : //myservers;
}
}
缓存配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
proxy_cache_path /opt /app /cache levels =1 : 2 keys_zone : my_cache : 10m max_size =10g inactive =60m use_temp_path =off ;
if ( $ request_uri ~ ^/( login | register \ password \ /reset ) ) {
set $ cookie_nocache 1 ;
}
server {
. . .
location / {
proxy_cache my_cache ;
proxy_pass http : //myserver;
proxy_cache_valid 200 304 12h ;
proxy_cache_valid any 10m ;
proxy_cache_key $ host $ uri $ is_args $ args ;
proxy_no_cache $ cookie_nocache $ arg_nocache $ arg_comment ;
proxy_no_cache $ http_pragma $ http_authorization ;
add_header Nginx_Cache "$upstream_cache_status" ;
proxy_next_upstream error timeout invalid_header http_500 http_502 https_503 http_504 ;
}
}
常用操作
# 开启服务
systemctl start nginx . service
# 查看状态
systemctl status nginx . service
# 重载服务
systemctl reload nginx . service
# 停止服务
systemctl stop nginx . service
# 检查配置文件是否有错误
nginx -tc /etc /nginx /nginx . conf
# 重新加载配置文件(修改配置文件后使用)
nginx -s reload -c /etc /nginx /nginx . conf
常见问题
1. Job for nginx.service failed because the control process exited with error code. See “systemctl status nginx.service” and “journalctl -xe” for details.
先执行setenforce 0再进行排查