一、nginx安装
1、安装相关依赖环境
yum install gcc;
yum install pcre-devel;
yum install zlib zlib-devel;
yum install openssl openssl-devel;
2、下载nginx安装包
wget http://nginx.org/download/nginx-1.12.2.tar.gz
3.解压nginx
tar -zxvf nginx-1.12.2.tar.gz
4.编译安装
cd nginx-1.12.2/ 进入nginx-1.12.2目录
./configure \
--prefix=/usr/local/nginx \ #nginx安装目录
--user=nginx --group=nginx #指定nginx用户以及用户组,也可以不用设置去掉这行
--with-http_stub_status_module #添加安装需要的status插件
--with-http_ssl_module #添加安装需要的ssl插件,如果需要实现Https的话就加上这行。当然如果已安装的nginx需要引入ssl插件也是可以的。
5.常用命令(进入上一步nginx编译安装目录/usr/local/nginx/sbin)
#启动nginx
nginx
#热重启nginx(修改后nginx.conf文件的时候常用到)
nginx -s reload
#检查nginx配置文件,也可以通过此命令查看当前nginx加载哪个配置文件启动的
nginx -t
#指定自定义目录配置文件,用于nginx启动加载
nginx -c /usr/local/nginx/conf/nginx.conf
#强制停止nginx
nginx -s stop
#优雅停止服务nginx
nginx -s quit
#查看nginx版本信息
nginx -v (小写的v)
#查看nginx加载的配置文件和编译安装时候的config
nginx -V (大写的v)
二、nginx location规则
1、location语法模板
location [ = | ~ | ~* | ^~ | @ ] pattern {
......
}
2、修饰符 [ = | ~ | ~* | ^~ | @ ] 说明
2.1 精准匹配 [=] uri节点必须和pattern完全一致才能匹配
server {
server_name dsz.com;
location = /test {
}
}
举例:
匹配
http://dsz.com/test
可能会匹配,也可以不匹配,取决于操作系统的文件系统是否大小写敏感。
http://dsz.com/TEST
匹配,忽略 querystring
http://dsz.com/test ?param1¶m2
不匹配,带有结尾的/
http://dsz.com/test/
不匹配
http://dsz.com/test2
2.2 正则匹配 [~] uri节点区分大小写正则匹配
server {
server_name dsz.com;
location ~ ^/test$ {
}
}
^/test$这个正则表达式表示字符串必须以/开始,以$结束,中间必须是test
http://dsz.com/test 匹配(完全匹配)
http://dsz.com/TEST 不匹配,大小写敏感
http://dsz.com/test?param1¶m2 匹配
http://dsz.com/test/ 不匹配,不能匹配正则表达式
http://dsz.com/test2 不匹配,不能匹配正则表达式
原文:https://www.cnblogs.com/dszazhy/p/14776604.html