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

**URL:** <https://www.ruby-forum.com/t/nginx-conf-ok-but-want-to-redirect-numeric-ip-to-sitename/155287>\
**Category:** NGINX\
**Created:** [December 10, 2008, 6:12am UTC](https://www.ruby-forum.com/t/nginx-conf-ok-but-want-to-redirect-numeric-ip-to-sitename/155287 "2008-12-10T06:12:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@stefans](https://www.ruby-forum.com/u/stefans)\
**Post date:** [December 10, 2008, 6:12am UTC](https://www.ruby-forum.com/t/nginx-conf-ok-but-want-to-redirect-numeric-ip-to-sitename/155287/1 "2008-12-10T06:12:07Z")

</div>

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](http://justinhileman.info/blog/2007/06/a-more-secure-drupal-multisite-install)

So far I’ve only configured [site1.com](http://site1.com) to use Drupal, and [site2.com](http://site2.com) and  
[site3.com](http://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](http://www.codegobbler.com/drupal-nginx-fastcgi-setup-and-configuration)  
[http://drupal.org/node/110224](http://drupal.org/node/110224)

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/](http://208.77.188.166/) ## this is the official IP address of  
[example.com](http://example.com)

I hoped it would go by default to the first site in my nginx.conf file  
([site1.com](http://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](http://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!

---

<div class="post-metadata">

**Author:** ![stefans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@stefans](https://www.ruby-forum.com/u/stefans)\
**Post date:** [December 10, 2008, 3:02pm UTC](https://www.ruby-forum.com/t/nginx-conf-ok-but-want-to-redirect-numeric-ip-to-sitename/155287/2 "2008-12-10T15:02:44Z")

</div>

try searching the archives or checking the wiki 🙂

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

---

<div class="post-metadata">

**Author:** ![stefans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@stefans](https://www.ruby-forum.com/u/stefans)\
**Post date:** [December 10, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/nginx-conf-ok-but-want-to-redirect-numeric-ip-to-sitename/155287/3 "2008-12-10T15:26:14Z")

</div>

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](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!

---

<div class="post-metadata">

**Author:** ![stefans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@stefans](https://www.ruby-forum.com/u/stefans)\
**Post date:** [December 10, 2008, 8:27pm UTC](https://www.ruby-forum.com/t/nginx-conf-ok-but-want-to-redirect-numeric-ip-to-sitename/155287/4 "2008-12-10T20:27:50Z")

</div>

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.
