使用nginx搭建静态资源web服务器

1.修改配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf	
#在http中添加以下内容
        server {				#定义一个虚拟主机
        gzip off;				#on代表开启压缩,off代表关闭压缩
        gzip_min_length 1;			#少于1字节不压缩
        gzip_comp_level 2;			#压缩级别为2
        gzip_types image/jpeg;			#压缩类型是jpeg格式的
        set $limit_rate 1k; 			#限制速度为1k
        listen 8080;				#监听在80端口
        server_name www.yu.com;   		#域名为www.yu.com
        error_log logs/yu.com.errlog;		#错误的日志文件
        access_log logs/yu.com.acclog;  	#日志文件
        location / { 				#定义一个location
            root /data/www;			#路径为/data/www/index.html
            index index.html; 
                   }   
    
        location /public {			#定义一个location用于文件共享
                root /data/www;			#路劲为/data/www/public
                autoindex on; 			#开启共享功能
                        }           
                }   

创建/data/www/index.html文件

[root@localhost ~]# vim /data/www/index.html 
#文件内容仅供参考
<!DOCTYPE html>
<html>

<head>
<title>wow!</title>
</head>

<body>
<p>first web</p>
<p>dear wu</p>
<img src="wu.jpg">
</body>

</html>

[root@localhost ~]# yum -y install lrzsz	#安装一个上传图片的实用工具
[root@localhost ~]# cd /data/www/		#切换到index.html文件的目录
[root@localhost ~]# rz				#命令行输入rz,会弹出窗口,选择图片即可

3.测试(记得关闭seliunx与开启防火墙端口)

打开谷歌浏览器,输入www.yu.com:8080,即可看到对应页面
可以使用谷歌的开发者工具对压缩,传输速率的限制进行测试。

你可能感兴趣的:(使用nginx搭建静态资源web服务器)