outroll / vesta

VESTA Control Panel
http://vestacp.com
GNU General Public License v3.0
2.97k stars 1.03k forks source link

Nginx Multiple domains force www config #1070

Open Niklan opened 7 years ago

Niklan commented 7 years ago

Operating System (OS/VERSION): CentOS 7.2 VestaCP Version: 0.9.8 Installed Software (what you got with the installer): php-fpm, nginx, mysql

Hi, sorry for posting my issue here, but srsly, your registration on forum is crazy. This drag tests is always fails.

Anyway. I have the case which cause problem on vesta configs. I have the site, wich has two different domains.

I created web-domain for the main domain of the site, and second is set as alias. All works fine on my custom nginx configs. But this site has SSL, f.e. I enabled LetsEncrypt and again, all works fine. But at the end, this two sites has www. prefix as main site. I need to redirect users from non-www to www site and this is the problem.

I tried to created custom template with just include custom config from /home/%user%/conf/web/domain.custom but i get error "server" directive is not allowed here.

I tried to create custom template just for this site, and this sites start redirect to another site on the server of different user.

I tried to set web-domain with www prefix, but vesta move it in aliases.

Thats my config i tried to create:

#custom.tpl
server {
    listen      %ip%:%web_port%
    server_name domain.com;
    return      302 https://www.domain.com$request_uri;
}

server {
    listen      %ip%:%web_port%;
    server_name domain2.com;
    return      302 https://www.domain2.com$request_uri;
}
#custom.stpl
server {
    listen      %ip%:%web_ssl_port% ssl http2;
    server_name www.domain.com www.domain2.com;
    root        %docroot%;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/%domain%.log combined;
    access_log  /var/log/nginx/domains/%domain%.bytes bytes;
    error_log   /var/log/nginx/domains/%domain%.error.log error;

    ssl         on;
    ssl_certificate      %ssl_pem%;
    ssl_certificate_key  %ssl_key%;

   …

Is there a way to make such thigs globaly, just single template for all other sites, or each site with force www prefix is required custom templates?

Skamasle commented 7 years ago

You not need a /home/%user%/conf/web/domain.custom ( of course you can do it )

But you can just create new template in /usr/local/vesta/data/templates/web/ (not remember full path for nginx )

Whit something like this

server {
    listen      %ip%:%web_port%;
    server_name %domain_idn% ;
return      302 https://www.%domain_idn%$request_uri;
}
server {
    listen      %ip%:%web_port%;
    server_name  %alias_idn%; # or  server_name www.%domain_idn% ;
    root        %docroot%;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/%domain%.log combined;
    access_log  /var/log/nginx/domains/%domain%.bytes bytes;
    error_log   /var/log/nginx/domains/%domain%.error.log error;

    location / {

        location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
            expires     max;
        }

        location ~ [^/]\.php(/|$) {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            if (!-f $document_root$fastcgi_script_name) {
                return  404;
            }

            fastcgi_pass    %backend_lsnr%;
            fastcgi_index   index.php;
            include         /etc/nginx/fastcgi_params;
        }
    }

    error_page  403 /error/404.html;
    error_page  404 /error/404.html;
    error_page  500 502 503 504 /error/50x.html;

    location /error/ {
        alias   %home%/%user%/web/%domain%/document_errors/;
    }

    location ~* "/\.(htaccess|htpasswd)$" {
        deny    all;
        return  404;
    }

    location /vstats/ {
        alias   %home%/%user%/web/%domain%/stats/;
        include %home%/%user%/web/%domain%/stats/auth.conf*;
    }

    include     /etc/nginx/conf.d/phpmyadmin.inc*;
    include     /etc/nginx/conf.d/phppgadmin.inc*;
    include     /etc/nginx/conf.d/webmail.inc*;

    include     %home%/%user%/conf/web/nginx.%domain_idn%.conf*;
}

Not copy paste this, just take as idea.

Niklan commented 7 years ago

But how i mange two different domains, each of them must force www and open it.

Drupal handle those two domains as language to show. So them are show different content but core and site the same.

I have domain.com and domain2.com. In Vesta i set up domain.com and set aliases: www.domain.com domain2.com and www.domain2.com

I need to force www.domain.com and www.domain2.com with https. With this config this is not posible. But then i try to create template with hardcoded domains Vesta can't parse it after second save, and this is not working, after that server start redirect to site of other user, and i don't know why.

Niklan commented 7 years ago

Seems i can handle this with custom template just for this site.

#TPL
server {
    listen      %ip%:%web_port%;
    server_name domain1.com www.domain1.com;
    return      302 https://www.domain1.com$request_uri;
}

server {
    listen      %ip%:%web_port%;
    server_name domain2.com www.domain2.com;
    return      302 https://www.domain2.com$request_uri;
}
#STPL

server {
    listen      %ip%:%web_ssl_port% ssl http2;
    server_name domain1.com;
    return      302 https://www.domain1.com$request_uri;
}

server {
    listen      %ip%:%web_ssl_port% ssl http2;
    server_name domain2.com;
    return      302 https://www.domain2.com$request_uri;
}

server {
    listen      %ip%:%web_ssl_port% ssl http2;
    server_name www.domain1.com www.domain2.com;
    root        %docroot%;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/%domain%.log combined;
    access_log  /var/log/nginx/domains/%domain%.bytes bytes;
    error_log   /var/log/nginx/domains/%domain%.error.log error;

    ssl         on;
    ssl_certificate      %ssl_pem%;
    ssl_certificate_key  %ssl_key%;

    … rest of the file
}

So thats seems Vesta can't handle templates with forced www in domain and multiple domains as aliases.

P.s. if someone try to do so, this template must be enabled after first generation of LetsEncrypt SSL.