Nginx.conf ok - but want to redirect numeric IP to sitename


#1

Hi -

I installed nginx + PHP-FPM, and set up multiple Drupal sites using the
“more secure” directory layout following the instructions from Justin
Hileman:

http://justinhileman.info/blog/2007/06/a-more-secure-drupal-multisite-install

So far I’ve only configured site1.com to use Drupal, and site2.com and
site3.com display “Welcome to Nginx” for now, which is fine, as I’ll be
configuring them to run Ruby on Rails sites.

To configure my nginx.conf file, I followed the instructions on these
two sites:

http://www.codegobbler.com/drupal-nginx-fastcgi-setup-and-configuration

My current nginx.conf file is shown below.

Now as an additional test, I tried pointing my browser at my numeric IP
address, eg:

http://208.77.188.166/ ## this is the official IP address of
example.com

I hoped it would go by default to the first site in my nginx.conf file
(site1.com) - but instead it goes to my Drupal installation page.

I don’t want to expose this Drupal installation page to the public.

How do I change my ningx.conf file so that entering a numeric IP address
in my browser will go to the first site in my nginx.conf file
(site1.com)?

Here’s my nginx.conf file:

user www-data www-data;
worker_processes 2;

error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local]

$request ’
# '"$status" $body_bytes_sent “$http_referer” ’
# ‘"$http_user_agent" “$http_x_forwarded_for”’;

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

keepalive_timeout  65;

gzip on;
gzip_comp_level 1; gzip_proxied any;
gzip_types text/plain
           text/html
           text/css
           application/x-javascript
           text/xml
           application/xml
           application/xml+rss
           text/javascript;

server {
    listen       80;
    server_name  .site1.com;

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

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

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

    # hide protected files
    location ~*

.(engine|inc|info|install|module|profile|po|sh|.sql|theme|tpl(.php)?|xtmpl)$|^(code-style.pl|Entries.|Repository|Root|Tag|Template)$
{
deny all;
}

    # hide backup_migrate files
    location ~* ^/files/backup_migrate {
        deny all;
    }

    # serve static files directly
    location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico)$ {
        root /usr/local/nginx/html/site1;
        access_log        off;
        expires           30d;
        break;
    }

    error_page  404              /index.php;

    error_page   500 502 503 504  /50x.html;

    location = /50x.html {
        root   html;
    }

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

server {
    listen       80;
    server_name  .site2.com;

    location / {
        root   html/site2;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
server {
    listen       80;
    server_name  .site3.com;

    location / {
        root   html/site3.com;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

}

Thanks!


#2

try searching the archives or checking the wiki :slight_smile:

what you want is “listen … default;” in the server {}.


#3

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!

Thanks!


#4

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

## 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.