A
A
Artyom Zubkov2012-08-02 07:24:23
Nginx
Artyom Zubkov, 2012-08-02 07:24:23

What is the problem of config for subdomain in nginx

Good afternoon!

There is a config:

server {

        listen   80;
        listen   [::]:80 default ipv6only=on;

        server_name  my.com ~^([.+])\.my.com$;

        set $subdomain $1;

        access_log  /var/log/nginx/localhost.access.log;
        error_log  /var/log/nginx/localhost.error.log info;

        root /var/www/$subdomain;
        location / {
            index index.html index.htm;
        }

        location ~* \.(jpg|ico|gif|png|css|js|svg)$ {
            expires 30d;
        }
}


The resource name was purchased from reg.ru and the rule for all *.my.com subdomains was configured .

When going to d3.my.com, for example, I expect nginx to make root equal to /var/www/d3 , but for some reason it still breaks into /var/www/.

What could be the problem?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Alexey Sundukov, 2012-08-02
@alekciy

The problem is with the regular expression. Should be something like ([a-z0-9-\.]+), not ([.+]). The current version means " any one character and plus".

S
Stdit, 2012-08-02
@Stdit

habrahabr.ru/post/26401/

V
VBart, 2012-08-02
@VBart

nginx.org/en/docs/http/server_names.html
nginx.org/en/docs/http/converting_rewrite_rules.html
wiki.nginx.org/IfIsEvil
wiki.nginx.org/Pitfalls
man pcresyntax
The correct config would look like this:

server {
    listen 80;
    server_name www.my.com;
    return 301 http://my.com$request_uri;
}

server {
    listen 80;
    server_name ~^(?<sd>.+)\.my\.com$;
    return 301 http://my.com/$sd$request_uri;
}


server {

    listen   80; ## listen for ipv4
    listen   [::]:80 default ipv6only=on; ## listen for ipv6

    server_name  my.com;

    access_log  /var/log/nginx/artzub.access.log main;
    error_log  /var/log/nginx/artzub.error.log info;

    root /var/www;

    location / {
        index index.html index.htm;
    }

    location ~* \.(?:jpg|ico|gif|png|css|js|svg)$ {
        access_log off;
        expires 30d;
    }
}

A
Artyom Zubkov, 2012-08-02
@artzub

And for more details, you can about strace

A
Artyom Zubkov, 2012-08-02
@artzub

Thanks again to everyone for participating!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question