一。安装过程html
1》下载java
http://tengine.taobao.org/download.html 找到下载包而且下载(Tengine-2.2.0.tar.gz)linux
2》解压nginx
tar zxvf Tengine-2.2.0.tar.gz 正则表达式
3》配置检查 后端
进入解压后的目录 ./configure服务器
异常1: [root@bogon tengine]# ./configure checking for OS + Linux 3.10.0-327.el7.x86_64 x86_64 checking for C compiler ... not found ./configure: error: C compiler cc is not found 也就是c编译器 gcc找不到 安装gcc yum -y install gcc 继续检查 异常2: checking for PCRE library ... not found checking for PCRE library in /usr/local/ ... not found checking for PCRE library in /usr/include/pcre/ ... not found checking for PCRE library in /usr/pkg/ ... not found checking for PCRE library in /opt/local/ ... not found ./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module 缺乏pcre和pcre-devel 安装 yum -y install pcre pcre-devel 这里安装好后 能够经过 Rpm -qa | grep pcre找到全部pcre的包 Rpm -ql 完整包名 查看安装的路径 异常3: checking for OpenSSL library ... not found 缺乏openssl和openssl-devel 安装 yum -y install openssl openssl-devel 继续检测成功 能够同构日志看到 须要pcre openssl zlib(安装openssl自动安装)的库 checking for PCRE library ... found checking for PCRE JIT support ... found checking for OpenSSL library ... found checking for zlib library ... found creating objs/Makefile
4》安装并发
Make && make install 安装完成 安装目录为/usr/local/nginxapp
找到 /usr/local/nginx/sbin/nginx -V 查看全部加载的模块curl
[root@bogon sbin]# ./nginx -V Tengine version: Tengine/2.2.0 (nginx/1.8.1) built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) TLS SNI support enabled configure arguments: nginx: loaded modules: nginx: ngx_core_module (static) nginx: ngx_errlog_module (static) nginx: ngx_conf_module (static) nginx: ngx_dso_module (static) nginx: ngx_events_module (static)
/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.csdn.net/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 能够看到抛出错误
Jun 08 03:08:55 bogon systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start. Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.能够看到在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使用 http://192.168.58.131/访问 看是否出现 欢迎使用tengine的英文
二。配置文件
默认配置文件位于
/usr/local/nginx/conf/nginx.conf
1》经常使用配置选项
#user nobody; #工做的cpu的内核数 默认应该是和当前电脑的cpu一致 设置为auto自动获取当前机器 worker_processes auto; #ngx_http_log_module模块功能 #语法为 error_log 存储的log文件路径 [debug | info | notice | warn | error | crit] #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #定义存储nginx主进程ID的file。 nigix可能创建虚拟主机是 会建立多个进程 #pid logs/nginx.pid; #最顶层的指令 events通常用于指定链接处理参数 events { #设置每一个工做进程能够打开的最大并发链接数。(数量包含全部链接(好比,和后端服务器创建的链接,还有其余的), 而不单单是和客户端的链接) worker_connections 1024; } # load modules compiled as Dynamic Shared Object (DSO) # #dso { # load ngx_http_fastcgi_module.so; # load ngx_http_rewrite_module.so; #} #配置http服务器 http { #包含其余语法正确的配置文件 include mime.types; #设置默认的mini类型 default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; #是否启用内核级别拷贝 零拷贝 速度快 sendfile on; #tcp_nopush on; #长链接的超时是假呢 #keepalive_timeout 0; keepalive_timeout 65; #是否启用gzip压缩 压缩须要时间 可是压缩后 能够节省带宽 #gzip on; #配置虚拟主机 server { #虚拟主机监听端口 listen 80; #虚拟主机监听的ip(同一电脑可能多个ip)或者域名 #添加虚拟ip ifconfig 网卡名(能够经过ifconfig查看):1 192.168.58.134 netmask 255.255.255.0 up #删除虚拟ip ifconfig 网卡名(能够经过ifconfig查看):1 down server_name localhost; //当根路径访问时 必须 http://localhost/ 自动进入根目录 html下找 index.html文件 location / { root html; index index.html index.htm; } //出现如下错误状态码 自动进入 /50x.html error_page 500 502 503 504 /50x.html; //自动找 html 实际文件路径就是 /html/50x.html location = /50x.html { root html; } } }
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)
location / { auth_basic "密码验证"; auth_basic_user_file conf/htpasswd; }
name1:password1 name2:password2:comment name3:password3
[root@bogon logs]# htpasswd 使用语法 htpasswd [-cimBdpsDv] [-C cost] passwordfile username htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password htpasswd -n[imBdps] [-C cost] username htpasswd -nb[mBdps] [-C cost] username password
location / { auth_basic "密码验证"; auth_basic_user_file /usr/local/nginx/conf/auth.conf; }
效果图
二。nginx经常使用指令
1》proxypass指令
该指令将请求定位到后端服务器 好比若是nginx代理了后端服务器 默认全部静态资源都会从root下找 能够代理到后台服务器
location / { root html; index index.html index.htm; if ($uri ~* "^/admin/(.*)$") { return 404; } if ($uri ~* "^(.*)\.(jpg|png|gif|bmp)$") { proxy_pass http://192.168.58.147:8080; } }
好比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官网 找到模块下 嵌入的变量查看 好比查看2》 rewrite指令
rewrite指令 是将当前匹配的location请求 rewrite到其余的location上 也就是至关于javaee请求转发
location / { rewrite ^/download/(.*)$ /50x.html permanent; set $id 1; } location = /50x.html { root html; }由于rewrite已经跳转了 那么rewrite后面的指令要不要继续执行 就要看最后一个参数的标志了
3》valid_referers指令
“Referer”请求头为指定值时,内嵌变量$invalid_referer被设置为空字符串, 不然这个变量会被置成“1”。查找匹配时不区分大小写。
该指令的参数能够为下面的内容:
none
缺乏“Referer”请求头;
blocked
“Referer” 请求头存在,可是它的值被防火墙或者代理服务器删除; 这些值都不以“http://” 或者 “https://”字符串做为开头;
server_names
“Referer” 请求头包含某个虚拟主机名;
任意字符串
定义一个服务器名和可选的URI前缀。服务器名容许在开头或结尾使用“*”符号。 当nginx检查时,“Referer”请求头里的服务器端口将被忽略。
正则表达式
必须以“~”符号做为开头。 须要注意的是表达式会从“http://”或者“https://”以后的文本开始匹配。
实例:
location / { root html; index index.html index.htm; valid_referers none blocked server_names 192.168.58.149; if ($invalid_referer) { return 403; } }