K
K
kazin82014-03-29 16:53:45
PHP
kazin8, 2014-03-29 16:53:45

nginx. How to setup redirect from subdomain to routing file?

Hello!
The bottom line is this: there is a bunch of Nginx + CentOS + PHP. There is a domain www.site.com.
There is an index.php router file at the root of the site. A user can register on the site, a "separate" subdomain is allocated for him.
Problem: I can't configure Nginx to redirect client.site.com to the main site's file router. Moreover, in the router file I would like to get some parameter that this is such and such a subdomain. Client folders exist, but they cannot contain their handler. Only custom images, etc. A prerequisite for setting up is that these subdomains must have their own SEO indicators (search engine indexing, traffic, and so on).
Maybe someone has already implemented this?
PS Explained not very clear. Here is an example of work:
1) The user visits www.site.com. site.com/index.php gives him the main site
2) The user registers and receives the client.site.com subdomain. The site.com/client/ folder is created for the user
3) The user goes to client.site.com... site.com/index.php - gives him the display and template of the client.site.com site
4) client.site.com is indexed search engines, etc.
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Kasymov, 2014-03-29
@kazin8

I will give only the key parts of the config:

server {
    server_name site.com www.site.com;
    root /path/to_you/site_dir;
    location ~ /user/$ {
        deny all;
        access_log off;
        log_not_found off;
    }
    location ~ \.php$ {
        ...
        fastcgi_param USER_SUBDOMAIN "_MAIN_";
        ...
    }
}
server {
    server_name "~^(?<user_subdomain>[a-z0-9_-]{2,20})\.site\.com$";
    root /path/to_you/site_dir;
    location ~ /assets/$ {
        alias /path/to_you/site_dir/user/$user_subdomain/
    }
    location ~ \.php$ {
        ...
        fastcgi_param USER_SUBDOMAIN $user_subdomain;
        ...
    }
}

The 3rd level domain name will become available in the global variable $_SERVER['USER_SUBDOMAIN'] If the _MAIN_ value is there, it will be clear that this is a 2nd level domain or www
User directories (for the visya user) are located along the path
a in the file system along the way
You can of course go further and shove everything into one server directive

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question