Forum: NGINX New to nginx

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7ff276b9a72c9fc0492dec907e272a31?d=identicon&s=25 Ivan L. (Guest)
on 2009-02-28 13:30
(Received via mailing list)
Hi,

I'm new to nginx, and I'm trying to configure it but I've some problems.
I use Ubuntu 8.10, and I installed nginx 0.6.32(apt-get install nginx)

The problems I see;

- unknown directive "if($request_method"

If I remove these statements, the error I receive

- unknown directive "if(-d"

When I remove if(-d) statement, the error I receive;

- unknown directive "if(!-e)"

I've tried to configure it as described in the English wiki, and some
blogs I've seen but I couldn't solve the problems. Can anyone help me?

Here's my /etc/nginx/sites-availabe/default config file;


server {
  listen 80;
  client_max_body_size 50M;
  client_body_buffer_size 128k;
  charset utf-8;

  root /var/www/myweb;
  index index.php index.html index.htm;

  access_log /var/log/nginx/access.log;

  location / {
    if($request_method !~ GET) {
      return 405;
    }
    if(-f $request_filename) {
      break;
    }
    if(-d $request_filename) {
      break;
    }
    if(!-e $request_filename) {
      rewrite ^/search$ /index.php?where=search last;
      rewrite ^/posts/([0-9])$ /index.php?posts=$1 last;

      break;
    }
  }

  location /about {
    if($request_method !~ GET) {
      return 405;
    }
    if(!-e $request_filename) {
      expires max;
      rewrite ^/about$ /index.php?where=about break;
    }
  }

  location /contact {
    if($request_method !~ ^(GET|POST)$) {
      return 405;
    }
    if(!-e $request_filename) {
      expires max;
      rewrite ^/contact$ /index.php?where=contact break;
    }
  }

  location /downlods/ {
    rewrite ^downloads/(.*)$ /downloads/$1 break;
    return 403;
  }

  location ~* ^.+\.(jpg|jpeg|gif|png|css|js)$ {
    expires 30d;
  }

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

  location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www/myweb$fastcgi_script_name;
    include fastcgi_params;
  }

}
F170776438808428f83106c4bae71dcd?d=identicon&s=25 Xavier Grangier (Guest)
on 2009-02-28 13:45
(Received via mailing list)
Hello

> - unknown directive "if($request_method"

