Nginx-使用Expires缓存静态文件

Nginx-使用Expires缓存静态文件

The ngx_http_headers_module module allows adding the “Expires” and “Cache-Control” header fields, and arbitrary fields, to a response header.

1
2
3
4
Syntax: expires [modified] time;
expires epoch | max | off;
Default: expires off;
Context: http, server, location, if in location

缓存静态文件

可以用以下的时间单位:
ms: 毫秒
s: 秒
m: 分钟
h: 小时
d: 天
w: 星期
M: 月 (30 天)
y: 年 (365 天)

Example Configuration

1
2
3
4
5
6
7
8
expires 24h;
expires modified +24h;
expires @24h;
expires 0;
expires -1;
expires epoch;
expires $expires;
add_header Cache-Control private;

The max parameter sets “Expires” to the value “Thu, 31 Dec 2037 23:55:55 GMT”, and “Cache-Control” to 10 years.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
expires -1;
}
# Feed
location ~* \.(?:rss|atom)$ {
expires 1h;
}
# Media: images, icons, video, audio, swf, flv
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp3|mp4|mmf|ogg|ogv|webm|wma|wmv|asf|swf|flv)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
# CSS and Javascript
location ~* \.(?:css|js)$ {
expires 1y;
access_log off;
}
# WebFonts
location ~* \.(?:ttf|ttc|otf|eot|woff|woff2)$ {
expires 1M;
access_log off;
}

修改实例

使用360奇云测给网站打分,看到如下提示:

1
2
3
4
指标描述:
CSS、JS、图片资源都应该明确的指定一个缓存时间,避免了接下来的页面访问中不必要的HTTP请求
评分规则:
如果有静态文件没有设置缓存,将会得到警告

查看请求favicon.ico文件的响应头:

使用 curl -I 命令只查看请求的响应头:

1
2
3
4
5
6
7
8
9
10
$ curl -I http://wang123.net/favicon.ico
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Thu, 13 Apr 2017 02:31:38 GMT
Content-Type: image/x-icon
Content-Length: 4286
Last-Modified: Mon, 20 Mar 2017 15:01:10 GMT
Connection: keep-alive
ETag: "58cfeeb6-10be"
Accept-Ranges: bytes

修改Nginx配置文件,然后重启服务:

在Nginx配置文件中增加以下配置,设置静态文件的缓存时间:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 编辑服务器配置文件
$ vi /usr/local/webserver/nginx/conf/wang123_net.conf
# CSS, Javascript, images, icons, video, audio,
location ~* \.(?:css|js|jpg|jpeg|gif|png|ico|svg|svgz|mp3|mp4|ogg|ogv|webm|wma|wmv|swf|flv)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
//重启服务:
$ sudo /usr/local/webserver/nginx/nginx -t
nginx: the configuration file /usr/local/webserver/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/webserver/nginx/nginx.conf test is successful
$ sudo /usr/local/webserver/nginx/nginx -s reload

重新查看请求favicon.ico文件的响应头:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ curl -I http://wang123.net/favicon.ico
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Thu, 13 Apr 2017 02:29:58 GMT
Content-Type: image/x-icon
Content-Length: 4286
Last-Modified: Mon, 20 Mar 2017 15:01:10 GMT
Connection: keep-alive
ETag: "58cfeeb6-10be"
Expires: Sat, 13 May 2017 02:29:58 GMT
Cache-Control: max-age=2592000
Cache-Control: public
Accept-Ranges: bytes

可以看到,返回结果中已经存在了 Expires、Cache-Control,说明我们配置成功。
重新使用360奇云测给网站打分, 可以看到就不会再有这个警告了。

Reference

http://nginx.org/en/docs/http/ngx_http_headers_module.html
https://github.com/h5bp/server-configs-nginx
http://www.cnblogs.com/lpfuture/p/5798811.html

[END]

Hexo-Sitemap

hexo-generator-sitemap

Github: https://github.com/hexojs/hexo-generator-sitemap

Generate sitemap.

Install

1
$ npm install hexo-generator-sitemap --save

Example:

1
2
3
npm install hexo-generator-sitemap --save
hexo-site@0.0.0 /Users/WangTom/Code/Hexo/Hexo-Website
└── hexo-generator-sitemap@1.1.2

Options

You can configure this plugin in _config.yml.

1
2
3
sitemap:
path: sitemap.xml
template: ./sitemap_template.xml

path - Sitemap path. (Default: sitemap.xml)

template - Custom template path. This file will be used to generate sitemap.xml (See default template)

Excluding Posts

Add sitemap: false to the post’s front matter.

参考链接:

https://github.com/hexojs/hexo-generator-sitemap

Hexo-快速入门指南

什么是 Hexo?

Hexo 是一个快速、简洁且高效的博客框架。Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页。

Hexo出自台湾大学生 tommy351 之手,是一个基于 Node.js 的静态博客程序.

安装前提

安装 Hexo 相当简单。然而在安装前,您必须检查电脑中是否已安装下列应用程序:

Node.js
Git

如果您的电脑中已经安装上述必备程序,那么恭喜您!接下来只需要使用 npm 即可完成 Hexo 的安装。

$ npm install -g hexo-cli

$ hexo clean

$ hexo generate

$ hex server

重启hexo server:

由于Nodejs服务器不支持热更新,需要使用 Ctrl+C 关闭服务器,再重启:

1
2
3
$ hexo server
INFO Start processing
INFO Hexo is running at http://localhost:4000/. Press Ctrl+C to stop.

Git deployer plugin for Hexo.

Installation

1
$ npm install hexo-deployer-git --save

Options

You can configure this plugin in _config.yml.

1
2
3
4
5
6
7
8
9
10
# You can use this:
deploy:
type: git
repo: <repository url>
branch: [branch]
message: [message]
name: [git user]
email: [git email]
extend_dirs: [extend directory]
ignore_hidden: false # default is true

[END]

IT开发常用网址导航

资讯

伯乐在线: http://blog.jobbole.com/

社区

开源中国社区: http://www.oschina.net/
cnodejs: http://cnodejs.org/

前端开发

nodejs: https://nodejs.org/en/

MDN https://developer.mozilla.org/en-US/docs/Web
MDN-Http https://developer.mozilla.org/en-US/docs/Web/HTTP
MDN-JavaScript https://developer.mozilla.org/en-US/docs/Web/JavaScript

后端开发

Laravel文档: https://laravel.com/docs

CodeIgniter文档: https://www.codeigniter.com/user_guide/index.html#
CodeIgniter文档(中文): https://codeigniter.org.cn/

工具

菜鸟教程 http://www.runoob.com/
易百教程 http://www.yiibai.com/
w3school http://www.w3school.com.cn/

OSChina 在线工具: http://tool.oschina.net/

PHP Laravel-Homestead开发环境安装与配置

PHP Laravel-Homestead 开发环境安装与配置

Laravel Homestead 是一个官方预载的 Vagrant「封装包」,提供你一个美好的开发环境,不需要在本机端安装 PHP、HHVM、网页服务器或任何服务器软件。
Homestead 可以在任何 Windows、Mac 或 Linux 上面运行,里面包含了 Nginx 网页服务器、PHP 5.6、MySQL、Postgres、Redis、Memcached等软件,
还有所有你要开发精彩的 Laravel 应用程序所需的软件。

本机系统环境:

1
2
3
4
5
6
7
8
9
//使用 uname 查看当前操作系统名称
$ uname -mnprs
Darwin YONG-TEST.local 15.6.0 x86_64 i386
//使用sw_vers 查看 macOS版本信息
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.11.6
BuildVersion: 15G31

不使用用Homestead,直接在本机安装:

使用homebrew安装PHP7:

$ brew update
$ brew search php70
homebrew/php/php70
homebrew/php/php70-gmagick
homebrew/php/php70-maxminddb
homebrew/php/php70-pdo-pgsql
homebrew/php/php70-stats
homebrew/php/php70-amqp

$ brew install homebrew/php/php70

配置php-fpm端口为9002, 因为本机已经安装了php5, 占用了9001端口。

配置Nginx配置文件:

重启Nginx服务器。

安装Homestead Vagrant Box:

下载 Homestead:

1
2
3
4
5
6
$ git clone https://github.com/laravel/homestead.git Homestead
$ cd Homestead/
$ ls
CHANGELOG.md Vagrantfile composer.lock init.bat readme.md /src
LICENSE.txt composer.json homestead init.sh /scripts
$ bash init.sh

下载 Homestead Vagrant Box:

$ vagrant box add laravel/homestead

执行示例:

1
2
3
4
5
6
7
8
9
10
11
//下载安装laravel/homestead:
$ vagrant box add laravel/homestead --force
==> box: Loading metadata for box 'laravel/homestead'
box: URL: https://atlas.hashicorp.com/laravel/homestead
==> box: Adding box 'laravel/homestead' (v0.5.0) for provider: virtualbox
box: Downloading: https://atlas.hashicorp.com/laravel/boxes/homestead/versions/0.5.0/providers/virtualbox.box
box: Progress: 5% (Rate: 572k/s, Estimated time remaining: 0:33:13)
...
//下载成功后显示如下:
==> box: Successfully added box 'laravel/homestead' (v0.5.0) for 'virtualbox'!

启动虚拟机

1
2
3
4
5
6
7
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
There are errors in the configuration of this machine. Please fix the following errors and try again:
vm: * The host path of the shared folder is missing: ~/Code
//报错没有“~/Code”目录,那我们就创建一个:
$ mkdir ~/Code
$ vagrant up

