安装
yum install pcre-devel openssl-devel gcc zlib-devel -y
tar zxf nginx-1.10.3.tar.gz
cd nginx-1.10.3
vim src/core/nginx.h 去除版本号
vim auto/cc/gcc 注释掉
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
make && make install
useradd nginx
ln -s /usr/local/nginx/sbin/nginx /sbin
nginx 启动
nginx 配置
user nginx; 用户
worker_processes auto; 工作数
全局日志
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 65535; 文件缓存数 这里修改65535 之后需要在/etc/security/limits.conf 添加 nginx - nofile 65535
}
http {
include mime.types;
default_type application/octet-stream; 默认 需要下载选择application/octet-stream
access_log logs/access.log main main日志地址
sendfile on; 高效传输文件模式
tcp_nopush on; 配置一次发送数据的包大小
tcp_nodelay on; 有包就发
keepalive_timeout 65; 持续连接
limit_conn_zone $binary_remote_addr zone=addr:5m; 下载带宽
server {
listen 80; 端口
server_name localhost; 用户名
charset utf-8; 输出格式
location /download { 访问目录需要在html下建立这个目录
limit_conn addr 1; 并发连接数
}
在浏览器所在的主机上执行 ab -c1 -n100 http://172.25.254.5/download/
ab -c2 -n100 http://172.25.254.5/download/
查看nginx 日志 tail /usr/local/nginx/logs/access.log
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; .允许1秒钟不超过10个请求
limit_conn_zone $binary_remote_addr zone=addr:10m;
location /download {
limit_conn addr 1;
limit_rate 80k; 限制速率
limit_req zone=one burst=5; nodelay 表示最大延迟请求数量不大于5。 如果太过多的请求被限制延迟是不需要的 ,这时需要使用nodelay参数,服务器会立刻返回503状态码。
}
测试
在download 中放一张图片 ajpg
在浏览器所在主机执行
ab -c1 -n10 http://172.25.254.5/download/a.jpg 到
ab -c6 -n10 http://172.25.254.5/download/a.jpg 执行6次
在nginx 端查看日志 每执行一次查看一次日志
执行五次后第六次查看
限制 ip 访问
可以更改 为你想要转的域rewrite ^(.*) http://www.baidu.com; 比如百度
nginx -t reload
再次访问172.25.254.5 就直接是百度了
Https
server {
listen 443 ssl;
server_name localhost;
ssl_certificate cert.pem;
ssl_certificate_key cert.pem; 更改
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
进入 /etc/pki/tls/certs/
执行 make cert.pem
nginx -s reload
更改默认发布目录
创建/www
文件 westos 写入index。html 文件 在主机中添加解析 www.westos.org
重定向
创建默认发布目录 /bbs
echo bbs >/bbs/index.html 中
更改 nginx 配置文件
添加
rewrite /*bbs$ http://bbs.westos.org; 表示www.westos.org/*bbs结尾的域名定向到 bbs.westos.org;
测试在主机中 执行
curl -I www.westos.org/232bbs 以bbs结尾
防盗链
再开启一台虚拟机
scp -rp /usr/local/nginx/ root@172.25.254.1:/usr/local/
在server中 添加 nginx 用户
useradd -s /sbin/nologin nginx
ln -s /usr/local/nginx/sbin/nginx /sbin
vim nginx.conf
nginx
nginx -s reload
创建 /nihao目录
cd /nihao
vim index.html
<html>
<body>
<img src="http://172.25.254.5/download/a.jpg"> 地址
</body>
</html>
在浏览器中主机加解析 或者直接访问ip后加/nihao
在 www.nihao.org 中查看
防盗链
在server五中
更改nginx 配置文件
执行
nginx -t
nginx -s reload
再次在s浏览器中查看