nginx 进阶配置
nginx-location使用
location语法
location使用的语法例子为: location [=|~|~*|^~] uri{} 对location语法列表说明。 location [=|~|~*|^~] uri {..}. # 指令 #匹配标识 #匹配的网站网址站 #匹配URI后要执行的配置段 上述语法中的URI部分是关键,这个URI可以是普通的字符串地址路径或者是正则表达式,当匹配成功则执行后面大括号里面的相关指令。正则表达式的前面还可以有~或~*等特殊的字符。 这两种特殊字符~或~*匹配的区别为: " ~ " 用于区分大小写(大小写敏感)的匹配;~/images{} " ~* " 用于不区分大小写的匹配。还可以用逻辑操作符!对上面的匹配取反,即 !~ 和!~* 。 " ^~ " 作用是在常规的字符串匹配检查之后,不做正则表达式的检查,即匹配最明确的那个字符
location = / { ##www.etiantian.org www.etiantian.org/ www.etiantian.org/index.html [ configuration A ] } location / { ##默认情况,无路可走 jd.com/hello/oldboy.html [ configuration B ] } location /documents/ { ##uri里面有/documents oldboyedu.com/documents/dsfsadfasdf.jpg [ configuration C ] } location ^~ /images/ { [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { ##~* 不区分大小写的正则匹配,以.gif或.jpg或.jpeg结尾 [ configuration E ] }
实现配置
[root@www extra]# cat www.conf server { listen 80; server_name www.etiantian.org etiantian.org; root html/www; location / { return 401; } location = / { return 402; } location /documents/ { return 403; } location ^~ /images/ { return 404; } location ~* \.(gif|jpg|jpeg)$ { return 500; } access_log logs/access_www.log main ; }
[root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null 10.0.0.8 402 [root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null http://10.0.0.8/ 402 [root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null http://10.0.0.8 402 [root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null http://10.0.0.8/oldboy 401 [root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null http://10.0.0.8/index.html 401 [root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null http://10.0.0.8/documents/ 403 [root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null http://10.0.0.8/documents/1.jpg 500 [root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null http://10.0.0.8/images/1.jpg 404
URI及特殊字符组合匹配顺序 匹配说明 第一名:"location = / {}" 精确匹配/ 第二名:"location ^~ /images/ {}" 匹配常规字符串,不做正则匹配检查, 优先匹配路径 第三名:"location ~* \.(gif|jpg|jpeg)$ {}" 正则匹配 第四名:"location /documents/ {}" 匹配常规字符串,如果有正则则优先匹配正则。 第五名:"location / {}" 所有location都不能匹配后的默认匹配
nginx-rewrite 短域名跳转
lewen.com ====>www.lewen.com lewen.com 变换为 www.lewen.com server { listen 80; server_name lewen.com; rewrite ^/(.*) http://www.lewen.com/$1 permanent; } lewen.com/index.html ==== www.lewen.com/index.html rewrite配置 [root@web01 extra]# cat www.conf server { listen 80; server_name etiantian.org; rewrite (^.*) http://www.etiantian.org/$1 permanent; } server { listen 80; server_name www.etiantian.org ; access_log logs/access_www.log main; location / { root html/www; index index.html index.htm; } }
测试
[root@web01 extra]# curl -L etiantian.org/index.html web01 www.etiantian.org Http状态码301和302概念简单区别及企业应用案例 http://oldboy.blog.51cto.com/2561410/1774260
Nginx status
[root@www extra]# yum install httpd-tools -y [root@www extra]# which htpasswd /usr/bin/htpasswd
设置密码 htpasswd -bc /application/nginx/conf/htpasswd oldboy 123456 chmod 400 /application/nginx/conf/htpasswd chown www /application/nginx/conf/htpasswd
##status.conf server { listen 80; server_name status.etiantian.org; location / { stub_status on; # 开启状态信息 access_log off; auth_basic "closed site"; auth_basic_user_file /application/nginx/conf/htpasswd; #密码文件的路径或者直接用绝对路径 } }
登录验证
[root@web01 extra]# curl -u oldboy status.lewen.com Enter host password for user 'oldboy': <html> <head><title>403 Forbidden</title></head> <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.14.0</center> </body> </html> [root@web01 extra]# curl -u oldboy:123456 status.lewen.com Active connections: 1 server accepts handled requests 46 46 80 Reading: 0 Writing: 1 Waiting: 0
Apache select和Nginx epoll模型区别形象比喻(面试常考);
虚拟主机概念及类型分类详解;
Nginx错误及访问日志及访问日志切割;
Nginx location介绍及配置实践;
Nginx Rewrite介绍及配置实践;
Nginx Web访问认证介绍及配置实践。