server {
#nginx服务器监控端口
listen 80;#设置域名
server_name dspx.com;
#设置项目根目录
root /www/dspx/public;
index index.html index.php;charset utf-8;
#…..PHP的pathinfo
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
#图片文件缓存
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
#js/css文件缓存
location ~ .*\.(js|css)?$
{
expires 12h;
}
#html 文件缓存
location ~ .*\.(?:htm|html)${
add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
}
#拒绝所有
location ~ /\.{
deny all;
}
#加载PHP监控php-fpm端口
location ~ \.php {fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
不把css/js/image相应的资源文件写入缓存,会导致资源文件404,无法被访问。至于缓存的时间长短可以自己设置。
location表达式类型
- ~ 表示执行一个正则匹配,区分大小写
- ~* 表示执行一个正则匹配,不区分大小写
- ^~ 表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其他location。
- = 进行普通字符精确匹配。也就是完全匹配。
- @ 它定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
location表达式类型
在nginx的location和配置中location的顺序没有太大关系。与location表达式的类型有关。相同类型的表达式,字符串长的会优先匹配。
- ~ 表示执行一个正则匹配,区分大小写
- ~* 表示执行一个正则匹配,不区分大小写
- ^~ 表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其他location。
- = 进行普通字符精确匹配。也就是完全匹配。
- @ 它定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
如果80端口被暂用或者不想使用80端口作为nginx默认端口,可以修改 listen 80值。浏览器访问