K
K
kfuntov2014-08-07 11:56:03
Nginx
kfuntov, 2014-08-07 11:56:03

How to "allocate" a separate location to a subdomain?

There is a working rather large site (many different settings in nginx).
There is a task:
1) Make sure that when you visit site.com/forum/* there is a redirect to forum.site.com/forum/*
2) Make sure that when you go to forum.site.com/* (where * - what anything except /forum/*) there was a redirect to site.com/*
Site in php, part of the settings (which currently process /forum/* requests):

server
{
server_name site site.com;
location /
{
    if ( !-f $request_filename) {
        rewrite .* /index.php last;
    }
    root   /sitedir/htdocs;
    index  index.php;
}


location ~ \.php$
{
    gzip on;
    if ( !-f $request_filename) {
        rewrite .* /index.php last;
    }
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_index   index.php;
    fastcgi_connect_timeout 5;
    fastcgi_buffer_size 32k;
    fastcgi_buffers 8 16k;
    fastcgi_param   DOCUMENT_ROOT   /sitedir/htdocs;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param   PATH_INFO       $fastcgi_script_name;
    include         fastcgi_params;
    open_file_cache off;
}

Tried changing server_name to
~^(?<forum>forum\.)site\.com$
and adding location:
location ^~ /forum
{
    if ($forum != "forum.") {
        rewrite ^(.*) http://forum.site.com$1 permanent;
    }
    rewrite .* /index.php last;
}

But this did not solve the problem:
You can go to forum.site.com/something/

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2014-08-07
@kfuntov

No need to try to cram everything into one server block.

server {
    server_name site site.com;
    root /sitedir/htdocs;
    index  index.php;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location ^~ /forum/ {
        return 301 http://forum.site.com$request_uri;
    }

    location ~ \.php$ {
        try_files $uri /index.php;
        gzip on;
        fastcgi_pass    127.0.0.1:9000;
        .....
    }
}

server {
    server_name forum.site.com;
    root /sitedir/htdocs;

    location / {
        return 301 http://site.com$request_uri;
    }

    location /forum/ {
        rewrite ^ /index.php break;
        # fastcgi directives;
        .....
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question