一、访问路径
在nginx项目编译好了之后,nginx.config文件的location配置中,有一行:
location /a {
root www;
index index.html index.htm;
}
这里的root是设定请求的访问路径的,当访问http://localhost/时,会读取www/index.html文件。
除了root可以指定目录路径之外,还有alias,alias的使用方法如下:
location /a {
#autoindex on;
alias www/;
index index.html index.htm;
}
使用alias时,注意后面的路径要加/,并且在请求时要指定具体的请求文件,比如:http://localhost/index.html。
这两者之间的区别是:
-
root指定是不需要加/,并且请求是不需要指定具体的请求文件名,
-
使用root时,浏览器地址是http://localhost/a,那么实际的请求会是www下的文件,而alias是请求www/a下的文件。
二、文件压缩
文件压缩是在nginx.conf中配置以下代码:
gzip on;
gzip_min_length 1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-x-httpd-php image/jpeg image/gif image/png;
这里只是一个案例,具体的参数根据实际情况而定。
-
gzip:打开或者关闭gzip压缩。
-
gzip_min_length:压缩的最小的字节数,超过多少字节才进行压缩。
-
gzip_comp_level:压缩的等级1-9,9为最小压缩,传输最快,但是非常消耗cpu,一般建议使用5。
-
gzip_types:要进行压缩的文件格式。
三、可访问文件夹
支持用户可访问目录的方法也很简单,在location中增加autoinde on;这个属性就可以了。
四、限定带宽
当用户访问量比较大时,我们需要限制带宽,留一定的带宽给用户可以访问其他的小文件。
使用set $limit_rate 1k;可以设定用户访问的带宽为1k。
五、记录日志
在nginx.conf中有关闭记录日志的代码,只不过没有打开:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/host.access.log main;
log_format指定了日志的格式,并且给它起了一个名字叫main,因为有可能我们想要根据不同的功能使用不用的日志格式,所以要起一个名字方便区分。
access_log指定日志的记录位置和使用的格式。
log_format的参数:
- $remote_addr:用户的ip地址
- $remote_user:用户名
- $time_local:访问的时间,是这种格式的:[15/Oct/2021:19:40:14 +0800]
- $request:请求行,例如:"GET / HTTP/1.1"
- $status:响应的状态
- $body_bytes_sent:传输的字节数
- $http_referer:请求的referer
- $http_user_agent:请求的浏览器版本信息
- $http_x_forwarded_for:请求的x-forwarded-for信息