nginx编译、优化、配置
**Nginx编译安装**
1 apt install libgd-dev 2 apt install libgeoip-dev 3 apt-get install zlib1g-dev 4 apt install openssl libssl-dev 5 apt install libpcre3-dev 6 apt install libpcre3 7 apt install openssl-devel pcre-devel 8 apt install gcc 9 apt install libgd-dev
1 ./configure --prefix=/apps/nginx --with-http_ssl_module \ 2 --with-http_v2_module \ 3 --with-http_realip_module \ 4 --with-http_addition_module \ 5 --with-http_image_filter_module \ 6 --with-http_geoip_module \ 7 --with-http_gunzip_module \ 8 --with-http_stub_status_module \ 9 --with-http_gzip_static_module \ 10 --with-pcre \ 11 --with-stream \ 12 --with-stream_ssl_module \ 13 --with-stream_realip_module
1 --prefix=/apps/nginx 安装路径 2 --with-http_ssl_module \ https模块 3 --with-http_v2_module \ http版本 4 --with-http_realip_module \ 当本机的nginx处于一个反向代理的后端时获取到真实的用户IP 5 --with-http_addition_module \ 模块是一个过滤器,可在响应之前和之后添加文本 6 --with-http_image_filter_module \ 图片文件 7 --with-http_geoip_module \ 读取IP所在地域信息。 8 --with-http_gunzip_module \ 解压缩 9 --with-http_stub_status_module \ 查看nginx状态信息 10 --with-http_gzip_static_module \ 压缩 11 --with-pcre \ 编译需要 12 --with-stream \ 负载均衡 13 --with-stream_ssl_module \ 支持https的负载均衡 14 --with-stream_realip_module
**nginx默认配置文件优化**
1 #user nobody; 2 worker_processes auto; 3 #绑定CPU 4 worker_cpu_affinity auto 5 #error_log logs/error.log; 6 #error_log logs/error.log notice; 7 #error_log logs/error.log info; 8 9 #pid logs/nginx.pid; 10 11 events { 12 #单个进程最大并发连接数 13 worker_connections 1024; 14 use epoll; 15 #同一时刻只有一个请求避免多个睡眠的work进程被唤醒。 16 accept_mutex on; 17 #同时接受所有新的网络连接 18 multi_accept on; 19 } 20 #最大打开文件数 21 worker_rlimit_nofile 65535; 22 #关闭守护进程 docker 前台执行 23 daemon off; 24 #daemon on; 25 #只使用master 进程作工作进程 测试使用 26 #master_process on|off 27 28 http { 29 #导入支持的文件类型 30 include mime.types; 31 #超出mime.types 文件设置后,设置默认的类型,会提示下载不匹配的类型文件 32 default_type application/octet-stream; 33 34 #可以设置多个log配置 35 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 36 # '$status $body_bytes_sent "$http_referer" ' 37 # '"$http_user_agent" "$http_x_forwarded_for"'; 38 39 log_format app '$remote_addr - $remote_user [$time_local] "$request" ' 40 '$status $body_bytes_sent "$http_referer" ' 41 '"$http_user_agent" "$http_x_forwarded_for"'; 42 #调用log配置 43 #access_log logs/access.log app; 44 45 #实现文件零拷贝,静态文件用 46 sendfile on; 47 #在开启了sendfile的情况下,合并请求后统一发送给客户端,节省网络带宽,会产生延迟。 48 #tcp_nopush on; 49 #在开启了keepalived模式下的连接是否启用TCP_NODELAY选项,当为off时,延迟0.2s发送,默认on时,不延迟发送,立即发送用户响应报文,建议off 50 #tcp_nodelay off; 51 #回话保持时间,第二个参数使客户端访问时可以看到设置的超时时间 52 #keepalive_timeout 0; 53 keepalive_timeout 65 65; 54 #开始文件压缩,降低消耗 55 #gzip on; 56 57 server { 58 listen 80; 59 #正则表达式匹配访问域名,如果没有匹配到全部使用www.admin.com 返回 60 server_name localhost www.(abc\d+)\.admin.com$ www.admin.com; 61 #设置编码格式 62 #charset utf-8; 63 64 #access_log logs/host.access.log main; 65 66 location / { 67 root html; 68 index index.html index.htm; 69 } 70 71 #error_page 404 /404.html; 72 73 # redirect server error pages to the static page /50x.html 74 # 75 #设置报错访问页面,可以加入404 403 等等 76 error_page 500 502 503 504 /50x.html; 77 location = /50x.html { 78 root html; 79 } 80 81 error_page 404 403 /40x.html; 82 location = /40x.html { 83 root html; 84 } 85 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 86 # 87 #location ~ \.php$ { 88 # proxy_pass http://127.0.0.1; 89 #} 90 91 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 92 # 93 #location ~ \.php$ { 94 # root html; 95 # fastcgi_pass 127.0.0.1:9000; 96 # fastcgi_index index.php; 97 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 98 # include fastcgi_params; 99 #} 100 101 # deny access to .htaccess files, if Apache's document root 102 # concurs with nginx's one 103 # 104 #限制访问 105 location ~ /passwd.html { 106 deny all; 107 } 108 #location ~ /\.ht { 109 # deny all; 110 #} 111 }
root:指定web的家⽬录,在定义location的时候,⽂件的绝对路径等于 root+location
alias:定义路径别名,会把访问的路径重新定义到其指定的路径
**语法规则: location [=|~|~*|^~] /uri/ { … }**
= #⽤于标准uri前,需要请求字串与uri精确匹配,如果匹配成功就停⽌向下匹配并⽴即处理请求。
~ #⽤于标准uri前,表⽰包含正则表达式并且区分⼤⼩写,并且匹配
!~ #⽤于标准uri前,表⽰包含正则表达式并且区分⼤⼩写,并且不匹配
~* #⽤于标准uri前,表⽰包含正则表达式并且不区分⼤写,并且匹配
!~* #⽤于标准uri前,表⽰包含正则表达式并且不区分⼤⼩写,并且不匹配
^~ #⽤于标准uri前,表⽰包含正则表达式并且匹配以什么开头
$ #⽤于标准uri前,表⽰包含正则表达式并且匹配以什么结尾
\ #⽤于标准uri前,表⽰包含正则表达式并且转义字符。可以转. * ?等
* #⽤于标准uri前,表⽰包含正则表达式并且代表任意⻓度的任意字符
**Nginx四层访问控制**
1 location /about { 2 alias /data/nginx/html/pc; 3 index index.html; 4 deny 192.168.1.1; 5 allow 192.168.1.0/24; 6 allow 10.1.1.0/16; 7 #IPV6 8 allow 2001:0db8::/32; 9 deny all; 10 }
**Nginx账户认证功能**
centos:
1 location /about { 2 alias /data/nginx/html/pc; 3 index index.html; 4 deny 192.168.1.1; 5 allow 192.168.1.0/24; 6 allow 10.1.1.0/16; 7 #IPV6 8 allow 2001:0db8::/32; 9 deny all; 10 }
ubuntu:
apt install apache2-utils
创建认证用户文件和用户
1 htpasswd -cbm /usr/local/nginx/conf/.htpasswd user1 123456 2 Adding password for user user1 3 htpasswd -bm /usr/local/nginx/conf/.htpasswd user2 123456 4 Adding password for user user2
1 /usr/local/nginx/conf/pc.conf 2 locathon = /login/ { 3 root /usr/local/nginx/html/pc; 4 index index.html; 5 auth_basic "login password"; 6 auth_basic_user_file /usr/local/nginx/conf/.htpasswd; 7 }
**自定义错误页面**
1 listen 80; 2 server_name www.admin.net; 3 error_page 500 502 503 504 404 /error.html; 4 location = /error.html { 5 root html; 6 }
**自定义访问日志**
1 listen 80; 2 server_name www.admin.net; 3 error_page 500 502 503 504 404 /error.html; 4 access_log /usr/local/nginx/logs/www-admin-net_access.log; 5 error_log /usr/local/nginx/logs/www-admin-net_error.log; 6 location = /error.html { 7 root html; 8 }
**监测访问文件是否存在**
1 location /about { 2 root /usr/local/nginx/html/pc; 3 #alias /usr/local/nginx/html/pc; 4 index index.html; 5 #try_files $uri /about/defaut.html; 6 #try_files $uri $uri/index.html $uri.html /about/default.html; 7 try_files $uri $uri/index.html $uri.html = 489; 8 }
**长连接配置**
1 #设置在一个长连接上可以服务的最大请求数目。当达到最大请求数目并结束服务后,连接被关闭。 2 keepalive_requests 3; 3 #开启长连接后,返回客户端的回话保持时间为65秒,单次长连接累计请求书达到指定次数请求或65秒回被断开 4 #后面的65位客户端应答报文中显示的超时时间(可以不设置第二个参数) 5 keepalive_timeout 65 65;
**作为下载服务器配置**
1 location /download { 2 #自动索引功能 3 autoindex on; 4 #计算文件确切大小(单位bytes),off只显示大概大小(单位kb、mb、gb) 5 autoindex_exact_size on; 6 #显示本机时间而非GMT时间 7 autoindex_localtime on; 8 root /data/nginx/html/pc; 9 #限制响应给客户端的传输速率,单位是bytes/second,默认0标识不限速 10 limit_rate 10k; 11 }
**作为上传服务器**
1 #设置允许客户端上传单个文件的最大值,默认值为1M; 2 client_max_body_size 1m; 3 #用于接收每个客户端请求报文的body不分缓冲区大小,默认16k;超过此大小将被暂存到磁盘上的由client_body_temp_path指令所定义的位置 4 client_body_buffer_size size; 5 #设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量,目录名为16进制数字,使用hash之后往前截取1位,2位,2位作为文件名; 6 client_body_temp_path path [level1 [level2 [level3]]]
md5sum /data/nginx/html/pc/index.html
95f6f65f498c74938064851b1bb 96 3d 4 /data/nginx/html/pc/index.html
1级⽬录占1位16进制,即2^4=16个⽬录 0-f
2级⽬录占2位16进制,即2^8=256个⽬录 00-ff
3级⽬录占2位16进制,即2^8=256个⽬录 00-ff
配置⽰例:
1 client_max_body_size 10m; 2 client_body_buffer_size 16k; 3 #reload Nginx会⾃动创建temp⽬录 4 client_body_temp_path /apps/nginx/temp 1 2 2;