you need a space between if and (
eg:

if ($request_method ...

xav
5640e332954fc0006aea97a155ce0afd?d=identicon&s=25 Igor Sysoev (Guest)
on 2009-02-28 14:05
(Received via mailing list)
On Sat, Feb 28, 2009 at 04:21:56AM -0800, Ivan L. wrote:

> - unknown directive "if(-d"
>
> When I remove if(-d) statement, the error I receive;
>
> - unknown directive "if(!-e)"

As it was already said, you need a space between "if" and "(":

     if (

However, your configuraiton is very strange.

>
>       break;
>   }
Why do you use "^/search$" ?

>   location /about {
>     if($request_method !~ GET) {
>       return 405;
>     }
>     if(!-e $request_filename) {
>       expires max;
>       rewrite ^/about$ /index.php?where=about break;
>     }
>   }

What do you expect here ?

>   location /contact {
>     if($request_method !~ ^(GET|POST)$) {
>       return 405;
>     }
>     if(!-e $request_filename) {
>       expires max;
>       rewrite ^/contact$ /index.php?where=contact break;
>     }
>   }

What do you expect here ?

>   location /downlods/ {
>     rewrite ^downloads/(.*)$ /downloads/$1 break;
>     return 403;
>   }

What do you expect here ?
E185808d768ce67b53f6e5eed1c74714?d=identicon&s=25 jeffs (Guest)
on 2009-02-28 15:46
(Received via mailing list)
In apache there is a mod_log_mysql, which will log apache log files to
MySQL?

Is there a method to do this with nginx?

If not, before I install, how can an administrator log the log files in
nginx to a database?

thank you.
Ed6583386f2b2d56e1b5eb2ee236da39?d=identicon&s=25 Nick Pearson (Guest)
on 2009-02-28 16:03
(Received via mailing list)
You may find some help in this thread from about three weeks back:
http://thread.gmane.org/gmane.comp.web.nginx.english/9602
F5a6ed477b109fe6acc11a5a8f87e7e8?d=identicon&s=25 mike (Guest)
on 2009-02-28 23:13
(Received via mailing list)
Hmm
http://snippets.aktagon.com/snippets/247-Logging-n...

Is a neat idea, but I would think with hundreds of requests per second
coming from what could be multiple servers that the syslog daemon
would eventually become so busy that you'd miss legitimate messages
too.

I wonder if you could setup two syslog instances?

Right now I do this:

log_format traffic
'$http_host|$bytes_sent|$time_local|$remote_addr|$request_uri';
access_log /var/log/nginx/traffic traffic;

Then I have a nightly cronjob which goes to each server, scps the file
locally, parses them and essentially combines them in the process
(since I have multiple webservers) and then can do statistics on them.
I actually don't load the entire thing into SQL either (I used to)

This is the script I run once a night at like 12:30am, it seems to
give me good enough metrics:
http://mikehost.com/~mike/tmp/runstats.php.txt

Ideally something like a syslog to catch all the servers might be even
better but I'm worried the load on a syslog daemon...
On Sat, Feb 28, 2009 at 6:53 AM, Nick Pearson <nick.pearson@gmail.com>
wrote:
>> If not, before I install, how can an administrator log the log files in
>> nginx to a database?
>>
>> thank you.
>>
>
>

mm
7ff276b9a72c9fc0492dec907e272a31?d=identicon&s=25 Ivan L. (Guest)
on 2009-03-01 06:13
(Received via mailing list)
Thanks.

I know it's strange, I've found most of them on the internet, and on the
English wiki documentation, and mix them together. After my first
problem was solved, that lead me to another problems. Perhaps that's
because of config file is confusing.

>> if(!-e $request_filename) {
>>   rewrite ^/search$ /index.php?where=search last;
>>   rewrite ^/posts/([0-9])$ /index.php?posts=$1 last;
>>
>> break;
>> }

> Why do you use "^/search$" ?

I use this for the queries like "search?q=search_string"

>>  location /about {
>>    if($request_method !~ GET) {
>>      return 405;
>>    }
>>    if(!-e $request_filename) {
>>      expires max;
>>      rewrite ^/about$ /index.php?where=about break;
>>    }
>>  }

>What do you expect here ?

I wanted only GET|HEAD availability on some pages, and only POST on some
pages.

>>  location /contact {
>>    if($request_method !~ ^(GET|POST)$) {
>>      return 405;
>>    }
>>    if(!-e $request_filename) {
>>      expires max;
>>      rewrite ^/contact$ /index.php?where=contact break;
>>    }
>>  }

> What do you expect here ?

That suppose to be a page that only accepts POST.

>  location /downlods/ {
>>    rewrite ^downloads/(.*)$ /downloads/$1 break;
>>    return 403;
>>  }

> What do you expect here ?

That's downloads folder, I wanted to disable directory listing.


I wanted only GET|HEAD availability on some pages, and only POST on some
pages. But my config file didn't work so I changed it as follows, but
the problem is on some pages(about, and contact pages) I see the php
source code(browser downloads the php source code), and contact_ajax
page returns 405 even if the method is POST.



server {
  listen 80;
  client_max_body_size 50M;
  client_body_buffer_size 128k;
  charset utf-8;

  root /var/www/myweb;
  index index.php index.html index.htm;

  access_log /var/log/nginx/access.log;

  location / {
    if ($request_method !~ ^(GET|HEAD)$ ) {
      return 405;
    }
    if (-f $request_filename) {
      break;
    }
    if (-d $request_filename) {
      break;
    }
    if (!-e $request_filename) {
      rewrite ^/search$ /index.php?where=search last;
      rewrite ^/about$ /index.php?where=about last;
      rewrite ^/contact$  /index.php?where=contact last;

      rewrite ^/posts/([0-9])$ /index.php?posts=$1 last;

      break;
    }
  }

  location /contact_ajax {
    if ($request_method !~ ^(POST)$ ) {
      return 405;
    }
    if (!-e $request_filename) {
      rewrite ^/contact_ajax$ /index.php?where=contact_js break;
    }
  }

  location /downloads/ {
    rewrite ^downloads/(.*)$ /downloads/$1 break;
    return 403;
  }

  location ~* ^.+\.(jpg|jpeg|gif|png|css|js)$ {
    expires 30d;
  }

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

  location ~ \.php$ {
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www/myweb$fastcgi_script_name;
    include fastcgi_params;
  }

}






----- Original Message ----
From: Igor Sysoev <is@rambler-co.ru>
To: nginx@sysoev.ru
Sent: Saturday, February 28, 2009 11:52:53 PM
Subject: Re: New to nginx

On Sat, Feb 28, 2009 at 04:21:56AM -0800, Ivan L. wrote:

> - unknown directive "if(-d"
>
> When I remove if(-d) statement, the error I receive;
>
> - unknown directive "if(!-e)"

As it was already said, you need a space between "if" and "(":

     if (

However, your configuraiton is very strange.

>
>       break;
>   }
Why do you use "^/search$" ?

>   location /about {
>     if($request_method !~ GET) {
>       return 405;
>     }
>     if(!-e $request_filename) {
>       expires max;
>       rewrite ^/about$ /index.php?where=about break;
>     }
>   }

What do you expect here ?

>   location /contact {
>     if($request_method !~ ^(GET|POST)$) {
>       return 405;
>     }
>     if(!-e $request_filename) {
>       expires max;
>       rewrite ^/contact$ /index.php?where=contact break;
>     }
>   }

What do you expect here ?

>   location /downlods/ {
>     rewrite ^downloads/(.*)$ /downloads/$1 break;
>     return 403;
>   }

What do you expect here ?
7ff276b9a72c9fc0492dec907e272a31?d=identicon&s=25 Ivan L. (Guest)
on 2009-03-01 06:13
(Received via mailing list)
Thanks.




----- Original Message ----
From: Xavier Grangier <grangier@gmail.com>
To: nginx@sysoev.ru
Sent: Saturday, February 28, 2009 11:36:36 PM
Subject: Re: New to nginx


Hello

> - unknown directive "if($request_method"

you need a space between if and (
eg:

if ($request_method ...

xav
5640e332954fc0006aea97a155ce0afd?d=identicon&s=25 Igor Sysoev (Guest)
on 2009-03-01 09:43
(Received via mailing list)
On Sat, Feb 28, 2009 at 09:03:14PM -0800, Ivan L. wrote:

>
> > Why do you use "^/search$" ?
>
> I use this for the queries like "search?q=search_string"

Then you need just:

    location = /search {
        rewrite ^  /index.php?where=search last;
    }

    location /posts {
        rewrite ^/posts/(\d)$ /index.php?posts=$1 last;
    }

> >What do you expect here ?
>
> I wanted only GET|HEAD availability on some pages, and only POST on some pages.

    location = /about {
        if ($request_method != GET) {
            return 405;
        }
        rewrite ^  /index.php?where=about last;
    }

or

    location = /about {
        limit_except GET {
            deny all;
        }
        rewrite ^  /index.php?where=about last;
    }

> > What do you expect here ?
>
> That suppose to be a page that only accepts POST.

  location = /contact {
      if($request_method != POST) {
         return 405;
      }

      rewrite ^  /index.php?where=contact last;
  }

> >  location /downlods/ {
> >>    rewrite ^downloads/(.*)$ /downloads/$1 break;
> >>    return 403;
> >>  }
>
> > What do you expect here ?
>
> That's downloads folder, I wanted to disable directory listing.

Just:

  location /downlods/ {
  }
This topic is locked and can not be replied to.