[not solved but manualy done]'auto'rewrite link to a folder

Hi all,
i am looking for a ‘simple rewrite rule’ like typing :
FOLDER.2maxi.com
will point the folder /var/www/FOLDER/

and the folder is create by ngninx user, with winscp.

my old vps debian6, nginx-1.2.6+php, used to work fast and perfectly
with /etc/nginx/host.d/main.conf :


server {
listen 80;
charset utf-8;
source_charset utf-8;

server_name ~^(?.+).2maxi.com$;
root /var/www/$user;
return 301 http://~^(?.+).2maxi.com$request_uri;

index 2maxi.com.html index.php;

error_page 405 /405.html;
location = /405.html {
rewrite . http://www.2maxi.com/2maxi.com.gif redirect;
allow all;
}
error_page 400 401 402 403 404 406 407 408 409 410 411 412 413 414 415
416 417 495 496 497 500 501 502 503 504 505 506 507 /error_page.html;
location /error_page.html {
rewrite . http://www.2maxi.com/ redirect;
allow all;
}

include /etc/nginx/conf.d/cache.conf;
include /etc/nginx/conf.d/deny.conf;
include /etc/nginx/conf.d/php.conf;
}

I reinstalled my old config under actual nginx and debian 7
when i type FOLDER.2maxi.com
web browser rewrote :
http://~^(?.+).2maxi.com

On Wed, Aug 13, 2014 at 10:43:24PM +0200, Pascale Camille wrote:

Hi there,

my old vps debian6, nginx-1.2.6+php, used to work fast and perfectly
with /etc/nginx/host.d/main.conf :

return       301 http://~^(?<user>.+)\.2maxi\.com$request_uri;

Are you sure this used to work on the old server?

That line suggests that it will return exactly what you report it does
return.

It is not obvious to me what that line is intended to do. What happens
if you just remove it?

f

Francis D. [email protected]

hey!
i confirmed it used to work greatly from 08/2012 to 06/2014
i did no vps reboot in this laps of time.
i was working as teacher and needed subdomain easy
create or delete in one minute!
avoiding me to do subdomain process…
(no database site)

maybe it needs a custom module to be select/unselect
at compilation of nginx…
and google found subdomain if search contain specifics theme/words.

i have userdir.conf (maybe useless because i point on /var/www):
location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/http$2;
autoindex on;
}

On Thu, Aug 14, 2014 at 09:12:39AM +0200, Pascale Camille wrote:

Hi there,

i confirmed it used to work greatly
i was working as teacher and needed subdomain easy
create or delete in one minute!
avoiding me to do subdomain process…
(no database site)

I don’t see how what you provided could ever have worked as you
describe.

However, that does not matter: the following skeleton seems to work for
me; add the rest of your config in as needed:

Preparation:

$ mkdir -p /tmp/www/{one,two,three}
$ for i in /tmp/www/{one,two,three}; do
echo “content of $i/index.html” > $i/index.html; done

nginx.conf server block:

server {
server_name ~^(?P.+).example.com;
root /tmp/www/$user;
}

