Nginx位置块匹配规则

时间:2017-03-20 11:42:12

标签: regex nginx configuration webserver

我是nginx服务器的新手 我要将诸如codeigniter之类的php框架部署到nginx服务器 我的配置文件如下。

server {
    index index.html index.php index.htm;

    # set expiration of assets to MAX for caching
    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {  
        expires max;
        log_not_found off;
    }

    location / {  
        # Check if a file exists, or route it to index.php.
        try_files $uri $uri/ /index.php;
    }

    location ~* \.php$ {  
        fastcgi_pass unix:/var/run/php/php7-fpm.sock;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
 }

我的问题是: 1)请求uri就像这样“www.domain.com/controllername/functionname/param1/param2/”
nginx如何使用此URL?

2)第三个位置块与正则表达式“.php $”匹配 只有当uri以“.php”结尾时,这是真的吗? (我想是的,但是这个块的fastcgi_split_path_info有不同的正则表达式。)

1 个答案:

答案 0 :(得分:1)

问题1)是的,这应该有效,因为行

try_files $uri $uri/ /index.php;

逐个处理请求。首先nginx尝试查找URI描述的文件,如果没有匹配,则检查它是否是目录。如果没有,它会调用您的index.php文件。原始URI与许多其他HTTP_REQUEST变量一起移交,如果你配置(codeigniter是正确的),codeigniter的代码负责解析url。 codeigniter的调用约定是“www.domain.com/controllername/public_function/param1/param2/” 所以通常你不会在你的URI中给出viewname,而是控制器和函数名。

问题2)“location”指令仅使用没有任何GET参数的URI路径。 split_path的工作方式不同,所以它需要一个不同的正则表达式。

相关问题