用Nginx搭建一个可用的静态资源web服务器

[root@Alen conf]# vim nginx.conf[root@Alen conf]# cat nginx.confhttp { # log_format 关键字定义日志的格式 # $remote_addr 源端的IP地址 # $time_local 当前的时间 include mime.types; default_type application/octet-stream; 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/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; # 打开gzip功能 gzip on; # 小于1字节后不再压缩 gzip_min_length 1; # 设定压缩级别 gzip_comp_level 2; # 设定压缩类型 gzip_types text/plain application/x-javascript text/css application/xml text/javascriptapplication/x-httpd-php image/jpeg image/gif image/png; server { # 监听端口 listen 800; # 绑定域名 server_name test.local.com; #charset koi8-r; #access_log logs/host.access.log main; location / { alias local/; #root html; #index index.html index.htm; # autoindex功能打开后,当访问以“/”结尾的URL时,会自动显示该目录的结构 autoindex on; # 因为我们的公网带宽时非常有限的,当有许多用户同时访问我们的网站时,他们会形成竞争关系。 # 为了限制他们访问大文件的速度,以期望能够分离出足够的带宽给用户访问必要的小文件,如js、css # 这时,可使用set命令配合一些内置的变量来实现这样的功能 set $limit_rate 1k; } }}[root@Alen conf]# cd ..[root@Alen nginx]# ./sbin/nginx -tnginx: the configuration file /opt/nginx/conf/nginx.conf syntax is oknginx: configuration file /opt/nginx/conf/nginx.conf test is successful# 创建/opt/nginx/local目录,并在其下任意创建两个文件[root@Alen nginx]# mkdir local[root@Alen nginx]# echo "hahahah" > local/test1[root@Alen nginx]# echo "xixixixi" > local/test2# 重载配置文件[root@Alen nginx]# ./sbin/nginx -s reload# 实验所用域名并非公网域名,故而要先在hosts文件中进行指定[root@Alen nginx]# echo "192.168.5.4 test.local.com" >> /etc/hosts# 使用curl命令测试网站是否能正常访问# 可看到两个文件的状态[root@Alen nginx]# curl test.local.com:800<html><head><title>Index of /</title></head><body><h1>Index of /</h1><hr><pre><a href="../">../</a><a href="test1">test1</a> 12-Feb-2020 09:04 8<a href="test2">test2</a> 12-Feb-2020 09:05 9</pre><hr></body></html># 测试访问两个文件[root@Alen nginx]# curl test.local.com:800/test1hahahah[root@Alen nginx]# curl test.local.com:800/test2xixixixi

相关文章