Multi Site WordPress rewrites rules in Nginx

I am trying to start a blog installation of several domains using WordPress and Nginx. The final step is to configure some rewrite rules in .htaccess (apache only) for the web server. How to translate this into Nginx rewrite rules?

 RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] # uploaded files RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L] RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] RewriteRule . index.php [L] 
+7
nginx wordpress mod-rewrite
source share
4 answers

Example nginx rewrite example for Wordpress 3 :

 server{ server_name *.example.com; listen 80; #on server block ##necessary if using a multi-site plugin server_name_in_redirect off; ##necessary if running Nginx behind a reverse-proxy port_in_redirect off; access_log /var/log/nginx/example-com-access.log; location / { root /var/www/example.com/wordpress; index index.html index.htm index.php; rewrite ^.*/files/(.*)$ /wp-includes/ms-files.php?file=$1 last; if (!-e $request_filename) { rewrite ^.+/?(/wp-.*) $1 last; rewrite ^.+/?(/.*\.php)$ $1 last; rewrite ^(.+)$ /index.php?q=$1 last; } } location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ { root /var/www/example.com/wordpress; rewrite ^/.*(/wp-.*/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last; rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ /wp-includes/ms-files.php?file=$1 last; expires 30d; break; } location ~ wp\-.*\.php|wp\-admin|\.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/example.com/wordpress$fastcgi_script_name; } } 
+9
source share

Above there is no space after overwriting ^. / Files / (. (Html ​​| jpg | ....... you need a space after $ and before / wp -includes / ms-files.php. Hooray!

+3
source share

These nginx rewrite rules for WordPress completely solved my problem, including changing ex.com/wp-admin to ex.com/wp-admin/.

But I found an error inside that wasted me for several hours as follows: If the static URL includes .html just like /%category%/%post_id%.html , you need to set:

 location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ { root /var/www/example.com/wordpress; rewrite ^/.*(/wp-.*/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last; rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ /wp-includes/ms-files.php?file=$1 last; expires 30d; break; } 

Change it to:

 location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ { root /var/www/example.com/wordpress; rewrite ^/.*(/wp-.*/.*\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last; rewrite ^.*/files/(.*(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ /wp-includes/ms-files.php?file=$1 last; expires 30d; break; } 

Or, you had a β€œ404 page not found” error on content pages!

+1
source share

Instead:

 location ~ wp\-.*\.php|wp\-admin|\.php$ { 

Change it to:

 location ~ .php$ 

Lets him steer

 blog.com/wp-admin 

to

 blog.com/wp-admin/ 
0
source share

All Articles