场景二:http服务器
一、配置静态页面
1、直接解压后,在/usr/local/nginx/html/ 下创建个www目录,然后在目录下放一个test.html
2、nginx.conf 中server的配置
http {
server {
#监听端口
listen 80;
#服务名称用于配置域名
server_name localhost;
client_max_body_size 1024M;
# 默认location
location / {
root /usr/local/nginx/html/www;
index index.html index.htm;
}
}
}
3、访问
-
http://localhost/ 指向/usr/local/nginx/html/index.html, index.html 是安装Nginx自带的html
-
http://localhost/test.html指向/usr/local/nginx/html/www/test.html
说明:如果访问出现403,可能是因为#user nobody 被注释,改成对应的组用户,然后重启
location解释:
a)root是根路径,当访问 http://localhost/test.html,"/test.html"会匹配到"/",
找到root /usr/local/nginx/html/www,用户访问的资源是/usr/local/nginx/html/www/test.html
b)index设置是nginx默认的首页,当url直接是http://localhost/ 不走root规则,直接走index规则
Nginx location uri正则表达式规则
-
.:匹配除换行符以外的任意字符
-
?:重复0次或1次
-
+* 重复1次或更多次
-
*:重复0次或更多次
-
\d:匹配数字
-
^:匹配字符串的开始
-
$:匹配字符串的结束
-
{n}:重复n次
-
{n,}:重复n次或更多次
-
[c]:匹配单个字符c
-
[a-z]:匹配a-z小写字母的任意一个
-
(a|b|c):属线表示匹配任意一种情况,每种情况使用竖线分隔,一般使用小括号括括住,匹配符合a字符或是b字符或是c字符的字符串
-
\反斜杠:用于转义特殊字符
小括号()之间匹配的内容,可以在后面通过$1来引用,$2表示的是前面第二个()里的内容。正则里面容易让人困惑的是\转义特殊字符。