I have installed a Wordpress in a Sub directory called /WordPressDemo.
There is a domain called demo.example.com with the document root / which means that you need to enter demo.example.com/WordPressDemo to access the WordPress.
I use nGINX what causes alot of trouble, I managed to get the basic links (e.g. permalinks) working, so the multisite itself works, the only problem is that it is requesting the static data from the wrong path.
Example:
It requests: http://demo.example.com/WordPressDemo/MULTISITE-NAME/wp-includes/js/jquery/jquery.js?ver=1.11
but should request
http://demo.example.com/WordPressDemo/wp-includes/js/jquery/jquery.js?ver=1.11
My current nginx directives:
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^/WordPressDemo(/[^/]+)?(/wp-.*) /WordPressDemo$2 last;
rewrite ^/WordPressDemo(/[^/]+)?(/.*\.php)$ /WordPressDemo$2 last;
}
location / {
try_files $uri $uri/ /WordPressDemo/index.php?$args ;
}
location ~ \.php$ {
try_files $uri /WordPressDemo/index.php;
include fcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires max;
}
Have you looked into the Wordpress codex?
Anyway, you may be interested in adapting this code:
# WordPress multisite subdirectory rules.
# Designed to be included in any server {} block.
# This order might seem weird - this is attempted to match last if rules below fail.
# http://wiki.nginx.org/HttpCoreModule
location / {
index index.php;
try_files $uri $uri/ /index.php?$args;
}
# Directives to send expires headers and turn off 404 error logging.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 24h;
log_not_found off;
}
location ~ ^/[_0-9a-zA-Z-]+/files/(.*)$ {
try_files /sites/$blogid/files/$2 /wp-includes/ms-files.php?file=$2 ;
access_log off; log_not_found off; expires max;
}
#avoid php readfile()
location ^~ /sites {
internal;
alias /var/www/mu/sites ;
access_log off; log_not_found off; expires max;
}
# Uncomment one of the lines below for the appropriate caching plugin (if used).
#include global/wordpress-ms-subdir-wp-super-cache.conf;
#include global/wordpress-ms-subdir-w3-total-cache.conf;
# Rewrite multisite '.../wp-.*' and '.../*.php'.
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}
# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ \.php$ {
# Zero-day exploit defense.
# http://forum.nginx.org/read.php?2,88845,page=3
# Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.
# Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine. And then cross your fingers that you won't get hacked.
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_cache_bypass $no_cache
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 6000m;
}
location ~ /purge(/.*) {
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}
Does it answer your question?
Related
I have been trying to setup a directory to use a seperate root directory (or alias).
location ~ \.php$ # root location
{
try_files $uri #php;
include /etc/openresty/fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
location ^~ /sub2/
{
rewrite_log on;
alias /var/www/sub2/;
location ~ \.php$
{
try_files $uri #ono_php;
include /etc/openresty/fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
}
try_files $uri #ono_main_cache;
}
location #ono_php
{
rewrite ^/sub2/(.+)$ /index.php?request=$1 last;
}
location #ono_main_cache
{
if ( $http_accept_encoding !~ gzip )
{
rewrite ^/sub2/(.*)$ /index.php?request=$1 last;
}
if ( $query_string )
{
rewrite ^/sub2/(.*)$ /index.php?request=$1 last;
}
try_files /var/www/sub2/data/cache/html$uri.html.gz #ono_php;
add_header Content-Encoding gzip;
gzip off;
default_type text/html;
}
There is more in the overall nginx configuration for the domain, but I've only included what is relevant.
To overview, I want the /sub2/ to use the root/alias /var/www/sub2/, rather than the default /var/www/site/. That works correctly. However, all URLs that don't load a file should be redirected to index.php as in the #ono_php block. However, they use the first location block listed (with # root location appended to it). So if I load the URL:
https://example.com/sub2/contact
It loads the same URL as:
https://example.com/contact
How can I get the block #ono_php to use the location ~ \.php$ defined inside location ^~ /sub2/?
There were a number of issues. The main being I set the root/alias to /var/www/sub2 and when requests were made, they had that directory appended from the URL, e.g. the path requested was /var/www/sub2/sub2/url - where sub2/url is the URL. Changing the root to /var/www fixed this.
location ^~ /sub2
{
root /var/www;
try_files $uri $uri/ #ono_main_cache;
location ~ \.php$
{
include /etc/openresty/fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
}
}
location #ono_php
{
rewrite ^/sub2/(.*)$ /sub2/index.php?request=$1 last;
}
location #ono_main_cache
{
if ( $http_accept_encoding !~ gzip )
{
rewrite ^/sub2/(.*)$ /sub2/index.php?request=$1 last;
}
if ( $query_string )
{
rewrite ^/sub2/(.*)$ /sub2/index.php?request=$1 last;
}
try_files /sub2/data/cache/html$uri.html.gz #ono_php;
add_header Content-Encoding gzip;
gzip off;
default_type text/html;
}
I had three WordPress Installations /basketball /football and /kitesurfen each was defined in nginx.conf file with separate directories.
removed all configurations and installed a multisite on / path.
when I create new site with one of the previous paths e.g /basketball /football or /kitesurfen I can not access the dashboard. I get ERR_TOO_MANY_REDIRECTS and different error based on browser.
If I create a site with any other path e.g /test /volleyball or /swimming it works fine.
Need help to fix this. I am not sure if its a WordPress (PHP) error or Nginx configurations error.
Nginx Conf
if (!-e $request_filename) {
rewrite ^/([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 break;
}
location / {
root /var/www/html/multinetworks/networktheme;
index index.html index.htm index.php;
rewrite ^/([_0-9a-zA-Z-]+/)?wp-admin$ /$1wp-admin/ redirect;
rewrite ^/([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 break;
rewrite ^/([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 break;
try_files $uri $uri/ /index.php$is_args$args;
location = /favicon.ico {
log_not_found off; access_log off;
}
location = /robots.txt {
log_not_found off; access_log off; allow all;
}
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_index index.php;
}
}
We're running a Wordpress site on a Nginx server, I am now trying to install Piwik there in the /stats folder.
This is the default Nginx configuration that came with the install, I added the "/stats" block myself, but it doesn't work - it gets rendered by WordPress whenever I go to mysite.com/stats instead of going to that folder.
Desired behavior would be that the /stats subdirectory (and all files and directories in it) is just parsed by PHP as would be on a default install without Nginx rules
Any clue what I'm missing?
server_name _;
port_in_redirect off;
client_header_buffer_size 4k;
client_body_buffer_size 128k;
client_max_body_size 16m;
root /var/www/html;
index index.html index.php;
charset utf-8;
log_not_found off;
gzip_static on;
gzip_types text/css application/javascript text/xml;
gzip_vary on;
gzip on;
# redirect server error pages to the static page /50x.html
#
error_page 500 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location #rewrite {
rewrite ^.*$ /index.php?$args;
}
error_page 404 #rewrite;
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# WP Multisite rewrites
rewrite /([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 last;
rewrite /([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 last;
location / {
try_files $uri $uri.gz $uri/ #rewrite;
}
location ~ \.sql$ {
rewrite ^.*$ /index.php?$args;
}
# We do not want to run php from wp uploads
location ~* /(?:uploads|files)/.*\.php$ {
rewrite ^.*$ /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
}
location /stats {
try_files $uri $uri/ /index.php?$args;
index index.php;
}
location = /favicon.ico {
access_log off;
expires 2w;
add_header Cache-Control public;
try_files $uri #rewrite;
}
location ~* \.(js|css|jpg|jpeg|png|gif|ico|woff|woff2|ttf|otf|eot|pdf|xml|mp4|ogg|mp3|mov|wmv|avi|cur|rtf|txt|swf)$ {
add_header Cache-Control public;
add_header Access-Control-Allow-Origin *;
expires 2w;
try_files $uri $uri.gz;
}
The rules for multi-site WordPress, particularly this one: rewrite /([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 last; will redirect any /stats/index.php URI back to WordPress's /index.php.
If you are not using a multi-site WordPress, you can safely delete the redundant rewrite rules.
If you are using a multi-site WordPress, some redesign is required.
I'm using Wordpress as root of my website and Invision Power Boards as forum.
http://localhost -> Wordpress
http://localhost/forum -> IPB
I have removed "index.php" from Wordpress URLs successfully with Nginx-rewrite however when I try to use SEO Friendly URLs on IPB, nginx simply returns to Wordpress' 404 page.
My configuration is like this:
#This removes "index.php" from Wordpress URLs
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
Then I follow this link to modify my nginx conf file in order to be able to use SEO friendly URLs of IPB: http://www.devcu.com/forums/topic/262-furl-friendly-urls-with-ipb-and-nginx/
#This part is to be able to use IPB SEO
location /forum/ {
index index.php;
try_files $uri $uri/ /forum/index.php?$uri&$args;
rewrite ^ /index.php? last;
}
When I click a link on my forum (for example: http://localhost/forum/index.php/forum/51-sport/) nginx simply redirects me to (http://localhost/forum/forum/51-sport/) which displays Wordpress 404 error page.
I have very little knowledge about regex so any help would be appreciated.
This is my whole conf file after modifications, little messy I accept that.
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /home/user_name/public_html;
access_log /var/log/nginx/a/access.log;
error_log /var/log/nginx/a/error.log
server_name localhost;
server_tokens off;
location / {
try_files $uri $uri/ #wordpress;
}
location #wordpress {
fastcgi_pass php-fpm;
fastcgi_param SCRIPT_FILENAME /home/user_name/public_html$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME /index.php;
}
location /forum {
try_files $uri $uri/ try_files $uri $uri/ /forum/index.php?q=$uri;
}
location /forum/ {
try_files $uri $uri/ try_files $uri $uri/ /forum/index.php?q=$uri;
}
#location / {
#index index.php index.html index.htm;
#try_files $uri $uri/ /index.php?q=$uri&$args;
#}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \.php$ {
fastcgi_split_path_info ^(/)(/.*)$;
}
# Add trailing slash to */wp-admin and */forum requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9001
#location ~ \.php$ {
# fastcgi_split_path_info ^(/)(/.*)$;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /home/user_name/public_html$fastcgi_script_name;
# fastcgi_param PATH_INFO $fastcgi_script_name;
# include /etc/nginx/fastcgi_params;
#REMOVE THIS
#fastcgi_read_timeout 60000;
#fastcgi_send_timeout 6000;
#}
}
Since the last post, I have played with IPB's SEO configurations and I managed to remove "index.php" from URLs. It doesn't effect the result of course. But it seems that location / decides what to do and therefore link is being considered as a Wordpress permalink.
EDIT - Solution
# Upstream to abstract backend connection(s) for php
upstream php {
# server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9001;
}
server {
## Your website name goes here.
server_name localhost;
## Your only path reference.
root /home/username/public_html;
## This should be in your http block and if it is, it's not needed here.
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content
try_files $uri $uri/ /index.php;
}
location /forum {
try_files $uri $uri/ /forum/index.php;
rewrite ^ /forum/index.php? break;
}
location ~ ^/forum/index.php {
if ($args != "") {
rewrite ^ http://www.google.com/ permanent;
}
try_files $uri $uri/ /forum/index.php;
rewrite ^ /forum/index.php? last;
}
location /forum/admin/ {
try_files $uri $uri/ /forum/admin/index.php;
rewrite ^ /forum/admin/index.php? last;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include /etc/nginx/fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
I'm too using wordpress and ipb on nginx, now configuring, i added wordpress permalink to ipb config and turn on seo Rewrite URLs, Force Friendly URLs
location / {
try_files $uri $uri/ /index.php?$args;
}
it worked for me
What would be the nginx rewrite rule to redirect my wordpress permalink structure from /%category%/%postname%/ to /%postname%/?
In summary, you need to let NGINX know that if that file doesn't exist, to not throw a 404 error, but rather call index.php. Wordpress is smart enough to parse the URL as parameters, and serve the correct page.
Add this snippet in your server configuration block:
location / {
try_files $uri $uri/ /index.php?$args;
}
Here is a complete example from nginx.org:
# Upstream to abstract backend connection(s) for php
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}
server {
## Your website name goes here.
server_name domain.tld;
## Your only path reference.
root /var/www/wordpress;
## This should be in your http block and if it is, it's not needed here.
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}