搭建静态资源站包括以下几部分:

  1. root指令与alias指令的区别
  2. 使用gzip压缩资源
  3. 如何访问指定目录下的全部资源文件
  4. 如何限制访问流量
  5. 如何自定义log日志

root指令与alias指令的区别

我们的网站静态资源放到  /home/wwwroot/demo 目录下

root@2a33e33fa785:/home/wwwroot/demo# ls
about.html about1.html css fonts gallery.html images index.html js typography.html

nginx.conf 文件

worker_processes  ;

events {
worker_connections ;
} http {
include mime.types;
default_type application/octet-stream; sendfile on; keepalive_timeout ; server {
listen ;
server_name localhost; location /demo/ {
root /home/wwwroot;
       #alias /home/wwwroot/demo/;
} } }

上面的配置文件中 root和alias 指令配置完之后实现的效果是一样的,其实用的区别在于:

  • 使用root指令时,访问 http://ip:端口号/demo/index.html 时,nginx回去root 指定的目录下按照url地址来寻找index.html文件
  • 使用 alias 指令就相当于为 /demo/ 起了个别名 /demo/ 与 alias 指定的目录是等同的所以当同样访问 http://ip:端口号/demo/index.html 时,nginx 获取 alias 指定的目录下寻找 index.html 文件

使用gzip压缩

#开启gzip
gzip on;
#低于1kb的资源不压缩
gzip_min_length 1k;
#压缩级别【-】,越大压缩率越高,同时消耗cpu资源也越多,建议设置在4左右。
gzip_comp_level ;
#需要压缩哪些响应类型的资源,多个空格隔开。不建议压缩图片,下面会讲为什么。
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;

使用前 index.html 的请求大小

开启gzip之后

访问指定目录下的全部资源文件

在 server 或者 http 或者 location 指令中 加入  autoindex on;  指令

限制访问流量

添加  set $limit_rate 1k 限制请求每秒只能传输1kB数据,这时我们访问页面会明显感觉到很慢

设置log日志

设置日志格式     log_format 模板名称 日志中包含的内容  注意:模板中所保存的内容可以是nginx模块及第三方模块提供的任意参数内容,例如 这里 提供的变量都可以存储起来

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 日志路径 模板名称;

access_log  logs/host.access.log  main;

最后是完整的配置文件

worker_processes  ;

events {
worker_connections ;
} 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"'; sendfile on; keepalive_timeout ; gzip on;
gzip_min_length 1k;
gzip_comp_level ;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css; server {
listen ;
server_name localhost; access_log logs/host.access.log main; location / {
root /home/wwwroot/demo/;
autoindex on;
set $limit_rate 1k;
} } }

搭建nginx静态资源站的更多相关文章

  1. 用node.js搭建一个静态资源站 html,js,css正确加载 跳转也完美实现!

    昨天买了一个服务器想着用来测试一些自己的项目,由于是第一次建站,在tomcat,linux,node.js间想了好久.最终因为node搭建比较方便没那么麻烦就决定用node.js来搭建网站项目. 搭建 ...

  2. Nginx——静态资源服务器(一)

    java web的项目中,我们经常将项目部署到Tomcat或者jetty上,可以通过Tomcat或者jetty启动的服务来访问静态资源.但是随着Nginx的普及,用Nginx来作为静态资源服务器,似乎 ...

  3. 清除nginx静态资源缓存

    之前写过一篇如何配置nginx缓存及手动清除缓存的文章: http://www.cnblogs.com/Eivll0m/p/4921829.html 但如果有大量缓存需要清理,手动一条条清理就比较慢了 ...

  4. Nginx 静态资源缓存配置

    示例 # Media: images, icons, video, audio, HTC location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|m ...

  5. nginx静态资源设置缓存的方法

    nginx静态资源设置缓存的方法 直接加expires 30d; 就是就可以了 缓存时间30天完整如下 <pre> location / { root /home/www/wordpres ...

  6. 06 . Nginx静态资源缓存

    Nginx静态资源 Nginx可以处理静态资源 非Web服务器可以运行处理而生成的文件,即服务器只需要从硬盘或者缓存中读取然后直接给客户端响应即可. 常见的静态资源 # 浏览器渲染: html文件,样 ...

  7. 008.Nginx静态资源

    一 Nginx静态资源概述 1.1 静态资源类型 Nginx作为静态资源Web服务器部署配置, 传输非常高效, 常常用于静态资源处理,请求以及动静分离.通常非服务器动态运行生成的文件属于静态资源. 类 ...

  8. nginx系列3:搭建一个静态资源web服务器

    搭建静态资源web服务器 1,创建静态页面 在nginx的安装目录(/usr/local/nginx)下创建文件夹webapplications/helloworld,然后创建一个名为index.ht ...

  9. 基于CentOS搭建Nginx 静态网站

    系统要求: CentOS 7.2 64 位操作系统 一. 安装 Nginx(在 CentOS 上,可直接使用 yum 来安装 Nginx) yum install nginx -y 安装完成后,使用 ...

随机推荐

  1. Windows10下Anaconda虚拟环境下安装pycocotools

    0 - 步骤 安装visualcppbuildtools_full.exe(链接:https://blog.csdn.net/u012247418/article/details/82314129) ...

  2. delphi 根据特殊符号字符获取字符串前或后的字符

    function GetBefore(substr, str:string):string; {©Drkb v.3(2007): www.drkb.ru, ®Vit (Vitaly Nevzorov) ...

  3. 学习 TTreeView [15] - 连接数据库 (作为给 "丁永其" 和 "DELPHI万岁" 两位朋友的回复)

    本例效果图: unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Contro ...

  4. 【Leetcode_easy】788. Rotated Digits

    problem 788. Rotated Digits solution1: class Solution { public: int rotatedDigits(int N) { ; ; i< ...

  5. 第一次linux下安装nginx记录

    CentOS 7 安装Nginx 并配置自动启动 1.下载Nginx安装包---->地址:http://nginx.org/en/download.html 2.上传安装包到服务期 : rz 命 ...

  6. C#数字除法

    C#中计算double a=1/1000:应该结果是0.001,但为什么会变成0呢? C# 中 如果相除的两个数都是整数(int 型) 那么除的结果就是只取整数部分 所以你才会取到0 如果你要取精确的 ...

  7. [转帖]linux常用命令大全(linux基础命令入门到精通+实例讲解+持续更新+命令备忘录+面试复习)

    linux常用命令大全(linux基础命令入门到精通+实例讲解+持续更新+命令备忘录+面试复习) https://www.cnblogs.com/caozy/p/9261224.html 总结的挺好的 ...

  8. Fiddler之打断点

    1..Fiddler可以修改以下请求 --Fiddler设置断点,可以修改HTTP请求头信息,如修改Cookie,User-Agent等 --可以修改请求数据,突破表单限制,提交任意数字,如充值最小1 ...

  9. MEAN: AngularJS + NodeJS的REST API开发教程

    Node.JS https://www.jdon.com/idea/nodejs/web-app-with-angularjs-and-rest-api-with-node.html Mean是一个热 ...

  10. 使用redis和thread告警并避免重复告警

    spring cloud使用redis 增加监控,微服务的监控体系特别重要,这里增加了告警(使用thread异步告警),同时不能短时间内不能重复告警(使用redis避免重复) 1.增加依赖       ...