douwei7976 2018-04-12 05:00
浏览 246


I want to pass all my path to index.php?page=path. For example: -> index.php?page=a/b/c

Therefore this is my NGINX conf file:

location ~ /(?P<arg1>.*)$ {
    fastcgi_pass php:9000;
    fastcgi_param  QUERY_STRING       /index.php?page=$arg1;

As far as I know, query string should be everything after .com, right? I'm passing arg1 to it. However, I wanted to ignore truly file paths like /file.jpg or /images/favicon.ico. I could simply negate them in regex but then they'd have no path at all.

So how to match /anything to index.php?page=anything except for files and actually deliver those files?

  • 写回答

4条回答 默认 最新

  • duanchi8836 2018-05-18 06:32

    You need to use below

    location ~ /(.*)$ {
      try_files $uri $uri/ /index.php?page=$1;

    This will first check if file or directory exists if not then pass it to the index.php file

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
  • dongyuli4538 2018-05-18 06:14

    Based on your Apache's .htaccess, the equivalent setting under Nginx would be:

        location / {
            # First attempt to serve request as file, then
            # as directory, then index.php?page=$args.
            try_files $uri $uri/ /index.php?page=$args;
  • dsykx64220 2018-05-22 16:57

    I don't know nginx, but assuming truly/real path is something that ends with some kind of extension, and that you can write several rules with top rules having higher priority, you could write 2 rules like these:

    First rule matches an url that ends with an extension: ( dot + word ) and does nothing:


    second rule matches anything and captures:


    If that is not possible, you could try to use a rule that matches urls like these: foo/bar/foo/bar/...


    If you don't want to allow last / bar, just use

  • douman6679 2018-05-24 15:58

    The use case you mention is very common, and it's detailed in the common Nginx pitfalls page under the section Front Controller Pattern Web Apps

    As per the aforementioned page, for Drupal, Joomla, etc., just use this inside your location block:

    try_files $uri $uri/ /index.php?q=$uri&$args;

    Since you want to use the "page" variable name, your code will look as follows:

    location / {
        try_files $uri $uri/ /index.php?page=$uri&args;

    The part $uri/ is relevant only if you want to serve content from directories, for example if you want to serve a Wordpress blog from If that's not the case, you can remove the $uri/ part.

    My advice is to not use page as the variable to pass the section/route to PHP, because you might end up having a confusion between which section of the website to show vs. which page number. Page is misleading, I'd use route instead.

    Of course, your mileage may vary and you may require something more complex based on your needs, but for basic sites, these will work perfectly. You should always start simple and build from there.

    Additionally, to make debugging easier, consider using a verbose error log setting, I'd use info or notice to debug routing:

    error_log logs/error.log info;

    Good luck and keep us posted on how you solved the issue.




  • ¥15 Tpad api账户 api口令
  • ¥30 ppt进度条制作,vba语言
  • ¥15 stc12c5a60s2单片机测光敏ADC
  • ¥15 生信simpleaffy包下载
  • ¥15 请教一下simulink中S函数相关问题
  • ¥15 在二层网络中,掩码存在包含关系即可通信
  • ¥15 端口转发器解析失败不知道电脑设置了啥
  • ¥15 Latex算法流程图行号自定义
  • ¥15 关于#python#的问题:我在自己的电脑上运行起来总是报错,希望能给我一个详细的教程,(开发工具-github)
  • ¥40 基于51单片机实现球赛计分器功能