说明:
linux下安装nginx参照文章:linux环境下安装nginx
为了便于测试,这里安装nginx的时候附带安装一个扩展模块:echo-nginx-module
一、安装步骤(简略):
- 将nginx-1.14.2.tar.gz和echo-nginx-module-0.61.tar.gz模块的安装包都拷贝到linux的/soft目录下面:
- 解压这两个压缩包
- 安装依赖
[root@192 nginx-1.14.2]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel
- 进入到nginx目录下 进行配置
[root@192 nginx-1.14.2]# ./configure --add-module /soft/echo-nginx-module-0.61
- 编译安装
[root@192 nginx-1.14.2]# make & make install
注意:
关闭防火墙:[root@192 nginx-1.14.2]# systemctl stop firewalld.service
启动linux:[root@192 nginx]# ./sbin/nginx
重启nginx:[root@192 nginx]# ./sbin/nginx -s reload
二、测试echo-nginx-module
修改nginx.conf文件如下:
···
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /hello {
default_type text/html;
echo "hello echo-nginx-module!";
}
···
启动nginx后访问:http://192.168.154.131/hello
三、试验location配置
location文档说明:http://nginx.org/en/docs/http/ngx_http_core_module.html#location
location的匹配规则有5中
- 精确匹配 【location =/hello】
- 前缀匹配(非正则)【location /hello】
- 正则匹配(区分大小写)【location ~/hello】
- 正则匹配(不区分大小写)【location ~*/hello】
- 以特定url开头匹配【^~/hello】
暂时没有发现前缀配置和以特定url开头匹配的不同
修改nginx.conf 如下:
location / {
root html;
index index.html index.htm;
}
location /hello {
default_type text/html;
echo "/hello";
}
location ~ /demo {
default_type text/html;
echo "~ /demo";
}
location ~* /demo {
default_type text/html;
echo "~* /demo";
}
location ~* \.(gif|jpg|png|js|css)$ {
#下面的types将后缀gif png的这几个都作为text/html处理了
types {
text/html gif;
text/html jpg;
text/html png;
text/html js;
text/html css;
}
echo '~* \.(gif|jpg|png|js|css)';
}
以此使用http地方访问的结果为: