FeaturesWP CloudPricingEE CLIContactMy Account
Features
Team Access & User Permissions
Install
Resources
Community
CLEAR
Tutorials
Nginx
Forwarding Visitor’s Real-IP + Nginx Proxy/Fastcgi backend correctly
Topics
On this page
-
- Getting Started
- Git and File-System Layout (non-default setup)
- Version Constraints
- Managing Themes and Plugins (for entire WordPress site)
- Updating Themes and Plugins (for entire WordPress site)
- Adding Composer Support to Your Own Themes and Plugins (for developers)
- Using Composer to Manage Own Theme/Plugin Dependencies (for developers)
-
- Assigning Multiple IP Addresses to Single LAN Card
- Disable IPv6 on Ubuntu 12.04
- dsh – distributed shell
- fdupes – find & replace duplicate files with hardlinks
- GPG Keys Cheatsheet
- Increase "Open Files Limit"
- kill all php, nginx, mysql or any kind of processes
- Passwordless Authentication for SSH
- Screen
- search replace in multiple files useing grep xargs sed
- Setup sftp
- sysctl.conf
- Timezone Sync
- Using Google Public DNS on Ubuntu Server
- Using openssl to match private key, cerificate and CSR
- Configure Postfix to Use Gmail SMTP on Ubuntu
- Logrotate Example for Custom Logs
- PHP 5.5, MySQL, Postfix, Nginx and WordPress on Ubuntu
- Serverdensity
- Upgrade Ubuntu 10.04 to 12.04
- Using find and sed to replace strings in multiple files
- Webmin
-
- .my.cnf – mysql user & password
- Analyse slow-query-log using Anemometer
- Improving MySQL Query Cache
- MySQL Query Profiling
- Reset MySQL root password
- tuning-primer.sh – an alternative for mysqltuner
- Using MariaDB 10
- Install/Upgrade to MySQL 5.6 on Ubuntu 12.04 LTS
- Change WordPress Domain Name
- Character Sets and Collations
- Enable innodb_file_per_table
- Enable Remote Access (Grant)
- Installing Percona Toolkit
- Using tmpfs for temporary folder creation
- YARPP and InnoDB
- Convert MyISAM to InnoDB
- Using tmpfs for mysqldump & mysqlimport
- Convert from INNODB to MYISAM
- Analyse slow-query-log using mysqldumpslow & pt-query-digest
- mysqlcheck with cron to optimize automatically
- Using MySQLTuner to Optimize MySQL configuration
-
-
- Directly connect to PHP-FPM
- Generating core-dump for php5-fpm
- GeoIP
- Having php5 and php7 on same system.
- Install Xdebug and configure it with webgrind
- Installation of PHP Code Review Tools
- memcache
- PHP's Zend Opcache Config & Web Viewer
- Setting Sqlite support with PHP
- Using HHVM with PHP-FPM Fallback
- Using Redis for PHP Sessions on Ubuntu Server
- Installing PhpMyAdmin Quickly
- Moving PHP's session storage to tmpfs
- Install Xdebug and configure it with Netbeans
- Nginx – Enable PHP-FPM Status Page
- PHP-FPM: Socket vs TCP/IP and sysctl tweaking
- APC Cache Optimization & Monitoring Using Web Interface
- Checking if PHP/WordPress can send mails
- Debugging PHP Scripts Using slow_log and more
- Increase file upload size limit in PHP-Nginx
- Increase PHP script execution time with Nginx
-
- Performance Optimization
- Remove Query string of css/js files
- Setup ElasticSearch with ElasticPress
- Using Redis for WordPress Object-Cache
- WooCommerce Window Shopping Caching Technique
- Moving WordPress To New Server (Faster)
- Better wp-cron using linux's crontab
- 404 error- Page not Found!!!
- Debugging WordPress with Nginx
On Front-End Nginx
Nginx's HTTP Real IP Module
Forwarding Visitor’s Real-IP + Nginx Proxy/Fastcgi backend correctly
Here, we are dealing with 2 nginx servers. A front-end nginx, proxying request to another nginx-server running behind firewall. This backend-nginx is a WordPress setup, using PHP-FPM (fastcgi) on our case. Article is valid for any code/application running behind fastcgi upstream.
To manage security properly, we need to pass-on visitors real-IP to backend-nginx. In backend-nginx, WordPress do some stuff based on user-ip.
On Front-End Nginx
First, a portion of Nginx config from front-end proxy
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
With this config, backend-nginx receives extra HTTP Headers which are passed to fastcgi upstream (PHP/WordPress in our case).
If you see output of phpinfo() you will see extra headers as highlighted below:

The problem with above setup is that fastcgi app running behind backend-nginx, uses REMOTE_ADDR value whenever it needs to deal with visitor IP address.
A solution would be check existence ofHTTP_X_REAL_IP and use that value instead. I think WordPress does this for itself but one of our custom plugin was not already doing that. We could either fix it in PHP code, but rather than modifying every piece of PHP code which rely on REMOTE_ADDR, we added following lines inside backend-nginx conf:
location ~ \.php$ {
fastcgi_param REMOTE_ADDR $http_x_real_ip;
#...other rules
}
With this change phpinfo() shows correct user IP forREMOTE_ADDR value.

Now any piece of code, which depend onREMOTE_ADDR value will work as it is!
Nginx’s HTTP Real IP Module
If you want to do some real-user IP-based stuff in backend, it will NOT work.
Nginx itself will not see real-user IP. Only application/codes behind fastcgi will benefit from above change.
For Nginx, you can simply use variable $http_x_real_ip instead of IP-address.
Below is an example of $http_x_real_ip usage to emulate allow/deny functionality:
if ($http_x_real_ip != 115.115.82.210) {
return 403;
}
There are cases when a workaround using $http_x_real_ip may not be possible. In those caes, we can use Nginx’s Http Real IP Module.
You can fix real-ip andREMOTE_ADDR by adding a line like below to your backend nginx-config:
set_real_ip_from 192.168.122.1;
Make sure you replace 192.168.122.1 with REMOTE_ADDR value that was being received originally. It is IP of proxy-nginx as seen by backend-nginx.
Advantage of Http Real IP Module is that it sets correct IP for nginx config as well as fastcgi backend app in one go. Apart form this, it is secure also.
You can use earlier way if your Nginx is not built with --with-http_realip_module option.
If you have any questions, feel free to check our Nginx support forum.
Link: WordPress-Nginx Tutorials Series
Enable Nginx Status Page
PREVIOUS
Parsing access.log and error.logs using linux commands
NEXT
Comments
-
Do you know if there is a way to have NGINX report the FQDN of the visitor’s address? I’ve tried a number of configurations but nothing seems to work. I can only get user’s IP to output to the logs..
-
Try checking list of 3rd party modules – http://wiki.nginx.org/3rdPartyModules
Might be possible using a 3rd party module.
-
Features
- Server Setup & Management
- Site Creation & Management
- Create WP Cloud Sites
- Speed & Caching
- Firewall Protection
- Backups & Recovery
- Logs & Monitoring
- Deployments & Developer Tools
- Team Access & User Permissions
Useful Links
Product
Other Products
Subscribe for curated product updates, tips, and announcements.
"*" indicates required fields
This field is for validation purposes and should be left unchanged.
Email*
SIGN ME UP
Δ
© rtCamp, since 2009. All rights reserved.