Note that I use the “(?P<” syntax for the named capture; that’s what my
pcre engine needs.

Then test:

$ curl -H Host:one.example.com http://localhost/
content of /tmp/www/one/index.html
$ curl -H Host:two.example.com http://localhost/
content of /tmp/www/two/index.html
$ curl -H Host:nil.example.com http://localhost/

which gives “404 Not Found”, as expected.

f

Francis D. [email protected]

if i remove
return 301 http://~^(?.+).2maxi.com$request_uri;
of course i have just www.domain.tld access,
and cant access to something like 2maxi.com/subdomain
as subdomain folder is at the same level as domain folder
(not inside i meam)

server {
listen 80;
charset utf-8;
source_charset utf-8;
server_name www.2maxi.com;
root /var/www/www;
index 2maxi.com.html index.php;

error_page 405 /405.html;
location = /405.html {
rewrite . http://www.2maxi.com/2maxi.com.gif redirect;
allow all;
}
error_page 400 401 402 403 404 406 407 408 409 410 411 412 413 414 415
416 417 495 496 497 500 501 502 503 504 505 506 507 /error_page.html;
location /error_page.html {
rewrite . http://www.2maxi.com/ redirect;
allow all;
}
include /etc/nginx/conf.d/cache.conf;
include /etc/nginx/conf.d/deny.conf;
include /etc/nginx/conf.d/php.conf;

include /etc/nginx/conf.d/limit.conf;

}
server {
listen 80;
server_name 2maxi.com 198.23.165.81 .2maxi.com$(?.+)
www.2maxi.com$(?.+);

return       301 http://www.2maxi.com$request_uri;

}
server {
listen 80;
charset utf-8;
source_charset utf-8;

server_name ~^(?.+).2maxi.com$;
root /var/www/$user;
return 301 http://~^(?.+).2maxi.com$request_uri;

index 2maxi.com.html index.php;

error_page 405 /405.html;
location = /405.html {
rewrite . http://www.2maxi.com/2maxi.com.gif redirect;
allow all;
}
error_page 400 401 402 403 404 406 407 408 409 410 411 412 413 414 415
416 417 495 496 497 500 501 502 503 504 505 506 507 /error_page.html;
location /error_page.html {
rewrite . http://www.2maxi.com/ redirect;
allow all;
}

include /etc/nginx/conf.d/cache.conf;
include /etc/nginx/conf.d/deny.conf;
include /etc/nginx/conf.d/php.conf;

include /etc/nginx/conf.d/limit.conf;

}

hi,
thanks for mini lesson of batch command useful in futur.
hum, i did what you wrote
i rebuilded vps with debian 7
remove apache
just fail to restart nginx editing nginx.conf
so i put in /etc/nginx/sites-available/default

and i am beat confused:

root@www:/# curl -H Host:one.2maxi.com http://localhost/
content of /tmp/www/one/index.html
root@www:/# curl -H Host:nil.2maxi.com http://localhost/

404 Not Found

404 Not Found


nginx/1.2.1 root@www:/#

so in web browser i should see
content of /tmp/www/one/index.html

but i have 'this page not available … DNS lookup failed"

bonus :
-2maxi.com/nil or what else gives 2maxi.com
-nginx/1.2.1 even with apt-get update…
-curl gave the same with server_name ~^(?.+).2maxi.com$;

hi,
yes i already read month ago
but i can not make it works
i wrote to the autor,

no news …

@bodomic, in this /etc/nginx/sites-available/domain.tld
i renamed everything in ‘domain.tld’ with ‘2maxi.com

did it was right?

Hi, you may find this overkill configuration helpful:
http://publications.jbfavre.org/web/nginx-vhosts-automatiques-avec-SSL-et-authentification.en

Posted at Nginx Forum:

hi,
@bodomic, i just did again and remembered error, (ssl not implemented)
in webrowser it rewrote in loop:
http://198.23.139.108//www.2maxi.com/www.2maxi.com/www.2maxi.com
/www.2maxi.com/www.2maxi.com/www.2maxi.com/www.2maxi.com/www.2maxi.com
/www.2maxi.com/www.2maxi.com/www.2maxi.com/www.2maxi.com/www.2maxi.com
/www.2maxi.com/www.2maxi.com/www.2maxi.com/www.2maxi.com/www.2maxi.com
/www.2maxi.com/www.2maxi.com/

yep you are rigth DNS declaration,
so it become standard vhost…
i wonder why it did work without A

maybe it was not real subdomain :
pointing a folder from a volatil url
and rewriting this url permanetly…

as my little experiment
and
http://publications.jbfavre.org/web/nginx-vhosts-automatiques-avec-SSL-et-authentification.en

it works, but how to make it works?

I think your current config is too complicated now so that your regular
expressions do not do what you think they should.

Try a separate server setup in this manner:
server {
listen 80 default;
server_name .example.com;

    set $project www;
    set $user www-data;


    if ($host ~* ^([^.]+)\.([^.]+)\.example\.com$) {
        set $project $1;
        set $user $2;
    }

if (!-d /home/$user/sites/$project/public) { rewrite ^(.*)$
http://www.example.com permanent; }
root /home/$user/sites/$project/public;
index index.php index.htm index.html;
}

Create some dns names like user1.www.example.com (or just put them to
your
local hosts file) all pointing to the same host.
Create folders /home/user1/sites/www/public and put index.html stating
“This
is a home of user1” there.
Make sure you have access and error logs.

It should work.

Posted at Nginx Forum:

On Thu, Aug 14, 2014 at 04:33:04PM +0200, Pascale Camille wrote:

Hi there,

root@www:/#
So: they both say that if the request gets to nginx, nginx responds as
you want it to.

so in web browser i should see
content of /tmp/www/one/index.html

…if the browser makes the request of nginx.

but i have 'this page not available … DNS lookup failed"

And that says that the browser did not know where nginx was.

You need your browser to be able to turn the hostnames one.2maxi.com and
nil.2maxi.com (and any others that you will use) into the IP address of
your nginx server.

This is usually either “set up dns right”, or “populate your browser
machines etc/hosts file” – but it might be “get resolution working on
your proxy server” or something else instead.

It’s outside the scope of nginx, but may be useful to add to DNS many
individual A records, or a single wildcard one, for the hostnames that
you care about.

and 2maxi.com/nil or what else gives 2maxi.com

That suggests that your browser can resolve 2maxi.com to the nginx
server,
and hopefully it is clear from your configuration why the response was
what it was.

f

Francis D. [email protected]

up
for me tuxlite or other script made me crazy
-i could not modify script or nginx conf without crashing
-as strange it is, i could not make php working !

so the last working solution for me was a site that
pre-build a script with my parameters :
http://simpleserversetup.com/

and i modify the downloaded script modding all vhost the way i need

this way i understood what i have done !!!

thanks to all,
‘perhaps’ i solved my problem by adding in my DNS record:

and i used a script to create subdomain from tuxlite.com
(before DNS modification, no script worked)

of course i’d preferred to understand what it did
but no time to, site down for 6 monthes…