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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
| 1、检查nginx基本依赖包是否安装pcre-devel openssl-devel rpm -qa pcre-devel pcre rpm -qa openssl-devel openssl 若没有执行以下命令安装 yum -y install pcre-devel pcre yum -y install openssl-devel openssl yum -y install gcc-c++ 2、安装nginx [ ! -d /home/ropon/tools ] && mkdir -p /home/ropon/tools cd /home/ropon/tools id -u www >/dev/null 2>&1 [ $? -ne 0 ] && useradd www -s /sbin/nologin -M -g www wget -c http://nginx.org/download/nginx-1.10.3.tar.gz tar xvf nginx-1.10.3.tar.gz cd nginx-1.10.3 ./configure --user=www --group=www --prefix=/usr/local/nginx-1.10.3/ \ --with-http_stub_status_module --with-http_ssl_module make make install ln -s /usr/local/nginx-1.10.3 /usr/local/nginx 便于后期升级nginx版本,及快速获取nginx版本号 --prefix=设置安装路径 --user=进程用户权限 --group=进程用户组权限 --with-http_stub_status_module 激活状态信息 --with-http_ssl_module 激活ssl功能 3、nginx.conf配置文件说明 worker_processes 4; events { worker_connections 10240; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location /ropon_ngx_status { stub_status on; access_log off; allow 1.1.1.1; deny all; } } } Active connections: 4 server accepts handled requests 209 209 34 启动共处理多少连接;启动共创建多少次握手(请求丢失数(握手数-连接数)); 共处理多少次请求 Reading: 0读取客户端header信息数 Writing: 1返回客户端header信息数 Waiting: 3等待下次请求驻留连接 error_log logs/error.log warnerrorcrit; 可放置标签段:main,http,server,location log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; $remote_addr $http_x_forwarded_for $remote_user $time_local $request $status $body_bytes_sent $http_referer $http_user_agent access_log logs/access.log main(默认日志记录格式); 可放置标签段:http,server,location,if in location,limit_except 1.1.1.1 - - [23/Nov/2017:10:37:45 +0800] "GET /ropon.html HTTP/1.1" 404 571 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" "-" remote_addr 1.1.1.1 remote_user - time_local 23/Nov/2017:10:37:45 +0800 request GET /ropon.html HTTP/1.1 status 404 body_bytes_sent 57字节 http_referer 直接访问故记录- http_user_agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36 http_x_forwarded_for 没有使用CDN故记录- 深入了解日志参数[高并发避免日志频繁写入影响IO性能] access_log path format gzip [=level] [buffer=size] [flush=time] [if=condition]; access_log logs/access.gz main gzip buffer=32k flush=5s; 4、nginx日志切割 mkdir -p /usr/local/script vi cut_del_logs.sh
LOGS_PATH=/home/wwwlogs/default.gz YESTERDAY=$(date -d "yesterday" +%Y-%m-%d) mv ${LOGS_PATH}/access.gz ${LOGS_PATH}/access_${YESTERDAY}.gz kill -USR1 `ps axu grep "nginx: master process" grep -v grep awk '{print $2}'`
cd ${LOGS_PATH} find . -mtime +7 -name "*20[1-9][3-9]*" xargs rm -f exit 0 crontab -e 1 0 * * * /usr/local/script/cut_del_logs.sh 5、配置nginx虚拟主机 mkdir /usr/local/nginx/conf/vhost mkdir -p /home/wwwroot/default include vhost/*.conf; cd /usr/local/nginx/conf/vhost && vi 000default.conf server { listen 80; server_name 1.1.1.1 location / { root /home/wwwroot/default; index index.html; } } 6、location的使用 location [ = ~ ~* ^~ ] uri { } ~ 区分大小写 ~* 不区分大小写 !~ !~* 取反 ^~ 做常规字符串检查,不做正则匹配。 location = / { [ configuration A ] } location / { [ configuration B ] } location /files/ { [ configuration C ] } location ^~ /images/ { [ configuration D ] } location ~* \.(gifjpg)$ { [ configuration E ] } eg: location = / { return 401; } location /files/ { return 402; } location ^~ /images/ { return 403; } location ~* \.(jpg)$ { return 405; } 访问http://1.1.1.1 或http://1.1.1.1/ 出现401状态码 访问http://1.1.1.1/files/ 出现402状态码 访问http://1.1.1.1/images/ssfasdfsda或http://1.1.1.1/images/ 都会出现403状态码 访问http://1.1.1.1/dfasfsdfsd/sfdasdfsd.jpg或http://1.1.1.1/324123423.jpg 都会出现405状态码 =/是精确匹配优先级最高与放置顺序无关 /是默认匹配,指没有匹配上其他location,最后匹配默认 /files/是路径匹配 ^~ /images/也是路径匹配,加了特殊字符^~,/files/11.jpg 优先匹配路径 ~* \.(jpg)$是扩展名匹配 7、Nginx rewrite 语法:rewrite regex(正则表达式) replacement [flag]; 可放置标签段:server,location,if rewrite ^/(.*) http://1.1.1.1/$1 permanent; ^/(.*)匹配所有 匹配成功后跳转到http://1.1.1.1/$1 $1代表前面^/(.*)具体内容 permanent是永久重定向标记 \ 转义字符 \\ \$ ^匹配字符串起始位置 $匹配字符串结束位置 *匹配前面字符零次或多次,比如ro* 可匹配r、roo,*相当于{0,} +匹配前面字符一次或多次,比如ro+可匹配ro、roo,但不能匹配r,+相当于{1,} ?匹配前面字符零次或一次,比如lp(ro)?可匹配lp、lpro中lp,?相当于{0,1} .匹配除“\n”之外的任何单字符,若要匹配\n,可使用[.\n] (pattern)匹配括号内的pattern,并且后面可获取对应匹配,用$0,$1..$9, 若要匹配括号字符,使用\( \) last 本条规则匹配完,继续匹配下条location uri规则 break 本条规则匹配完,就终止不再继续匹配 redirect 返回302临时重定向 permanent 返回301永久重定向 例子 if ( $http_host ~* "^(.*)\.idiyrom\.com$") { set $temp $1; rewrite ^(.*) http://www.idiyrom.com/$temp/ropon.html; break; } 实现的是xxx.idiyrom.com 跳转到 www.idiyrom.com/xxx/ropon.html 8、try_files最核心的功能是可以替代rewrite 语法: try_files file ... uri 或 try_files file ... = code try_files $uri $uri/ /?$args; 按顺序检查文件或文件夹是否存在,返回第一个找到的文件或文件夹。 结尾的斜线表示为文件夹 比如$uri/ 如果所有的文件或文件夹都找不到,会进行一个内部重定向到最后一个参数/?$args。 eg: location / { try_files $uri $uri/ /?$args; } 比如访问http://west.idiyrom.com/imgaes/ $uri 指的是文件,比如images文件,$uri=/imgaes/ $uri/ 指的是文件夹,比如images文件夹 如果文件或文件夹都不存在,就会内部重定向到/?$args,其中$args是get方法获取的参数,比如images/test?testcanshu 日志格式:'[$cookie_customerID_cookie_flag] [$args]' '($uri)' [-] [testcanshu](/index.html) $args=testcanshu 9、Nginx访问认证 auth_basic 语法:auth_basic stringoff; 可放置标签段:http,server,location,limit_except auth_basic_user_file file; 可放置标签段:http,server,location,limit_except htpasswd -bc /usr/local/nginx/conf/htpasswd ropon 123456 chmod 400 /usr/local/nginx/conf/htpasswd chown www /usr/local/nginx/conf/htpasswd location = /ropon/ { auth_basic ropon_test; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } 10、添加启动项 cat /etc/init.d/nginx …… nginx_install_dir=/usr/local/nginx [ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=$nginx_install_dir/sbin:\$PATH" >> /etc/profile [ -n "`grep ^'export PATH=' /etc/profile`" -a -z "`grep $nginx_install_dir /etc/profile`" ] && sed -i "s@^export PATH=\(.*\)@export PATH=$nginx_install_dir/sbin:\1@" /etc/profile . /etc/profile wget -O /etc/init.d/nginx http://mirrors.ropon.top/shell/lnmp/init.d/Nginx-init-CentOS.txt chmod +x /etc/init.d/nginx chkconfig --add nginx chkconfig nginx on 自动安装shell脚本:http://mirrors.ropon.top/shell/lnmp/nginx.sh 附Rewriterule参数详解 1) R 强制外部重定向,后面可以代301或302跳转。 2) F 禁用URL,返回403HTTP状态码。 3) G 强制URL为GONE,返回410HTTP状态码。 4) P 强制使用代理转发。 5) L 表明当前规则是最后一条规则,停止分析以后规则的重写。 6) N 重新从第一条规则开始运行重写过程。 7) C 与下一条规则关联。 8) T=MIME-type(force MIME type) 强制MIME类型。 9) NS 只用于不是内部子请求。 10) NC 不区分大小写。 11) QSA 追加请求字符串。 12) NE 不在输出转义特殊字符。
|