Nginx – how to fix “Invalid command ‘set_real_ip_from'” error

cloudflareipnginx

I'm using centos 6 , nginx as reverse proxy,directadmin and cloudflare. I followed the instructions to get real visitors IP as below:

 $ nano /etc/nginx/nginx.conf

added these to http{

set_real_ip_from  192.168.1.0/24;
set_real_ip_from  192.168.2.1;
set_real_ip_from  2001:0db8::/32;
real_ip_header    X-Forwarded-For;
real_ip_recursive on;

restarting nginx is OK but when I restart httpd it gives this error:

   Invalid command 'set_real_ip_from', perhaps misspelled or defined 
by a module not included in the server configuration

then I tried to enable ngx_http_realip_module .
I couldn't do anything but I think it was enabled by default..

$ nginx -V

resualt:

nginx version: nginx/1.8.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr
 --sbin-path=/usr/s    bin --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid
 --http-log-p    ath=/var/log/nginx/access_log --error-log-path=/var/log/nginx/error_log --with-i   pv6 --without-mail_imap_module 
 --without-mail_smtp_module --with-http_ssl_module   --with-http_realip_module --with-http_stub_status_module 
 --with-http_gzip_stati   c_module --with-http_dav_module --with-cc-opt=''-D FD_SETSIZE=32768''

can anyone help me with this?

Best Answer

Looks like this module is enabled (--with-http_realip_module), but you just copied the example configuration from the module page.

The set_real_ip directive should be set in the backend server, not in the proxy one. Then you only need to use one line, what should be:

set_real_ip_from  192.168.2.1;

but replace 192.168.2.1 by the local address your backend server is listening to.

EDIT: so, to answer to some more information you've added in the comments so far, httpd.conf is a configuration file for apache (httpd) and nginx directives won't work in them. You should read apache documentation in order to configure it the way you need. (The rpaf module seems to be the one you're looking for. This may be useful for you)