How to host Mediawiki images outside root with an alias, using nginx?

201 Views Asked by At

I have a Mediawiki farm and everything works except image display. Image upload works in the sense that they get put in the correct folder along with thumbs, but images don't display. I'd like to keep hosting images outside of site root though.

The Mediawiki installation is in: /var/www/mediawiki

The image folder is in: /var/cats.wiki/images

My nginx config is:

server {
listen 80;
server_name cats.wiki;  #made up name for example                                                                                                                                            
root /var/www/mediawiki;                                                                                                                                                                                                                                                                                                                        
client_max_body_size 100M;                                                                                                                                                                                                                                                                                                                     

location /images {                                                                                                                                                       
alias /var/cats.wiki/images;   #relevant part                                                                                                                                 
}  
                                                                                                                                                                                                                                                                                                                                             
location / {                                                                                                                                                              
index index.php;                                                                                                                                                        
error_page 404 = @mediawiki;                                                                                                                                         
} 
                                                                                                                                                                                                                                                                                                                                           
location @mediawiki {                                                                                                                                                     
rewrite ^/w([^?]*)(?:\?(.*))? /index.php?title=$1&$2 last;                                                                                                            
}       
                                                                                                                                                                                                                                                                                                                                        
location ~ \.php$ {                                                                                                                                                         
include fastcgi_params;                                                                                                                                                 
fastcgi_pass unix:/run/php/php7.4-fpm.sock;                                                                                                                             
fastcgi_index index.php;                                                                                                                                                
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;                                                                                                       
fastcgi_param MW_INSTALL_PATH /var/www/mediawiki;                                                                                                                      
fastcgi_param WIKI_PATH "catwiki.php";                                                                                                                              
}   
                                                                                                                                                            
location ~* \.(js|css|svg|png|jpg|jpeg|gif|ico)$ {                                                                                                                          
try_files $uri /index.php;                                                                                                                                              
expires 365d;                                                                                                                                                           
log_not_found off;                                                                                                                                              
gzip_static on;                                                                                                                                                         
gzip_comp_level 5;                                                                                                                                                      
access_log off;                                                                                                                                                         
add_header Cache-Control private;                                                                                                                                           
}                                                                                                                                                                   
}       

and the relevant section of my LocalSettings, the logo also doesn't display on browser

$wgLogo = "/var/cats.wiki/images/logo.png"; 
$wgEnableUploads = true;                                                                                                
$wgUseImageMagick = true;                                                                                               
$wgImageMagickConvertCommand = "/usr/bin/convert";                                                                            
$wgUploadDirectory = "/var/cats.wiki/images";                                                                                                
$wgUploadPath = "/images";    

Thanks! :)

1

There are 1 best solutions below

0
On

It is not clear from your question whether the server directive from your nginx config applies to your whole farm or only one wiki. You definitely can have one server for all wikies; I do in my own wiki farm setup.

In my wiki farm setup, the section for the image folder says (simplified and adapted to your example):

location ~* ^/images(?<image_subpath>/.+)$ {
    root $images_root;
    try_files $image_subpath @mediawiki;
    # ... (some code to neutralise potentially malicious uploads)
}

where $images_root is set previously, in http directive, with a map directive (simplified and adapted to your example):

map $host     $images_root {
    cats.wiki /var/cats.wiki/images;
    dogs.wiki /var/dogs.wiki/images;
    # ...
}