NGINX Rewrite help needed
I have 2 questions for nginx users.
1) I'm trying to setup my joomla server onto my new linode running NGINX and after much (like days) of searching and testing, I finally have a config that works with with SEF url plugins…sorta. I was using an apache system on the old server and it used modrewrite and life was fine in terms of SEF. Since NGINX doesn't have modrewrite, I found something that works BUT it constantly leaves index.php in the urls.
ex:
i want it to be just
Here is my config file:
server {
listen 80;
server_name mysite.com www.mysite.com;
access_log /home/public_html/mysite.com/log/access.log;
error_log /home/public_html/mysite.com/log/error.log;
root /home/public_html/mysite.com/public/;
large_client_header_buffers 4 8k; # prevent some 400 errors
index index.php index.html;
fastcgi_index index.php;
location / {
expires 30d;
error_page 404 = @joomla;
log_not_found off;
}
# Rewrite
location @joomla {
rewrite ^(.*)$ /index.php?q=last;
}
# Static Files
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires 30d;
}
# PHP
location ~ \.php {
keepalive_timeout 0;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /home/public_html/mysite.com/public/$fastcgi_script_name;
}
}
2) second question should be easy but i can't get it to work.
I want to use the same config I posted above and have either mysite.com or
Basically when you hit up the front page with or without the www, it all gets forwarded to a sub directory on the server I called Portal.
I have tried several variations of:
rewrite ^/(.*) http://www.example.com/portal/$1 permanent;
but it usually ends with firefox telling me there is some crazy loop happening the address bar saying something like mysite.com/portalportalportalportalportal………on and on.
So, any help on either of these issues would be awesome!!
Thanks!!
13 Replies
2)
Try this
rewrite / /portal permanent;
that should rewrite just / which is the root of the site but it won't rewrite /index.php or /whatever.
########## Begin - Rewrite rules to block out some common exploits
## If you experience problems on your site block out the operations listed below
## This attempts to block the most common type of exploit `attempts` to Joomla!
#
## Deny access to extension xml files (uncomment out to activate)
# <files ~="" "\.xml$"="">#Order allow,deny
#Deny from all
#Satisfy all
#</files>
## End of deny access to extension xml files
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
# Block out any script trying to base64_encode crap to send via URL
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
# Block out any script that includes a
seems like it should be fairly easy to have mysite.com and
alas it's beyond me.
You want something like:
rewrite ^/(.*)
except with nginx you want to put that in an "if" block and with apache you want to add a rewritecond
Basically, you only want to do that rewrite if the host part of the request is not already
otherwise, you'll redirect the visitor every they make a request (infinite loop which apparently firefox detected)
if ($host != www.example.com) {
rewrite ^/(.*)$ http://www.example.com/$1 permanent;
}
if ($request_uri !~ ^/portal/.*$) {
rewrite ^/(.*)$ http://www.example.com/portal/$1 permanent;
}
untested but that should work.
example.com takes me to the default "Welcome to nginx!" page.
neither of them redirected to /portal
########## Begin - Joomla! core SEF Section
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
Should translate into:
if (!-e $request_filename)
{
set $redirect 'y';
}
if ($request_uri ~ (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$)
{
set $redirect $redirect'y';
}
if ($request_uri ~ ^/index.php)
{
set $redirect '';
}
if ($redirect = 'yy')
{
rewrite (.*) /index.php;
}
As for your portal problem, does the /portal directory actually exist and if so what's it's index file?
yes, portal does actually exist. portal is actually where my joomla installation sits (as opposed to my root) so index.php is the main file in there there.
What's in the root directory?
but yeah…nothing in root except a few audio files
server {
listen 80;
server_name mysite.com www.mysite.com;
access_log /home/public_html/mysite.com/log/access.log;
error_log /home/public_html/mysite.com/log/error.log;
root /home/public_html/mysite.com/public/;
large_client_header_buffers 4 8k; # prevent some 400 errors
index index.php index.html;
fastcgi_index index.php;
#redirect mysite.com and www.mysite.com to /portal
location = / {
rewrite (.*) /portal last;
}
#pass php files to fastcgi
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /home/public_html/mysite.com/public/$fastcgi_script_name;
#set expires to 0 to stop caching of php files
expires 0;
}
#files in the /portal dir
location ~ ^/portal
{
#set expires to 30d to cache static files
expires 30d;
#file doesn't exist, will need to be sent to php
if (!-e $request_filename)
{
rewrite ^/portal/(.*)$ /index.php?q=$1 last;
}
}
}
I've not tested it (since I don't have joomla) but it "should" work. If it doesn't then I'd have to have a look at your setup to get it to work.
now i gotta sort out how to make my SEF urls work properly but i think i need someone with joomla background to do that. Basicly, I think i have to trick joomla into think mod_rewrite is there so it takes index.php out of the urls like it does for wordpress.