开发者社区> 问答> 正文

关于一个nginx的问题,求助大虾们:报错

问题是这样的:

需要静态化一个页面如a.html。请求这样:http://localhost:8080/a.html这个页面直接配置在nginx里面。可以静态直接读取。

然后,http://localhost:8080/a.html?redirect=test.html。这个请求交给后台配置的tomcat处理。
然后其它的如:http://localhost:8080/b.html也交给tomcat处理。
意思就是只有一个页面,就是a.html是nginx静态读取,其它的都交给tomcat处理。
配置如下:
location =/a.html
{
            #root   html;
    index a.html index.html index.htm;
        }
location ~.*./a.html?a=...$
{
            #root   html;
    index a.html index.html index.htm;
        }
请问一下如何能实现上述功能呢?

展开
收起
kun坤 2020-06-06 15:11:43 415 0
1 条回答
写回答
取消 提交回答
  • nginx 不熟,但是apache里就是对query string 结合  url-params做判断 然后rewrite。 nginx也是这个思路吧 ######这里的参数不知道怎么去。在if里面判断不好做判断######

    location / {

           if ($request_uri = /a.html) {
                 root /xxxx/xxx
                 break;
           }
           #proxy_pass至后端略
    }
    ######问题解决了,谢谢啦,,我把条件换了一下。变成:if($request_uri != /a.html){...break;}这样就ok了。######我再整理一下配置文件,先谢谢咯。。方便留个联系方式不?我邮箱:wwqdd1@126.com。可以发我邮箱一下。。######回复 @wwqdd1 : 不会啊, 我试了下,当/a.html的时候是读取静态文件, 当/a.html?xxx的时候是走的后端######这样配置后全走后端去了。。######回复 @wwqdd1 : 刚搞错你意思了,改了下######
    location = /a.html {
      #root   html; 
      index a.html index.html index.htm;
      break; #这个 break 很关键。
    }
    
    location ~ /.*\.html\?.*$ 
    { 
      #root   html; 
      index a.html index.html index.htm;
    
      # 其他配置,比如代理请求什么的可以写在这里。
    }


    ######

    刚才看到最后一条。如果是 a.html ,nginx处理,其他交给后端。可以简单点写

    location =/a.html  {
    break;
    }

    location  /  {
    #后端代理部分。
    }

    ######那如果是$uri的话是怎么配置呢######好的,可能是我其它配置有问题。谢谢啦。######果然。 看来需要 if ( $uri ~ )######你搞错了把。 = /a.html 是不会匹配 /a.html?xxxxx 请求的。这点你要确认一下。######这样可以实现拦截a.html下的所有请求,包括a.html?a=2这个也拦截了。我想实现a.html?a=2这样的情况下不被拦截,有办法不######location = /a.html {

      if ($query_string = '') {
        break;
      }
      #代理部分
    }

    location ~ ^/.*\.html.*$ {
     #代理部分
    }

    看这样的配置能不能工作。

    ###### @wwqdd1 if (uri = '/a.html') { break; }######回复 @wwqdd1 : 不错。你这么写更好理解。非常的不错啊。######问题解决了,谢谢啦,,我把条件换了一下。变成:if($request_uri != /a.html){...break;}这样就ok了。...
    2020-06-06 15:11:50
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
《Nginx 代理系统常用手册》 立即下载
CentOS Nginx PHP JAVA 多语言镜像使用手 立即下载
CentOS Nginx PHP JAVA多语言镜像使用手册 立即下载