0

I have NGINX setup to serve content over localhost with HTTPS. Using the following location block, I can serve mediawiki just fine (it is in the /w folder)

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;
}

However, when I try to setup short urls (https://localhost/wiki/Some_Page for example) using this guide, I always get 404 file not found. I'm using PHP-FPM as the upstream, for reference. Here is the full configuration I'm using with NGINX to achieve short URLs

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 {
    }
  }
}

I haven't made any changes to my LocalSettings.php, except for adding the following data

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

Thanks in advance!

  • I don't think MediaWiki needs the PATH_INFO parameter, and you should remove the `rewrite` statement. – Richard Smith Apr 20 '19 at 07:01
  • Thanks, that mostly worked. https://localhost/wiki/Main_Page now loads fine. The only issue now is that the styling fails to load. It looks to be calling https://localhost/w/load.php?debug=false&lang=en&modules=mediawiki.legacy.commonPrint%2Cshared%7Cmediawiki.skinning.interface%7Cskins.vector.styles&only=styles&skin=vector which gives a 404 – Lucky The Rabbit Apr 20 '19 at 20:21
  • You implied that the `/w/` URIs were working fine. Look at the access log and error log. Also, the `404` at the end of the `try_files` statements should be `=404`. – Richard Smith Apr 20 '19 at 20:25
  • They do if I use the location block at the beginning. Do I need to keep that in? Thanks for the note about the `try_files`, fixed – Lucky The Rabbit Apr 20 '19 at 20:30
  • Only the article path URIs use the `/wiki/` prefix. All other URIs use the `/w/` prefix. – Richard Smith Apr 20 '19 at 20:35
  • Right, so shouldn't that be covered by `location ~ ^/w/(index|load|api|thumb|opensearch_desc)\.php$` block? – Lucky The Rabbit Apr 20 '19 at 22:22
  • I figured it out, in that location block `$fastcgi_script_name` was set to `/w/load.php`, and the `SCRIPT_FILENAME` was set to `$document_root/w/$fastcgi_script_name`. Setting it to `$document_root$fastcgi_script_name` fixed it. Thanks again, couldn't have figured this out without your help – Lucky The Rabbit Apr 20 '19 at 22:35

1 Answers1

0

Thanks to the help of Richard Smith in the comments, I was able to get a working configuration.

LocalSettings.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;
    }
  }
}