nginx配置之location

5 篇文章 0 订阅

说明:
linux下安装nginx参照文章:linux环境下安装nginx
为了便于测试,这里安装nginx的时候附带安装一个扩展模块:echo-nginx-module

一、安装步骤(简略):

  1. nginx-1.14.2.tar.gzecho-nginx-module-0.61.tar.gz模块的安装包都拷贝到linux的/soft目录下面:
  2. 解压这两个压缩包
  3. 安装依赖
    [root@192 nginx-1.14.2]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel
  4. 进入到nginx目录下 进行配置
    [root@192 nginx-1.14.2]# ./configure --add-module /soft/echo-nginx-module-0.61
  5. 编译安装
    [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中

  1. 精确匹配 【location =/hello】
  2. 前缀匹配(非正则)【location /hello】
  3. 正则匹配(区分大小写)【location ~/hello】
  4. 正则匹配(不区分大小写)【location ~*/hello】
  5. 以特定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地方访问的结果为:
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jackletter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值