打开VirtualBox中,可以看到有一个名为”homestead-7”的虚拟机正在运行(状态为:Running)。

配置Hosts文件:

1
2
3
$ sudo vi /etc/hosts
//增加一条记录:
192.168.10.10 homestead.app

Make sure the IP address listed is the one set in your ~/.homestead/Homestead.yaml file.

使用laravel创建一个站点:

$ cd ~/Code
$ composer require laravel/homestead –dev

配置文件“Homestead.yaml”里包含一个示例站点配置.
注意默认的站点是”/home/vagrant/Code/Laravel/public”,
所以我们现在创建在~/Code下面创建一个名为”Laravel”。

$ laravel new Laravel
-bash: laravel: command not found

$ ~/.composer/vendor/bin/laravel new Laravel

/home/vagrant/Code/Laravel/public

打开浏览器,输入”http://homestead.app/“, 即可正常访问。

配置 Homestead

配置文件路径是: ~/.homestead/Homestead.yaml

配置文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
folders:
- map: ~/Code
to: /home/vagrant/Code
sites:
- map: homestead.app
to: /home/vagrant/Code/Laravel/public
databases:
- homestead

配置示例: 新增一个网站”kaixin123.app”:

(1) 新增网站目录”kaixin123net”:
在Code目录中新增一个kaixin123net目录,作为新增网站的根目录:
创建一个index.html文件,随便写写内容,比如”hello,kaixin123.app”:
路径如下: ~/Code/kaixin123net/index.html

(2) 增加sites:

1
2
3
4
5
6
7
8
9
10
11
$ vim ~/.homestead/Homestead.yaml
//在sites中新增一条map记录,修改后配置如下:
//配置中的xunij “/home/vagrant/Code/kaixin123net” 对应我们本地的 "~/Code/kaixin123net"
sites:
### 默认
- map: homestead.app
to: /home/vagrant/Code/Laravel/public
### 新增加一个网站,域名为kaixin123.app
- map: kaixin123.app
to: /home/vagrant/Code/kaixin123net

(3) 新增一条host记录:

1
2
3
4
5
6
$ sudo vim /etc/hosts
//新增一条host记录,IP是Homestead.yaml中“ip”,修改后如下:
... (略) ...
192.168.10.10 homestead.app
192.168.10.10 kaixin123.app

(4) 打开浏览器,输入”http://kaixin123.app/“, 可以正常访问。

如果重新修改配置,需要重新加载vagrant:

1
2
$ cd /Users/WangTom/Homestead
$ vagrant reload --provision

登录虚拟机:

$ cd ~/Homestead/
$ vagrant ssh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/进入~/Homestead/目录
YONGTEST:~ WangTom$ cd ~/Homestead/
YONGTEST:Homestead WangTom$
//登录虚拟机
YONGTEST:Homestead WangTom$ vagrant ssh
Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-22-generic x86_64)
* Documentation: https://help.ubuntu.com/
Last login: Fri Sep 23 06:42:33 2016 from 10.0.2.2
//有Code文件夹,这个/home/vagrant/Code与本机的~/Code目录会保持同步
vagrant@homestead:~$ ls
Code
//退出虚拟机
vagrant@homestead:~$ exit
logout
Connection to 127.0.0.1 closed.

连接Homestead数据库:

>

To connect to your MySQL or Postgres database from your host machine via Navicat or Sequel Pro:
Connect to 127.0.0.1 and port 33060 (MySQL) or 54320 (Postgres).
The username and password for both databases is homestead / secret.

地址: 192.168.10.10
端口: 3306
用户名: homestead
密码: secret

登录虚拟机数据库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
vagrant@homestead:~$ mysql -h192.168.10.10 -uhomestead -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 51
Server version: 5.7.13-0ubuntu0.16.04.2 (Ubuntu)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| homestead |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql>

以后再次启动/登录:
$ cd ~/Homestead/
$ vagrant up
$ vagrant ssh

扩展阅读

(1) brew

brew(即Homebrew), 是macOS上的软件包管理工具, 能在Mac中方便的安装软件或者卸载软件,类似ubuntu系统下的apt-get的功能.
Homebrew- The missing package manager for OS X
http://brew.sh/

(2) Vagrant

Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境。它使用Oracle的开源VirtualBox虚拟化系统,使用Chef创建自动化虚拟环境。
https://www.vagrantup.com/

(3) Laravel

Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。
https://laravel.com/

参考资料

https://laravel.com/docs/5.3/homestead
http://laravelacademy.org/post/2749.html
http://laravel-china.org/docs/5.1/homestead
https://www.vagrantup.com/docs/getting-started/

更新记录

  1. 20160825 新增此文档 (wangyt)
  2. 20160829 更新文档,改成Homestead安装与配置 (wangyt)
  3. 20160922 更新文档,新增配置多站点 (wangyt)