大网数据广告活动
服务器运维问答

Nginx subdirectory try_files wildcard always fail

· 52CE 我爱测

Nginx 1.10.3 Ubuntu, standard apt installation. index index.php; located outside server block. I need: http://example.com/test/1 pointing to /var/www/example.com/test/1 http://example.com/test/2 pointing to /var/www/example.com/t…

问题原文

原帖链接 · Isak Pontus · CC BY-SA 4.0

Nginx 1.10.3 Ubuntu, standard apt installation. index index.php; located outside server block.

I need:

  • http://example.com/test/1 pointing to /var/www/example.com/test/1
  • http://example.com/test/2 pointing to /var/www/example.com/test/2

..and so on.

Because I will create too many test, I need a wildcard for try_files. Currently I'm doing without wildcard:

server {
    server_name example.com;
    root   /var/www/example.com;
    location /test/1/ {
        try_files $uri $uri/ /test/1/index.php?$args;
    }
    location /test/2/ {
        try_files $uri $uri/ /test/2/index.php?$args;
    }
    location ~ \.php$ {
        ...
}

Out of many recommendations, none of them works.

Plain PHP running fine. WordPress & Laravel gave "File not found":

server {
    server_name example.com;
    location ~ ^/test/(?<content>.+)$ {
        root   /var/www/example.com/test/$content;
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        ...
}

File not found:

server {
    server_name example.com;
    location ~ ^/test/(?<content>[^/]+) {
        root   /var/www/example.com/test/$content;
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        ...
}

On all attempts below, It download the PHP file instead of run PHP:

server {
    server_name example.com;
    root   /var/www/example.com;
    location ~ /(?<content>[^/]+) {
        try_files $uri $uri/ /$content/index.php?$args;
    }
    location ~ \.php$ {
        ...
}

server {
    server_name example.com;
    root   /var/www/example.com;
    location ~ /(.*)/ {
        try_files $uri $uri/ /$1/index.php?$args;
    }
    location ~ \.php$ {
        ...
}

server {
    server_name example.com;
    root   /var/www/example.com;
    location ~ /test/(?<content>[^/]+) {
        try_files $uri $uri/ /test/$content/index.php?$args;
    }
    location ~ \.php$ {
        ...
}

server {
    server_name example.com;
    root   /var/www/example.com;
    location ~ /test/(?<content>.+) {
        try_files $uri $uri/ /test/$content/index.php?$args;
    }
    location ~ \.php$ {
        ...
}

If can, I willing to give $10 for the right answer

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 4.0

The regular expression location blocks are evaluated in order, so the .php block must be placed before the /test/... block, otherwise the .php files under /test/ will be downloaded instead of being executed. See this document for details.

Your best version was second from last. The regular expression extracts just the path element following the /test/ prefix.

Just reverse the location blocks. For example:

server {
    server_name example.com;
    root   /var/www/example.com;
    location ~ \.php$ {
        ...
    }
    location ~ /test/(?<content>[^/]+) {
        try_files $uri $uri/ /test/$content/index.php?$args;
    }
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-08-29)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。