Nginx目录自动列出功能配置
启用目录自动列表功能:
在获取开源软件时,常会见到直接展示所有版本源码包的简单页面,这正是启用了目录自动列出功能的结果。
在虚拟主机的location / {……}目录控制块中,添加相应配置即可实现目录自动列出:
location / {
autoindex on;
}
利用expires指令优化浏览器缓存
配置浏览器本地缓存:
为了提升浏览速度,浏览器会在用户磁盘上缓存近期访问过的文件。当用户再次请求同一页面时,
浏览器可直接从本地磁盘读取文件,从而加速浏览过程,节省网络带宽并提高整体网络效率。
关键指令:expires
默认状态:off
适用作用域:http, server, location
主要功能:通过expires指令来管理HTTP应答中的“Expires”和“Cache-Control”头部信息。
具体配置示例:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
Nginx多类型虚拟主机搭建方法
设置虚拟主机参数:
Nginx支持构建多种类型的虚拟主机,包括基于IP地址、基于域名以及基于端口的方式。
简而言之,即不同IP相同端口、不同端口相同IP或域名,或者不同域名相同端口的组合。
以下是一个基于相同域名的虚拟主机配置文件示例:
server {
listen 80 default;
server_name www.wpython.com;
index index.html index.htm index.php;
root /alidata/www/www.wpython.com;
# wordpress 伪静态规则
location / {
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
access_log /alidata/log/nginx/access/www.wpython.com.log;
}
server {
listen 80 default;
server_name www.pyyw.net;
index index.html index.htm index.php;
root /alidata/www/www.pyyw.net;
# wordpress 伪静态规则
location / {
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
access_log /alidata/log/nginx/access/www.pyyw.net.log;
}