nginx.conf ok - but want to redirect numeric IP to site

Igor Sysoev is at rambler-co.ru
Wed Dec 10 22:15:33 MSK 2008


On Wed, Dec 10, 2008 at 03:26:14PM +0100, Stefan Scott wrote:

> OK, fixed it - but 'listen ... default' wasn't working for some reason - 
> somehow Drupal was grabbing it anyways and redirecting to the "Install 
> Drupal" page.
> 
> Instead I followed the advice here:
> 
> https://calomel.org/nginx.html
> 
> and added this:
> 
>     ## Deny access to any host other than (www.)mydomain.com
>     server {
>          server_name  _;  #default
>          return 444;
>      }
> 
> BEFORE all the other server directives in the nginx.file - and it 
> worked!

You may set it in any place with "listen ... default":

     server {
          listen       80 default;
          server_name  _;  #default
          return 444;
      }

It seems that Drupal analyzes "Host" header (however, I do not know this).
To to redirect, you need something like this:

     server {
          listen       80 default;
          server_name  _;  #default
          rewrite      ^(.*)  http://site1$1;
     }

BTW, this configuration part is not good:

    location / {
        root   /usr/local/nginx/html/site1;
        index  index.php index.html;

        if (!-e $request_filename) {
            rewrite  ^/(.*)$  /index.php?q=$1  last;
            break;
        }
    }

    location ~ \.php$ {
        root   html/site1;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include /usr/local/nginx/conf/fastcgi_params;
    }

It's better to use:

    location / {
        root   /usr/local/nginx/html/site1;
        index  index.php index.html;

        log_not_found   off;
        error_page  404 = /index.php?q=$1;
    }

    location ~ \.php$ {
        root   html/site1;
        fastcgi_pass 127.0.0.1:9000;
        include /usr/local/nginx/conf/fastcgi_params;
    }

And the best way is:

    location / {
        root   /usr/local/nginx/html/site1;
        index  index.php index.html;

        log_not_found   off;
        error_page  404 = @drupal;
    }

    location @drupal {
        root   html/site1;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME /usr/local/nginx/html/site1/index.php;
        fastcgi_param  QUERY_STRING    q=$request_uri;
        include /usr/local/nginx/conf/fastcgi_params1;
    }

    # needed only if there are some other .php files except /index.php
    location ~ \.php$ {
        root   html/site1;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME /usr/local/nginx/html/site1$uri;
        fastcgi_param  QUERY_STRING    $query_string;
        include /usr/local/nginx/conf/fastcgi_params1;
    }

Note, that included /usr/local/nginx/conf/fastcgi_params1 must not
contain SCRIPT_FILENAME and QUERY_STRING parameters.


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





More information about the nginx mailing list