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

2838897dc87bed42bcd71348bb90bd04?d=identicon&s=25 Pascale Camille (pascalecamille)
on 2014-08-13 22:43
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 ~^(?<user>.+)\.2maxi\.com$;
root /var/www/$user;
    return       301 http://~^(?<user>.+)\.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://~^(?<user>.+)\.2maxi\.com
36a8284995fa0fb82e6aa2bede32adac?d=identicon&s=25 Francis Daly (Guest)
on 2014-08-14 01:44
(Received via mailing list)
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 Daly        francis@daoine.org
2838897dc87bed42bcd71348bb90bd04?d=identicon&s=25 Pascale Camille (pascalecamille)
on 2014-08-14 09:12
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;
}
2838897dc87bed42bcd71348bb90bd04?d=identicon&s=25 Pascale Camille (pascalecamille)
on 2014-08-14 09:34
if i remove
return       301 http://~^(?<user>.+)\.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$\(?<user>.+)
www.2maxi\.com$\(?<user>.+);

    return       301 http://www.2maxi.com$request_uri;
}
server {
listen 80;
 charset    utf-8;
 source_charset  utf-8;

server_name ~^(?<user>.+)\.2maxi\.com$;
root /var/www/$user;
    return       301 http://~^(?<user>.+)\.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;
}
36a8284995fa0fb82e6aa2bede32adac?d=identicon&s=25 Francis Daly (Guest)
on 2014-08-14 11:01
(Received via mailing list)
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<user>.+).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 Daly        francis@daoine.org
2838897dc87bed42bcd71348bb90bd04?d=identicon&s=25 Pascale Camille (pascalecamille)
on 2014-08-14 16:33
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/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.2.1</center>
</body>
</html>
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 ~^(?<user>.+)\.2maxi\.com$;
2974d09ac2541e892966b762aad84943?d=identicon&s=25 bodomic (Guest)
on 2014-08-14 16:48
(Received via mailing list)
Hi, you may find this overkill configuration helpful:
http://publications.jbfavre.org/web/nginx-vhosts-a...

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,252553,252577#msg-252577
2838897dc87bed42bcd71348bb90bd04?d=identicon&s=25 Pascale Camille (pascalecamille)
on 2014-08-14 16:51
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?
2838897dc87bed42bcd71348bb90bd04?d=identicon&s=25 Pascale Camille (pascalecamille)
on 2014-08-14 17:55
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/
36a8284995fa0fb82e6aa2bede32adac?d=identicon&s=25 Francis Daly (Guest)
on 2014-08-14 19:23
(Received via mailing list)
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 Daly        francis@daoine.org
2838897dc87bed42bcd71348bb90bd04?d=identicon&s=25 Pascale Camille (pascalecamille)
on 2014-08-14 20:27
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-a...

it works, but how to make it works?
2974d09ac2541e892966b762aad84943?d=identicon&s=25 bodomic (Guest)
on 2014-08-15 09:23
(Received via mailing list)
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:
http://forum.nginx.org/read.php?2,252553,252585#msg-252585
2838897dc87bed42bcd71348bb90bd04?d=identicon&s=25 Pascale Camille (pascalecamille)
on 2014-08-15 17:44
thanks to all,
'perhaps' i solved my problem by adding in my DNS record:

* CNAME 14440 2maxi.com

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...
2838897dc87bed42bcd71348bb90bd04?d=identicon&s=25 Pascale Camille (pascalecamille)
on 2014-08-19 13:09
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 !!!
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.