Subdomínio de redirecionamento NGINX para o subdiretório [closed]

1

Eu sei que isso deve ter sido respondido um milhão de vezes, mas parece que não consigo dar certo

Gostaria de redirecionar um subdomínio para um subdiretório.

Atualmente estou usando

server {
server_name sub.domain.com;
rewrite ^(.*) http://www.domain.com/sub-directory permanent;
}

Se eu navegar para sub.domain.com, serei redirecionado para o link

EDITAR

Aqui está minha configuração completa. Eu mudei os domínios e sub-diretórios reais

server {
server_name forum.domain.com;
rewrite ^(.*) http://www.domain.com/forum permanent;
}

server {
# Listen on port 80 as well as post 443 for SSL connections.
listen xx.xxx.xxx.xxx:80;


server_name domain.com www.domain.com;


# Path to the files in which you wish to
# store your access and error logs.
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

root /usr/share/nginx/www;

location /wordpress-blog {
    index index.php;
    try_files $uri $uri/ /wordpress-blog/index.php?args;
}

#root is a magento ecommerce install
location / {
    index index.htm index.php;
    try_files $uri $uri/ @handler;
}


    #this part makes all pretty urls work for my simple machines forum
location /forum/
{

    index index.php index.html;

    if (!-e $request_filename)
    {
            # Rules for: profiles
            rewrite ^/burton-snowboard-forum/profile/([^/]+)/?$ "/burton-snowboard-forum/index.php?pretty;action=profile;user=$1" last;

            # Rules for: actions
            rewrite ^/burton-snowboard-forum/(activate|admin|ads|announce|attachapprove|ban|boardrecount|buddy|calendar|clock)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(collapse|convertentities|coppa|credits|deletemsg|detailedversion|display|dlattach|editpoll|editpoll2)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(emailuser|featuresettings|findmember|groups|help|helpadmin|im|jseditor|jsmodify)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(jsoption|lock|lockvoting|login|login2|logout|manageboards|managecalendar|managesearch|manageattachments|maintain|markasread|mascot)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(membergroups|mergetopics|mlist|moderate|modifycat|modifykarma|movetopic|movetopic2|news|notify)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(notifyboard|optimizetables|openidreturn|packages|permissions|pm|post|postsettings|post2|printpage|profile|quotefast)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(quickmod|quickmod2|recent|regcenter|register|register2|reminder|removepoll|removetopic2)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(repairboards|reporttm|requestmembers|restoretopic|reports|search|search2|sendtopic|serversettings|smileys|smstats|suggest)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(spellcheck|splittopics|stats|sticky|theme|trackip|about:mozilla|about:unknown)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(unread|unreadreplies|verificationcode|viewErrorLog|viewmembers|viewprofile|vote|viewquery|viewsmfile|who)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(\.xml|xmlhttp)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;

            # Rules for: boards
            rewrite ^/burton-snowboard-forum/([-_!~*'()$a-zA-Z0-9]+)/?$ "/burton-snowboard-forum/index.php?pretty;board=$1.0" last;
            rewrite ^/burton-snowboard-forum/([-_!~*'()$a-zA-Z0-9]+)/([0-9]*)/?$ "/burton-snowboard-forum/index.php?pretty;board=$1.$2" last;

            # Rules for: topics
            rewrite ^/burton-snowboard-forum/([-_!~*'()$a-zA-Z0-9]+)/([-_!~*'()$a-zA-Z0-9]+)/?$ "/burton-snowboard-forum/index.php?pretty;board=$1;topic=$2.0" last;
            rewrite ^/burton-snowboard-forum/([-_!~*'()$a-zA-Z0-9]+)/([-_!~*'()$a-zA-Z0-9]+)/([0-9]*|msg[0-9]*|new)/?$ "/burton-snowboard-forum/index.php?pretty;board=$1;topic=$2.$3" last;

            rewrite ^/(.*)$ /index.php?params=$1 last;
    }
}

#Magento configurations

# Deny access to specific directories no one
# in particular needs access to anyways.
location /app/ { deny all; }
location /includes/ { deny all; }
location /lib/ { deny all; }
location /media/downloadable/ { deny all; }
location /pkginfo/ { deny all; }
location /report/config.xml { deny all; }
location /var/ { deny all; }

# Allow only those who have a login name and password
# to view the export folder. Refer to /etc/nginx/htpassword.
location /var/export/ {
    auth_basic "Restricted";
    auth_basic_user_file htpasswd;
    autoindex on;
}

# Deny all attempts to access hidden files
# such as .htaccess, .htpasswd, etc...
location ~ /\. {
     deny all;
     access_log off;
     log_not_found off;
}

# This redirect is added so to use Magentos
# common front handler when handling incoming URLs.
location @handler {
    rewrite / /index.php;
}

# Forward paths such as /js/index.php/x.js
# to their relevant handler.
location ~ .php/ {
    rewrite ^(.*.php)/ $1 last;
}

# Handle the exectution of .php files.
location ~ .php$ {
    if (!-e $request_filename) {
        rewrite / /index.php last;
    }
    expires off;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param HTTPS $fastcgi_https;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param MAGE_RUN_CODE btn;
    fastcgi_param MAGE_RUN_TYPE website;
    include fastcgi_params;
}

}

    
por John 12.09.2012 / 12:10

4 respostas

2

server {
    server_name sub.domain.com;
    return 301 http://www.domain.com/sub-directory$request_uri;
}
    
por 12.09.2012 / 15:59
1

Isso pode ser feito simplesmente com uma reescrita ...

if ($host !~* ^www\.example\.com) {
  rewrite ^(.*)$ http://www.example.com$1 permanent;
}

Apenas certifique-se de que sua entrada "normal" server também ouça este nome de domínio

server {

  # Listen on port 80 as well as post 443 for SSL connections.
  listen xx.xxx.xxx.xxx:80;

  server_name example.com www.example.com mysubdomain.example.com;

  location / {
    if ($host !~* ^www\.example\.com) {
      rewrite ^(.*)$ http://www.example.com$1 permanent;
    }
    ...
  }

  ...
    
por 15.09.2012 / 19:46
1

Isso deve funcionar. Por favor, leia Armadilhas do NGINX e Erros Comuns ! Você receberá de volta o tempo investido e alguns dias de graça;)

server { 
   server_name OLD.example.com;
   return 301 $scheme://example.com/NEW$request_uri;
}
    
por 21.05.2013 / 06:47
0

Acho que parte do que você está tentando fazer está sendo perdido quando você usa o sub.domain.com genérico - para enviar especificamente o subdomínio para o diretório com o mesmo nome, você tem duas opções:

Código difícil de cada um

server {
  server_name sub1.domain.com;
  return 301 "http://domain.com/sub1${uri}";
}

server {
  server_name sub2.domain.com;
  return 301 "http://domain.com/sub2${uri}";
}

Ou combine com expressões regulares (menos eficientes, mas mais fáceis)

server {
  server_name ~^(?<sub>[a-zA-Z0-9-]+)\.domain\.com$; # will cause the sub-domain to be placed in $sub
  return 301 "http://domain.com/${sub}${uri}";
}

Note que $ uri inclui a barra inicial diferente do mod_rewrite do Apache.

    
por 12.09.2012 / 12:33

Tags