尝试设置 mediawiki 短网址时,NGINX 出现 404 响应

尝试设置 mediawiki 短网址时,NGINX 出现 404 响应

我已将 NGINX 设置为通过 HTTPS 在本地主机上提供内容。使用以下位置块,我可以很好地提供 mediawiki(它位于/w文件夹中)

location ~ /w/(.*)(\.php)?$ {
  index index.php index.html;
  try_files $uri $uri.php /w/index.php =404;
  fastcgi_pass 127.0.0.1:9000;
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}

然而,当我尝试设置短网址时(https://localhost/wiki/Some_Page例如)使用本指南,我总是得到 404 文件未找到。我使用 PHP-FPM 作为上游,供参考。这是我使用 NGINX 实现短网址的完整配置

worker_processes 1;

events {
  worker_connections 1024;
}

http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;
  keepalive_timeout 65;
  gzip on;

  log_format compression '$time_local - "$uri" $status "$http_referer" $request_filename';
  access_log /Users/nicholas.chambers/log/nginx/access.log compression;
  error_log /Users/nicholas.chambers/log/nginx/error.log;

  server {
    listen 80;
    server_name localhost;
    return 301 https://$host$request_uri;
  }

  server {
    listen 443 ssl http2;
    server_name localhost;

    ssl_certificate /Users/nicholas.chambers/nginx/ssl/cert.pem;
    ssl_certificate_key /Users/nicholas.chambers/nginx/ssl/private.key;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    ssl_protocols TLSv1.2;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
    ssl_prefer_server_ciphers on;
    ssl_trusted_certificate /Users/nicholas.chambers/ca/intermediate/certs/chain.pem;

    root /Users/nicholas.chambers;

    allow 127.0.0.0/8;
    deny all;

    location ~ /wordpress/(.*)(\.php)?$ {
      index index.php index.html;
      try_files $uri $uri.php /wordpress/$1/index.php /wordpress/index.php =404;
      fastcgi_pass 127.0.0.1:9000;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }

    location ~ ^/w/(index|load|api|thumb|opensearch_desc)\.php$ {
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root/w/$fastcgi_script_name;
      fastcgi_pass 127.0.0.1:9000; # or whatever port your PHP-FPM listens on
    }

    location /w/images {
    }

    location /w/images/deleted {
      deny all;
    }

    location ~ ^/w/resources/(assets|lib|src) {
      try_files $uri 404;
      add_header Cache-Control "public";
      expires 7d;
    }

    location ~ ^/w/(skins|extensions)/.+\.(css|js|gif|jpg|jpeg|png|svg)$ {
      try_files $uri 404;
      add_header Cache-Control "public";
      expires 7d;
    }

    location = /favicon.ico {
      alias /w/images/6/64/Favicon.ico;
      add_header Cache-Control "public";
      expires 7d;
    }

    location /wiki/ {
      rewrite ^/wiki/(?<pagename>.*)$ /w/index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root/w/index.php;
      fastcgi_param PATH_INFO $pagename;
      fastcgi_param QUERY_STRING $query_string;
      fastcgi_pass 127.0.0.1:9000;
    }

    location = /robots.txt {
    }
  }
}

我没有对 LocalSettings.php 进行任何更改,除了添加以下数据

$wgScriptPath = "/w";
$wgScriptExtension = ".php";
$wgArticlePath = "/wiki/$1";
$wgUsePathInfo = true;

提前致谢!

答案1

感谢评论中 Richard Smith 的帮助,我得以获得一个可行的配置。

本地设置.php:

$wgScriptPath = "/w";
$wgScriptExtension = ".php";
$wgArticlePath = "/wiki/$1";

nginx.conf:

worker_processes 1;

events {
  worker_connections 1024;
}

http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;
  keepalive_timeout 65;
  gzip on;

  server {
    listen 80;
    server_name localhost;
    return 301 https://$host$request_uri;
  }

  server {
    listen 443 ssl http2;
    server_name localhost;

    # SSL Settings redacted for brevity

    root /Users/nicholas.chambers;

    location ~ ^/w/(index|load|api|thumb|opensearch_desc)\.php$ {
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param QUERY_STRING $query_string;
      fastcgi_pass 127.0.0.1:9000;
    }

    location /w/images {
    }

    location /w/images/deleted {
      deny all;
    }

    location ~ ^/w/resources/(assets|lib|src) {
      try_files $uri =404;
      add_header Cache-Control "public";
      expires 7d;
    }

    location ~ ^/w/(skins|extensions)/.+\.(css|js|gif|jpg|jpeg|png|svg)$ {
      try_files $uri =404;
      add_header Cache-Control "public";
      expires 7d;
    }

    location /wiki/ {
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root/w/index.php;
      fastcgi_param QUERY_STRING $query_string;
      fastcgi_pass 127.0.0.1:9000;
    }
  }
}

相关内容