Ross Edwards
asked on
NGINX config to exclude subdirectory
Hi folks
We support a Magento shop for which we recently enabled Nginx as a front end. All is good, however the Wordpress blog located at ./blog is being ignored, instead Magento tries to serve the location, and ends up in a 404.
I know this is due to the Nginx config not correctly redirecting for the /blog folder, but I don't know enough about Nginx to fix it.
There is a .htaccess file in the /blog folder, which looks like this:
However this is now being ignored. I have tried to add various bits of code to the Nginx config to no avail. Please can someone fix my Nginx config to allow the Wordpress blog to work. Either by redirecting to the correct index.php, or just excluding the /blog path from Nginx and allowing Apache to do it's thing.
This is on a Plesk-controlled CentOS server, so shared hosting is involved. Here is the current NGINX config:
We support a Magento shop for which we recently enabled Nginx as a front end. All is good, however the Wordpress blog located at ./blog is being ignored, instead Magento tries to serve the location, and ends up in a 404.
I know this is due to the Nginx config not correctly redirecting for the /blog folder, but I don't know enough about Nginx to fix it.
There is a .htaccess file in the /blog folder, which looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
However this is now being ignored. I have tried to add various bits of code to the Nginx config to no avail. Please can someone fix my Nginx config to allow the Wordpress blog to work. Either by redirecting to the correct index.php, or just excluding the /blog path from Nginx and allowing Apache to do it's thing.
This is on a Plesk-controlled CentOS server, so shared hosting is involved. Here is the current NGINX config:
## These locations would be hidden by .htaccess normally
location ^~ /app/ { deny all; }
location ^~ /includes/ { deny all; }
location ^~ /lib/ { deny all; }
location ^~ /media/downloadable/ { deny all; }
location ^~ /pkginfo/ { deny all; }
location ^~ /report/config.xml { deny all; }
location ^~ /var/ { deny all; }
location /var/export/ { ## Allow admins only to view export folder
auth_basic "Restricted"; ## Message shown in login window
auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
autoindex on;
}
location /. { ## Disable .htaccess and other hidden files
return 404;
}
location @handler { ## Magento uses a common front handler
rewrite / /index.php;
}
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}
location ~ .php$ { ## Execute PHP scripts
if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
expires off; ## Do not cache dynamic content
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}
location ~ .php$ { ## Execute PHP scripts
if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
expires off; ## Do not cache dynamic content
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
if ($uri !~ "^/(media|skin|js)/"){
set $rule_1 1$rule_1;
}
if (!-f $request_filename){
set $rule_1 2$rule_1;
}
if (!-d $request_filename){
set $rule_1 3$rule_1;
}
if ($request_filename !~ "-l"){
set $rule_1 4$rule_1;
}
if ($rule_1 = "4321"){
rewrite /.* /index.php last;
}
fastcgi_read_timeout 300;
ASKER
Hi David
It's a standard Plesk Nginx / Apache setup, so Nginx is proxying requests to Apache.
Since enabling Nginx the .htaccess file in /blog is being ignored, as the Nginx config is trying to serve this back to /index.php in the Magento root.
I need to tell the config file to serve /blog direct from Apache.
It's a standard Plesk Nginx / Apache setup, so Nginx is proxying requests to Apache.
Since enabling Nginx the .htaccess file in /blog is being ignored, as the Nginx config is trying to serve this back to /index.php in the Magento root.
I need to tell the config file to serve /blog direct from Apache.
Okay this makes sense.
You're NGINX config is over ruling Apache.
If a client hired me to get this working, I'd setup Magento using Magento's normal Apache setup, so all my .htaccess processing was done at the Apache level.
This means NGINX contains no file access directives. It's only job is to proxy requests to Apache.
Keeping all your config at the Apache level will be far easier to figure out... likely a few minutes, verses hours to never, trying to mix NGINX + Apache file access directives.
You're NGINX config is over ruling Apache.
If a client hired me to get this working, I'd setup Magento using Magento's normal Apache setup, so all my .htaccess processing was done at the Apache level.
This means NGINX contains no file access directives. It's only job is to proxy requests to Apache.
Keeping all your config at the Apache level will be far easier to figure out... likely a few minutes, verses hours to never, trying to mix NGINX + Apache file access directives.
ASKER
Thanks David
Unfortunately this is not an option, it's a large Magento store with heavy traffic and >20,000 products, which requires Nginx to get any decent speed out of it.
Nginx config is working just fine, hence our need to figure out how to bypass a specific url from Nginx and serve it from Apache directly.
Kind regards
Ross
Unfortunately this is not an option, it's a large Magento store with heavy traffic and >20,000 products, which requires Nginx to get any decent speed out of it.
Nginx config is working just fine, hence our need to figure out how to bypass a specific url from Nginx and serve it from Apache directly.
Kind regards
Ross
Okay... If I had a client with this requirement, here's what I'd do.
I'd port the entire site over to an LXD container, so nothing I did effected site conversions + money.
Then I'd start through the NGINX config file + when I found a line I suspected of hijacking page request to embedded directories, I'd comment it out + test my /blog link. I'd keep doing this till I found the offending config section.
Once I had this figured out, I'd work with the identified config section till I had it fixed, then backport the fix to the production site.
This is one reason I do all my client hosting in LXD, because with LXD sites run in containers + cloning a container to do work like I just described takes roughly <1 to do a site clone + fixup DNS so I can work on site clone as live site.
LXD make debugging gnarly issues easy, because you can clone sites instantly + fix bugs + backport fixes + destroy cloned container.
If you're running native (at host/machine level rather than in a container) debugging this gets a bit tricky.
I'd likely spin up a VPS + clone your NGINX + Apache config there + go through the same debug process.
I'd port the entire site over to an LXD container, so nothing I did effected site conversions + money.
Then I'd start through the NGINX config file + when I found a line I suspected of hijacking page request to embedded directories, I'd comment it out + test my /blog link. I'd keep doing this till I found the offending config section.
Once I had this figured out, I'd work with the identified config section till I had it fixed, then backport the fix to the production site.
This is one reason I do all my client hosting in LXD, because with LXD sites run in containers + cloning a container to do work like I just described takes roughly <1 to do a site clone + fixup DNS so I can work on site clone as live site.
LXD make debugging gnarly issues easy, because you can clone sites instantly + fix bugs + backport fixes + destroy cloned container.
If you're running native (at host/machine level rather than in a container) debugging this gets a bit tricky.
I'd likely spin up a VPS + clone your NGINX + Apache config there + go through the same debug process.
Without containering, you must be very careful not to destroy your production site.
This is why I'd clone a site copy somewhere to do this work, rather than effecting a production site config.
This is why I'd clone a site copy somewhere to do this work, rather than effecting a production site config.
ASKER
Hi - thanks for the input but this is way over complicating the situation. This doesn't require moving the site or anything like that. I just need to know how to add a single line to the NGINX config.
I have worked around the problem for now by moving /blog to blog.domain.co.uk. So now all I need to do is add a reqwrite to the NGINX config that can do this.
So far I have
But it's not doing anything.
Can anyone with NGINX config knowledge advise? Many thanks.
I have worked around the problem for now by moving /blog to blog.domain.co.uk. So now all I need to do is add a reqwrite to the NGINX config that can do this.
So far I have
[code]location ^~ /blog/ {
rewrite ^/blog/(.*) $scheme://blog.onlineforequine.co.uk/$1 permanent;
break;
}
[/code]But it's not doing anything.
Can anyone with NGINX config knowledge advise? Many thanks.
https://www.digitalocean.c om/communi ty/tutoria ls/how-to- configure- nginx-as-a -reverse-p roxy-for-a pache provides a good starting point for this.
Likely way to do this, is to convert your rewrite above, to proxy your connection to Apache.
So inside your /blog rule, you'd setup your entire Apache proxy connection details.
Likely way to do this, is to convert your rewrite above, to proxy your connection to Apache.
So inside your /blog rule, you'd setup your entire Apache proxy connection details.
ASKER
Thanks David - but not relevant here. Nginx is working fine as a reverse proxy already. I don't need to add a rule to proxy to apache for /blog.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
If .htaccess is being processed at all, then Apache's in the mix somewhere.
Best to diagram your entire system, like NGINX <-> Apache <-> files or maybe NGINX serves some files + NIGNX proxies Apache to serve some files.