Linux下环境搭建
1,wget下载:wget http://nginx.org/download/nginx-1.6.2.tar.gz
2, 安装:tar -zxvf nginx-1.6.2.tar.gz
3, 下载需要的依赖库文件:
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
4,进行configure配置:cd nginx-1.6.2 && ./configure --prefix=/user/local/nginx
查看是否报错。(/user/local/nginx是你nginx的所在路径)
5,编译安装 make && make install
6,启动nginx
cd /user/local/nginx
目录下:看到如下四个目录:conf配置文件,html网页文件,logs日志文件,sbin主要二进制程序
启动命令:/user/local/nginx/sbin/nginx
关闭(-s stop
)重启(-s reload
)
7,成功:查看是否启动成功(netstat -ano|grep 80
)失败可能是80端口被占用
虚拟主机配置讲解
虚拟主机配置文件路径为:/user/local/nginx/conf/nginx.conf
server {
#监听端口
listen 80;
#监听域名
server_name localhost;
#返回的相应文件地址 /代表根路径
location / {
#返回根路径地址(相对路径:相对于/usr/local/nginx/)
root html;
#默认访问文件 顺序查找,第一个没找到找第二个
index index.html index.htm;
}
#错误页面及其返回地址
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
location配置
location表示url方式定位,没有对应路径的资源会报错404
1,location=pattern{}精准匹配
2,location pattern{}一般匹配
#匹配/goods开头的请求,重定向能匹配goods-(\d{1,5})\.html的请求到/goods-ctrl.html
location /goods {
rewrite "goods-(\d{1,5})\.html" /goods-ctrl.html;
root bhz.com;
index index.html;
}
3,location ~ pattern{}正则匹配
#正则表达式匹配uri方式:在/usr/local/nginx/bhz.com下 建立一个test123.html 然后使用正则匹配
location ~ test {
## 重写语法:if return (条件 = ~ ~*)
#如果请求的客户端地址是指定地址则返回401
if ($remote_addr = 192.168.1.200) {
return 401;
}
#~*忽略大小写
#请求客户端浏览器为firefox则重定向请求到html
if ($http_user_agent ~* firefox) {
rewrite ^.*$ /firefox.html;