Passing Real IP in WordPress behind Proxy or in Docker

If you have followed the tutorial on How to run WordPress Blog behind Nginx Secure (https) Proxy, you might be under a situation that WordPress is showing all ips as proxy ips. In case of Docker it must be like 172.X.X.X otherwise, it is the ip of your server. 

If this is a problem?

You might be wondering if this is worth solving? Well Yes!, Most of the real comments were categorized as spam. 

Adding Real-IP to WordPress

Step 1 – Editing WordPress config

In wp-config.php file add following lines just above /* That’s all, stop editing! Happy blogging. */

// Use X-Forwarded-For HTTP Header to Get Visitor's Real IP Address
if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
  $http_x_headers = explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] );
  $_SERVER['REMOTE_ADDR'] = $http_x_headers[0];
}
/* That's all, stop editing! Happy blogging. */

Step 2 – Editing Nginx

Inside your proxy settings in nginx, simply add this:

proxy_set_header        X-Real-IP       $remote_addr;

In case of WordPress Behind Docker

In case if you are using Docker, you will need to copy wp-config.php from container and later copy to container. This can be done as following.

#Copy from docker container
docker cp project_wordpress_1:/var/www/html/wp-config.php .

#Copy to docker container
docker cp wp-config.php project_wordpress_1:/var/www/html/wp-config.php

Easy-peasy right?

Why struct isn’t getting any data on unmarshal in Go?

Among first few problems, a go lang programmer will probably be struggling with a blank struct. Structs have blank values when one of the following things are wrong:

1. Invalid Naming Conventions:

First letter of fields in struct should be capital. For example


//Invalid name 	
type Person struct {
    name string
    age  int
}

//Valid Name
type Person struct {
    Name string
    Age  int
}

2. Invalid Matches:

Consider this is the json which we can’t change.


{
 FirstName: "John Doe",
 Age: "30",
}

 


//Results in blank data 	
type Person struct {
    FirstName string //This will match first_name but we have FirstName
    Age  int //This will match age but we have Age
}

//The fix
type Person struct {
    FirstName string `json:"FirstName"`
    Age  int `json:"Age"`
}

First three utility packages for a Go Lang developer

With awesome Go, we have some usual problems which are solved by some awesome packages. Some common problems which every programming language has are – versioning, dependency and debugging.

1. GVM

GVM – Go Version Manager It helps in switching between different go versions. Depending on code base, you can easily switch between different go version.

2. DEP

Dep is dependency manager for go lang. It is production ready. All your codes dependency is maintain in vendor folder. You start with dep init command and it creates a configuration for you.

3. PRETTY

Pretty package helps you debugging variables by printing them in a pretty way.

So before your project goes out of control, it is better to learn and use these three packages right from the start.