使用短网址时找不到带有 File:example.jpg 的 MediaWiki 文件

MediaWiki File Not Found with File:example.jpg when using short urls

我正在尝试使用 Nginx 建立一个 wiki。

当我使用/wiki/File:image.jpg Nginx returns 404.
当我使用 /index.php?title=File:image.jpg 时它工作正常。

server {
    listen 80;
    listen [::]:80 ipv6only=on;

    root /usr/share/nginx/mediawiki;
    index index.php index.html index.htm;

    ...

    location /wiki/ {
        index index.php;
        rewrite ^/wiki/([^?]*)(?:\?(.*))? /index.php?title=& last;
    }

    location ~* /wiki/images/.*.(html|htm|shtml|php)$ {
        types { }
        default_type text/plain;
    }

    location ~* /wikiimages/ {
        try_files $uri /wiki/index.php;
    }

    location ~* \.(js|css|jpg|jpeg|png|gif|ico)$ {
        try_files $uri /wiki/index.php;
        expires max;
        log_not_found off;
    }

    location ~*\.php?$ {
        try_files $uri =404;
    #   # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        include fastcgi_params;
    }

    location /wiki/.*\.php?$ {
        try_files $uri =404;
    #   # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        include fastcgi_params;
    }
}

将 error_log 级别更改为调试 + 添加重写日志。类似于:

error_log /var/log/nginx/error.log debug;
rewrite_log on;

要进行测试,请查看此处:https://gist.github.com/jmervine/8943627 - 有示例配置,可让您自动进行测试。使用完整的日志,您可以解决问题。

我有这样的东西:

2016/04/21 13:02:10 [debug] 7566#7566: *1 http script regex: "^/wiki/([^?]*)(?:\?(.*))?"
2016/04/21 13:02:10 [notice] 7566#7566: *1 "^/wiki/([^?]*)(?:\?(.*))?" matches "/wiki/File:test.jpg", client: 127.0.0.1, server: , request: "GET /wiki/File:test.jpg HTTP/1.1", host: "localhost"
2016/04/21 13:02:10 [debug] 7566#7566: *1 http script copy: "/index.php"
2016/04/21 13:02:10 [debug] 7566#7566: *1 http script args
2016/04/21 13:02:10 [debug] 7566#7566: *1 http script copy: "title="
2016/04/21 13:02:10 [debug] 7566#7566: *1 http script capture: "File:test.jpg"
2016/04/21 13:02:10 [debug] 7566#7566: *1 http script copy: "&"
2016/04/21 13:02:10 [debug] 7566#7566: *1 http script capture: ""
2016/04/21 13:02:10 [debug] 7566#7566: *1 http script regex end
2016/04/21 13:02:10 [notice] 7566#7566: *1 rewritten data: "/index.php", args: "title=File:test.jpg&", client: 127.0.0.1, server: _, request: "GET /wiki/File:test.jpg HTTP/1.1", host: "localhost"

看起来你的正则表达式可以更好...今天晚些时候我会尝试找到更好的东西。

我更改了尝试直接为静态资源提供服务的正则表达式。

server {
    ...

    location ~* \.(js|css|gif|ico)$ {
        try_files $uri /wiki/index.php;
        expires max;
        log_not_found off;
    }

    ...
}