一。安装过程
1》下载
http://tengine.taobao.org/download.html 找到下载包并且下载(Tengine-2.2.0.tar.gz)
2》解压
tar zxvf Tengine-2.2.0.tar.gz
3》配置检查
进入解压后的目录 ./configure
4》安装
Make && make install 安装完成 安装目录为/usr/local/nginx
找到 /usr/local/nginx/sbin/nginx -V 查看所有加载的模块
/usr/local/nginx/sbin/nginx start|stop|restart 启动和关闭ngix服务
访问 http://ip地址
5》nginx命令参数
nginx -m 显示所有加载的模块
nginx -l 显示所有可以使用的指令
nginx -t 检查nginx的配置文件是否正确
nginx -s 启动nginx
nginx -s reload 重启nginx
nginx -s stop 停止nginx
6》开机启动nginx
nigix官网上找到 https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/ 查看linux下的启动脚本
进入官网 www.nginx.com 点击resources-community wiki
进入后 点击 下图 Getting Started按钮进入
点击下界面的Ngix init Scipts
点击 Red hast nginx init script 即可打开脚本
保存内容到 /etc/init.d/nginx (服务的教程参考http://blog.youkuaiyun.com/liaomin416100569/article/details/72876349)
拷贝后 需要修改其中的两个变量 nginx安装目录的配置文件和nginx程序的目录
nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf"修改为
nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
注意 nginx文件的内容是否完整拷贝 有时拷贝 缺少一些字符导致报错
service nginx start 或者 systemctl start nginx
一般启动会卡住 通过 systemctl status nginx 可以看到抛出错误
可以看到在nginx 在启动时候 自动会去 /var/run下面去找pid文件所以必须 要将pid文件生成在/var/run下
修改conf/nginx.conf文件
将 #pid=logs/nginx.pid(可以看到pid默认生成在logs目录下) 修改为 pid=/var/run/nginx.pid 即可 去掉注释的# 重启即可
添加到系统服务 设置开机启动
[root@bogon init.d]# chkconfig --add nginx[root@bogon init.d]# chkconfig nginx on
使用 http://192.168.58.131/访问 看是否出现 欢迎使用tengine的英文
二。配置文件
默认配置文件位于
/usr/local/nginx/conf/nginx.conf
1》常用配置选项
2》location的路径匹配
该配置位于 ngx_http_core_module 模块 (http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#location)
nginx中location的路径匹配规则
使用正则表达式需要在路径开始添加“~*”前缀 (不区分大小写),或者“~”前缀(区分大小写)
匹配的路径以“^~”开始,那么nginx不再检查后面的所有条件和正则表达式。
使用“=”前缀可以定义URI和路径的精确匹配。如果发现匹配,则终止路径查找
#这个表示 路径精确就是 /就匹配A 比如 http://localhost/
location = / {
[ configuration A ]
}
location / {
[ configuration B ]
}
location /documents/ {
[ configuration C ]
}
location ^~ /images/ {
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
请求“/”匹配配置A, 请求“/index.html”匹配配置B, 请求“/documents/document.html”匹配配置C, 请求“/images/1.gif”匹配配置D, 请求“/documents/1.jpg”匹配配置E。
2》location其他配置
》ngx_http_access_module模块 限制客户端访问ip(http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_access_module.html)
配置范例
location / {
deny 192.168.58.1;
allow all;
}
这里 192.168.58.1被禁止访问 除了58.1其他都可以访问
58.1访问 出现 403被禁止 其他ip都可以正常访问(linux访问可以使用 wget 地址 或者 curl地址) 可以通过logs/access.log查看访问服务的客户端信息
》模块ngx_http_auth_basic_module 允许使用“HTTP基本认证”协议验证用户名和密码来限制对资源的访问。
(http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_auth_basic_module.html)
指定保存用户名和密码的文件,格式如下:
密码应该使用crypt()函数加密。 可以用Apache发行包中的htpasswd命令来创建此类文件。
安装Apache yum -y install httpd
[root@bogon logs]# htpasswd -nb zs 123456
zs:$apr1$8cAQMH7X$AvjTq5Gon0kiKHR/2LW2g/
通过vi创建文件拷贝内容到该文件 /usr/local/nginx/conf/auth.conf
修改
效果图
二。nginx常用指令
1》proxypass指令
该指令将请求定位到后端服务器 比如如果nginx代理了后端服务器 默认所有静态资源都会从root下找 可以代理到后台服务器
比如nginx的ip是 192.168.58.1 当我访问 http://192.168.58.1/ttt/abc.png 会自动访问 http://192.168.58.147:8080/ttt/abc.png
if语法参考rewrite模块 (http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_rewrite_module.html#if)
具体这些参数可以去tengine官网 找到模块下 嵌入的变量查看 比如查看比如http_core模块
http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#variables
2》 rewrite指令
rewrite指令 是将当前匹配的location请求 rewrite到其他的location上 也就是相当于javaee请求转发
参考 http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_rewrite_module.html#rewrite
3》valid_referers指令
“Referer”请求头为指定值时,内嵌变量$invalid_referer被设置为空字符串, 否则这个变量会被置成“1”。查找匹配时不区分大小写。
该指令的参数可以为下面的内容:
none
缺少“Referer”请求头;
blocked
“Referer” 请求头存在,但是它的值被防火墙或者代理服务器删除; 这些值都不以“http://” 或者 “https://”字符串作为开头;
server_names
“Referer” 请求头包含某个虚拟主机名;
任意字符串
定义一个服务器名和可选的URI前缀。服务器名允许在开头或结尾使用“*”符号。 当nginx检查时,“Referer”请求头里的服务器端口将被忽略。
正则表达式
必须以“~”符号作为开头。 需要注意的是表达式会从“http://”或者“https://”之后的文本开始匹配。
实例: