Skip to content

Instantly share code, notes, and snippets.

@WerySkok
Created October 8, 2023 20:26
Show Gist options
  • Save WerySkok/261b64e2674a15a9a25daea4941533c5 to your computer and use it in GitHub Desktop.
Save WerySkok/261b64e2674a15a9a25daea4941533c5 to your computer and use it in GitHub Desktop.
NGINX config for MediaWiki
server {
listen 80;
server_name example.wiki;
# this config assumes that MediaWiki is installed into /opt/mediawiki/w,
# so LocalSettings.php would be located at /opt/mediawiki/w/LocalSettings.php
root /opt/mediawiki;
index index.php;
# allow larger file uploads and longer script runtimes
client_max_body_size 100m;
client_body_timeout 60;
# Location for wiki's entry points
location ~ ^/w/(index|load|api|thumb|opensearch_desc|rest|img_auth)\.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php8.1-fpm.sock; # or whatever port your PHP-FPM listens on
}
# Images
location /w/images {
# Separate location for images/ so .php execution won't apply
add_header X-Content-Type-Options "nosniff";
}
location /w/images/deleted {
# Deny access to deleted images folder
deny all;
}
# MediaWiki assets (usually images)
location ~ ^/w/resources/(assets|lib|src) {
try_files $uri 404;
add_header Cache-Control "public";
expires 7d;
}
# Assets, scripts and styles from skins and extensions
location ~ ^/w/(skins|extensions)/.+\.(css|js|gif|jpg|jpeg|png|svg|wasm)$ {
try_files $uri 404;
add_header Cache-Control "public";
expires 7d;
}
# License and credits files
location ~ ^/w/(COPYING|CREDITS) {
default_type text/plain;
}
## Uncomment the following code if you wish to use the installer/updater
## installer/updater
#location /w/mw-config/ {
# # Do this inside of a location so it can be negated
# location ~ \.php$ {
# include /etc/nginx/fastcgi_params;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# fastcgi_pass 127.0.0.1:9000; # or whatever port your PHP-FPM listens on
# }
#}
# Handling for Mediawiki REST API, see [[mw:API:REST_API]]
location /w/rest.php/ {
try_files $uri $uri/ /w/rest.php?$query_string;
}
## Uncomment the following code for handling image authentication
## Also add "deny all;" in the location for /w/images above
#location /w/img_auth.php/ {
# try_files $uri $uri/ /w/img_auth.php?$query_string;
#}
# Handling for the article path (pretty URLs)
location /wiki/ {
rewrite ^/wiki/(?<pagename>.*)$ /w/index.php;
}
# Allow robots.txt in case you have one
location = /robots.txt {
}
# Explicit access to the root website, redirect to main page (adapt as needed)
location = / {
return 301 /wiki/Заглавная_страница;
}
# Every other entry point will be disallowed.
# Add specific rules for other entry points/images as needed above this
location / {
return 404;
}
}
@mbarram
Copy link

mbarram commented Jan 5, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment