实验环境
我们在一个配置好nginx的虚拟机上,在/usr/local/nginx/html里面写入了index.html,在网页上可以访问成功,访问网页的大小为200多k
配置文件
nginx实现对网页的压缩,需要修改niginx的配置文件。我们首先了解一下nginx的配置文件都有什么内容:
#1.定义Nginx运行的用户和用户组为nginx,指的是worker的工作控制组是nginx用户,是为了保证系统的安全性。
user nginx nginx;
#2.进程数,即处理请求的进程(熟称负责接客的服务员),初始可设置为cpu总核数如:worker_processes 8;
worker_processes 2;
#3.全局错误日志定义类型
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#4.把进程号记录到文件,用于管理nginx进程
#pid logs/nginx.pid;
#5.Nginx的事件模型中的最大打开文件数,可设置为系统优化后的ulimit-HSn的结果,
即1024可改为65535,然后在/etc/security/limits.conf文件中修改,因为该文中是默认的nginx的连接数,
只需在该文件中修改即可
events {
worker_connections 1024; #nginx的最大连接数=worker连接数*worker进程数
}
#6.http模块的设置部分
http {
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; #开启高效文件传输模式,实现内核零copy
#tcp_nopush on; #激活tcp_nopush参数可以允许把httpresponseheader和文件的开始放在一个文件里面发布,减少网络报文段的数量
#keepalive_timeout 0; #连接超时时间,单位是秒
keepalive_timeout 65;
#gzip on; #gzip压缩,对于网站优化极其重要
#7.设置基于域名的虚拟主机部分
server {
listen 80; #监听的端口,也可以是172.25.254.1:80形式
server_name localhost; #域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / { #默认访问的location标签段
root html; #站点根目录,即网站程序存放的目录
index index.html index.htm; #首页排序
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ { #符合php扩展名的请求调度到fcgi server
# root html;
# fastcgi_pass 127.0.0.1:9000; #抛给本机的9000端口(php fastcgi server)
# fastcgi_index index.php; #设定动态首页
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params; #设定和fastcgi交互的参数包含文件
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
#8.http虚拟主机
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
开始压缩
我们查看一下网页,看它支持什么压缩。网页类型
我写的网页是支持gzip的,所以我修改的是配置文件里面gzip部分的内容
gzip on; #开启gzip压缩功能
gzip_min_length 1; #最小压缩文件为0.1k
gzip_comp_level 3; #压缩级别为3
gzip_types text/plain appliaton/x-javascript text/css application/xml text/javascripts application/x-httpd-php image/jpeg imgae/gif image/png; #压缩文件类型
然后查看我写的配置文件的语法是否正确
/usr/local/nginx/sbin/nginx -t
当它显示is ok,successful的时候代表语法没有错误,如果有错误的话它会具体提示是哪里出了错误
然后重启服务
systemctl restart nginx和systemctl reload nginx都可以重启
在网页上访问
在这里要提醒一下:重新访问网页的时候必须要(ctrl+shift+delete)清掉之前的缓存,不然查看不到效果。