vhost issues and rewrite rules

Igor Sysoev is at rambler-co.ru
Wed Aug 5 15:38:08 MSD 2009


On Wed, Aug 05, 2009 at 12:16:10PM +0100, Anselm R Garbe wrote:

> Hi there,
> 
> I switched to nginx 0.7.61 recently and came across some odd vhost issues:
> 
> 
> 1. Let's imagine the following cgi script:
> 
> --
> #!/bin/sh
> echo Content-Type: text/plain
> echo
> env | grep SERVER_NAME
> --
> 
> that is served using fcgiwrap as follows in the nginx.conf:
> 
>         server {
>                 listen       8080;
>                 server_name  a.dom.tld dom.tld b.dom.tld c.dom.tld;
>                 location / {
>                         root /; # html results in 403
>                         fastcgi_pass   unix:/var/run/fastcgi/test.sock;
>                         fastcgi_param  QUERY_STRING       $query_string;
>                         fastcgi_param  REQUEST_METHOD     $request_method;
>                         fastcgi_param  CONTENT_TYPE       $content_type;
>                         fastcgi_param  CONTENT_LENGTH     $content_length;
> 
>                         fastcgi_param  SCRIPT_NAME
> /usr/local/bin/printenv;
>                         fastcgi_param  REQUEST_URI        $request_uri;
>                         fastcgi_param  DOCUMENT_URI       $document_uri;
>                         fastcgi_param  DOCUMENT_ROOT      $document_root;
>                         fastcgi_param  SERVER_PROTOCOL    $server_protocol;
> 
>                         fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
>                         fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
> 
>                         fastcgi_param  REMOTE_ADDR        $remote_addr;
>                         fastcgi_param  REMOTE_PORT        $remote_port;
>                         fastcgi_param  SERVER_ADDR        $server_addr;
>                         fastcgi_param  SERVER_PORT        $server_port;
>                         fastcgi_param  SERVER_NAME        $server_name;
>                 }
>         }
> 
> The result is always the first server_name entry, regardless if one
> access dom.tld, c.dom.tld or b.dom.tld -- it's always a.dom.tld in the
> given example. So I suspect that $server_name is incorrect. A bug?
> 
> (I currently have a separate server { ... } section for each
> server_name, but I think that's not very great).

          fastcgi_param  SERVER_NAME        $host;

> 2. A second issue is, that the above cgi script is only executed
> correctly for me if the server setting:
> 
>      root /;
> 
> is given. If the root is anything else, I always get 403 errors, why
> is that? Is this a bug?

I do not know. Probably, fcgiwrap concatenates DOCUMENT_ROOT and SCRIPT_NAME.

> 3. Finally let's assume I have two domains dom1.tld and dom2.tld and
> let's imagine a bunch of sub-domains each:
> 
>      a.dom1.tld, b.dom1.tld, a.dom2.tld, and b.dom2.tld
> 
> Now my requirement is, that [{a,b}.]dom2.tld is ALWAYS redirected to
> [{a,b}.]dom1.tld. My current solution is this:
> 
>     server {
>         server_name  dom2.tld;
>         rewrite      ^(.*) http://dom1.tld$1 permanent;
>     }
> 
>     server {
>         server_name  a.dom2.tld;
>         rewrite      ^(.*) http://a.dom1.tld$1 permanent;
>     }
> 
>     server {
>         server_name  b.dom2.tld;
>         rewrite      ^(.*) http://b.dom1.tld$1 permanent;
>     }
> 
> How can I achieve this with a simpler setup?

     server {
          server_name  ~^(.*dom)2.tld;
          set          $name  $1;
          rewrite      ^  http://${name}1.tld$request_uri? permanent;
     }


-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list