Nginx(“engine x”)是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。javascript
yum install zlib zlib-devel openssl openssl-devel pcre pcre-develcss
useradd -s /sbin/nologin -r wwwhtml
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar fx nginx-1.18.0.tar.gz
cd nginx-1.18.0/
./configure --prefix=/usr/local/nginx-1.18.0 \
--user=www \
--group=www \
--with-http_ssl_module \
--with-cc-opt=-O2 \
--with-cpu-opt=opteron \
--with-http_sub_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-stream \
--with-debug
make && make install
ln -s /usr/local/nginx-1.18.0/ /usr/local/nginx
参数说明:java
--prefix:指定安装路径 --with-http_ssl_module:启用ssl模块 --with-cc-opt=-O2: 设置 C 编译器参数 --with-cpu-opt=opteron: 指定编译的 CPU --with-http_sub_module:过滤模块,用于修改网站响应内容的字符串 --with-http_gunzip_module: 为那些不支持gzip模块的客户端解压缩 --with-http_gzip_static_module:启用静态压缩 --with-http_stub_status_module:启用状态模块 --with-http_realip_module:在访问日志中去掉代理IP,记录真实IP --with-http_flv_module: 这个模块支持对FLV(flash)文件的拖动播放 --with-http_mp4_module: 支持H.264/AAC文件为伪流媒体 --with-http_random_index_module:随机展现当前目录下的文件做为首页 --with-stream:启用4层代理 --with-debug:启用debug日志 --user:指定nginx运行的用户
/usr/local/nginx/sbin/nginx #启动
/usr/local/nginx/sbin/nginx -s stop #中止
/usr/local/nginx/sbin/nginx -s reload #重载
/usr/local/nginx/sbin/nginx -t #测试node
http://localhostlinux
cat /usr/local/nginx/conf/nginx.confnginx
#nginx进程,通常设置为和cpu核数同样 worker_processes 4; #错误日志存放目录 #错误日志级别:常见的错误日志级别有[debug | info | notice | warn | error | crit | alert | emerg],级别越高记录的信息越少 error_log /data1/logs/error.log crit; #运行用户,默认便是nginx,可不设置 user www #进程pid存放位置 pid /application/nginx/nginx.pid; #最大文件打开数(链接),可设置为系统优化后的ulimit -HSn的结果 worker_rlimit_nofile 51200; #cpu亲和力配置,让不一样的进程使用不一样的cpu worker_cpu_affinity 0001 0010 0100 1000; #工做模式及链接数上限 events { use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,可是仅用于linux2.6以上内核,能够大大提升nginx的性能 worker_connections 1024; #;单个后台worker process进程的最大并发连接数 } http { #当fastcgi状态码大于300时,将响应转发给error_page指令处理 fastcgi_intercept_errors on; #当被代理的后端状态码大于300时,返回给error_page处理 proxy_intercept_errors on; #默认编码 charset utf-8; #服务器名字的hash表大小 server_names_hash_bucket_size 128; #指定客户端请求头buffer大小 client_header_buffer_size 4k; #指定客户端请求中较大的消息头的缓存最大数量和大小 large_client_header_buffers 4 32k; #容许用户最大上传数据大小 client_max_body_size 300m; #开启高效文件传输模式 sendfile on; #激活tcp_nopush参数能够容许把httpresponse header和文件的开始放在一个文件里发布,积极的做用是减小网络报文段的数量 tcp_nopush on; #设置客户端链接保持活动的超时时间 keepalive_timeout 300; #激活tcp_nodelay,内核会等待将更多的字节组成一个数据包,从而提升I/O性能 tcp_nodelay on; #缓冲区代理缓冲用户端请求的最大字节数 client_body_buffer_size 512k; #与upstream server的链接超时时间 proxy_connect_timeout 50; #nginx跟后端服务器链接超时时间(代理读超时) proxy_read_timeout 100; #发送请求给代理服务器的超时时间 proxy_send_timeout 50; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffer_size 16k; #proxy_buffers缓冲区,网页平均在32k如下的话,这样设置 proxy_buffers 4 64k; #高负荷下缓冲大小(proxy_buffers*2) proxy_busy_buffers_size 128k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 proxy_temp_file_write_size 128k; #开启动态压缩 gzip on; #设置容许压缩的最小字节数 gzip_min_length 1k; #压缩缓冲区大小 gzip_buffers 4 16k; #压缩版本 gzip_http_version 1.1; #压缩比率。1最小处理最快,9压缩比最大传速速度快 gzip_comp_level 2; #指定压缩类型 gzip_types text/plain application/x-javascript text/css application/xml; #和http头有关,断送客户端浏览器是否支持压缩,肯定是否压缩 gzip_vary on; #隐藏版本号 server_tokens off; #访问日志格式设置 log_format main '$remote_addr - $remote_user [$time_local] "$host" "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" $request_time'; access_log logs/access.log main; #反向代理设置 upstream backend_server { server 10.0.0.20:81 weight=1 max_fails=2 fail_timeout=30s; } 状态值说明: weight:指定权重 max_fails:最大失败次数 fail_timeout: max_fails次失败后,暂停的时间 down:不参与负载 backup:非backup机器down或者忙的时候,请求backup机器 max_conns:指定后端服务器的最大链接数 server { listen 80; server_name www.blpwdev.com; #若是请求方法不是GET|POST返回444 if ($request_method !~* GET|POST) { return 444; } #禁止某些IP访问后端服务器 set $allow true; if ($remode_addr ~ “110.46.120.36”){ set $allow false; } if ($allow = false){ return 404; } location / { proxy_pass http://backend_server; #当某台返回错误时,继续分配给下一台机器处理 proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header; #该参数做用是对发送给客户端的URL进行修改 proxy_redirect off; #后端的Web服务器能够经过X-Forwarded-For获取用户真实IP proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #防盗链 location ~ .*\.(jpg|gif|png)$ { valid_referers none blocked www.blpwdev.com; if ($invalid_referer) { return 403; } root /opt/app/code/images; } error_page 404 @proxy_error; location @proxy_error { rewrite ^ http://www.baidu.com; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
补充:
nginx的location配置详解
语法规则: location [=|~|~|^~] /uri/ { … }
=:表示精确匹配,只有彻底匹配上才能生效。
^~:对URL路径进行前缀匹配,而且在正则以前。
~:表示区分大小写的正则匹配。
~:表示不区分大小写的正则匹配。
/uri:不带任何修饰符,也表示前缀匹配,可是在正则匹配以后。
/:通用匹配,任何未匹配到其它location的请求都会匹配到。后端
基本语法: rewrite regex replacement [flag];
上下文:server location if
rewrite配置语法中的flag参数:
last:中止rewrite检测【若是没有匹配到,会继续向下匹配】
break:中止rewrite检测【若是没有匹配到,则再也不向下匹配,直接返回结果】
redirect :返回302临时重定向,地址栏会显示跳转后的地址
permanent:返回301永久重定向,地址栏会显示跳转后的地址
例子:
rewrite ^/(.*) http://www.baidu.com/$1 permanent;浏览器
limit_conn_zone
语法: limit_conn_zone $variable zone=name:size;
默认值: none
配置段: http
$variable定义键,zone=name定义区域名称,后面的limit_conn指令会用到的。size定义各个键共享内存空间大小。如:
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_conn_log_level
语法:limit_conn_log_level info | notice | warn | error
默认值:error
配置段:http, server, location
当达到最大限制链接数后,记录日志的等级
limit_conn
语法:limit_conn zone_name number
默认值:none
配置段:http, server, location
指定每一个给定键值的最大同时链接数,当超过这个数字时被返回503错误
如:
limit_conn addr 1;
limit_conn_status
语法: limit_conn_status code;
默认值: limit_conn_status 503;
配置段: http, server, location
limit_rate
语法:limit_rate rate
默认值:0
配置段:http, server, location, if in location
对每一个链接的速率限制。参数rate的单位是字节/秒,设置为0将关闭限速。 按链接限速而不是按IP限制,所以若是某个客户端同时开启了两个链接,那么客户端的总体速率是这条指令设置值的2倍。
例子:缓存
http { limit_conn_zone $binary_remote_addr zone=limit:10m; limit_conn_log_level info; server { location ^~ /download/ { limit_conn limit 4; #设置用户下载文件前10m时不限速 limit_rate_after 10m; limit_rate 200k; alias /data/www/download/; } } }
limit_req_zone
语法: limit_req_zone $variable zone=name:size rate=rate;
默认值: none
配置段: http
设置一块共享内存限制域用来保存键值的状态参数
如:
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
说明:区域名称为one,大小为10m,平均处理的请求频率不能超过每秒一次
limit_req_log_level
语法: limit_req_log_level info | notice | warn | error;
默认值: limit_req_log_level error;
配置段: http, server, location
设置你所但愿的日志级别
limit_req_status
语法: limit_req_status code;
默认值: limit_req_status 503;
配置段: http, server, location
limit_req
语法: limit_req zone=name [burst=number] [nodelay];
默认值: —
配置段: http, server, location
设置对应的共享内存限制域和容许被处理的最大请求数阈值
如:
limit_req zone=one burst=5;
限制平均每秒不超过一个请求,同时容许超过频率限制的请求数很少于5个。
若是不但愿超过的请求被延迟,能够用nodelay参数,如:
limit_req zone=ttlsa_com burst=5 nodelay;
例子:
http { limit_req_zone $binary_remote_addr zone=one:10m rate=6r/m; server { location ^~ /download/ { limit_req zone=one burst=5 nodelay; alias /data/www/download/; } } }
语法:set_real_ip_from address|cidr|unix;
默认值: —
配置段: http, server, location
例子:
#真实服务器上一级代理的IP地址或者IP段
set_real_ip_from 0.0.0.0/0;
#定义其值替换客户端地址的请求头
real_ip_header X-Forwarded-For;
#递归地排除直至获得用户ip
real_ip_recursive on;
该ngx_http_sub_module模块是一个过滤器,经过将一个指定的字符串替换为另外一个字符串来修改响应
sub_filter
做用:用于替换字符串
用法:sub_filter 要替换的字符串 替换后的字符串,不区分字符串的大小写
范围:http server location
默认:-
sub_filter_last_modified
做用:容许在替换期间保留来自原始响应的“Last-Modified”标题字段以促进响应缓存。默认状况下,在处理期间修改响应的内容时,标题字段被删除。
用法:sub_filter_last_modified on | off
范围:http server location
默认:sub_filter_last_modified on
sub_filter_once
做用:指示是否查找每一个字符串以替换一次或重复替换。
用法:sub_filter_once on | off
范围:http server location
默认:http server location
sub_filter_types
做用:指定MIME类型的字符串替换,除了“ text/html” 以外,还能够在指定MIME类型的响应中启用字符串替换。特殊值“ *”匹配任何MIME类型
用法:sub_filter_types mime-type ...
范围:http server location
默认:sub_filter_types text / html;
例子:
location / { #把nginx.com替换成baidu.com sub_filter 'nginx.com' 'baidu.com'; #把nginx.org替换成baidu.org sub_filter 'nginx.org' 'baidu.org'; sub_filter_types css/html; #替换的类型 sub_filter_once off; #所有替换 root html; index index.html index.htm; }
nginx中的stub_status模块主要用于查看Nginx的一些状态信息. 本模块默认没有安装,须要编译安装
在nginx配置文件中的server段加上以下配置
location /nginx_status {
stub_status on;
access_log off;
}
返回各项数听说明:
Active connections: 当前nginx正在处理的活动链接数.
server 表示Nginx启动到如今共处理了 30 个链接
accepts 表示Nginx启动到如今共成功建立 30 次握手
handled requests 表示总共处理了 37 次请求
请求丢失数 = 握手数 - 链接数 ,能够看出目前为止没有丢失请求
Reading:Nginx 读取到客户端的 Header 信息数
Writing:Nginx 返回给客户端 Header 信息数
Waiting:Nginx 已经处理完正在等候下一次请求指令的驻留连接
自生成证书
cd /etc/pki/CA
touch index.txt
echo 01 > serial
#生成CA自签证书
(umask 077;openssl genrsa -out private/cakey.pem 2048)
openssl req -new -x509 -key private/cakey.pem -days 7300 -out cacert.pem
#生成证书
(umask 077;openssl genrsa -out /etc/nginx/ssl/nginx.key 2048)
openssl req -new -key /etc/nginx/ssl/nginx.key -days 365 -out /etc/nginx/ssl/nginx.csr
openssl ca -in /etc/nginx/ssl/nginx.csr -out /etc/nginx/ssl/nginx.crt -days 365
修改nginx配置文件
server { listen 443; #监听端口为443 server_name www.blpwdev.com ssl on; #开启ssl ssl_certificate /etc/nginx/ssl/nginx.crt; #证书位置 ssl_certificate_key /etc/nginx/ssl/nginx.key; #私钥位置 ssl_session_timeout 5m; #红色部分为可选项 ssl_protocols SSLv2 SSLv3 TLSv1; #指定密码为openssl支持的格式 ssl_ciphers HIGH:!aNULL:!MD5; #密码加密方式 ssl_prefer_server_ciphers on; #依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码 location / { root html; //根目录的相对位置 index index.html index.htm; } } server { listen 80; server_name www.localhost.com; send_timeout 1800; rewrite ^(.*)$ https://$host$1 permanent; }
auth basic
语法: auth_basic string | off;
默认值: auth_basic off;
配置段: http, server, location, limit_except
默认表示不开启认证,后面若是跟上字符,这些字符会在弹窗中显示
auth_basic_user_file
语法: auth_basic_user_file file;
默认值: —
配置段: http, server, location, limit_except
指定用户密码文件,使用htpasswd命令生成
nginx认证配置
建立用户密码文件
#htpasswd命令在httpd-devel包中,第一次建立用户须要加-c选项
htpasswd -c /usr/local/nginx/conf/htpasswd test
#密码生成也可使用openssl命令
printf “test:$(openssl passwd -crypt 123456)\n” >>/conf/htpasswd
在nginx配置文件中加入以下配置
server{ server_name www.blpwdev.com; index index.html index.htm; root html; location / { auth_basic "nginx basic http test for localhost.com"; auth_basic_user_file conf/htpasswd; #容许以列表格式显示文件 autoindex on; } }
开启缓存配置
proxy_cache_path /data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g use_temp_path=off; server { listen 80; server_name www.localhost.com; access_log /var/log/nginx/student_access.log cache; error_log /var/log/nginx/student_error.log ; location ~ /purge(/.*) { allow 127.0.0.1; deny all; proxy_cache_purge cache_one $host$1$is_args$args; } location ~ .*\.(gif|jpg|jpeg|png|bmp|ico|js|css)$ { proxy_cache cache_one; proxy_cache_valid 200 304 12h; proxy_cache_key $host$uri$is_args$args; #expires 30d; add_header X-Cache $upstream_cache_status; include /usr/local/nginx/vhost/proxy.configure; proxy_pass http://student_server; } location / { include /usr/local/nginx/vhost/proxy.configure; proxy_pass http://student_server; } log_format cache '$remote_addr [$time_local] "$request" ' '"$upstream_status" $body_bytes_sent "$http_referer" ' '"$http_user_agent" ' '"$upstream_addr" "$upstream_response_time" $upstream_cache_status';
#$upstream_cache_status 定义浏览器中的缓存状态 HIT MISS EXPIRED
清除缓存
http://student.metasequoia.com/resource/images/login13.png #缓存
curl student.fcla***oom.com/purge/resource/images/login13.png #清除缓存
参数说明
/data/proxy_cache_dir:本地路径,定义缓存资源的存放路径
levels:定义缓存文件目录层次
keys_zone:设置缓存名字和共享内存大小
inactive:未被访问文件在缓存中保留时间
max_size:最大cache空间,不指定会用掉磁盘全部空间
use_temp_path:为off会直接把缓存写入cache文件中,不会使用temp_path
proxy_cache:启动缓存,off表示关闭缓存
proxy_cache其它相关指定
proxy_no_cache: 定义知足条件的响应不会被缓存,其值有一个不为空或0生效
proxy_cache_bypass: 定义哪些状况不从cache读取
proxy_cache_key: 给缓存数据定义一个键,在清除缓存用到
proxy_cache_methods: 定义缓存哪些http方法
proxy_cache_valid: 设置不一样响应码的缓存时间
proxy_cache_min_uses: 设置缓存的最小使用次数
upstream配置
upstream xxxxxx{ server 10.0.0.100:8080; server 10.0.0.101:8080; check interval=2000 rise=2 fall=2 timeout=1000 type=http; check_http_send "HEAD /check.txt HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx http_4xx; }
其中参数的定义以下:
interval:检查的周期
fall:容许的检查失败次数,若失败次数超过该值,则后端被标记为"down"。
rise:检查的成功次数达到该值,则后端被标记为“up”。
timeout:检查的超时时间。
type:后端检查的协议类型(tcp|http)。
开启状态页面
location /status { check_status; access_log off; allow 127.0.0.1; deny all; }