Search code examples
wordpressnginxbitnami

nginx redirect returning 301 code instead final page


I've created an EC2 instance with Bitnami Wordpress and then I've cloned the instance into a new one.

After that I've modified in both instances this NGINX script to redirect HTTP => HTTPS requests.

# HTTP server

server {
    listen       80;
    server_name  localhost;

    #include "/opt/bitnami/nginx/conf/bitnami/phpfastcgi.conf";

    if ($http_x_forwarded_proto = 'http'){
            return 301 https://$host$request_uri;
    }

    include "/opt/bitnami/nginx/conf/bitnami/bitnami-apps-prefix.conf";
}

# HTTPS server

server {
   listen       443 ssl;
   server_name  localhost;

   ssl_certificate      server.crt;
   ssl_certificate_key  server.key;

   ssl_session_cache    shared:SSL:1m;
   ssl_session_timeout  5m;

   ssl_ciphers  HIGH:!aNULL:!MD5;
   ssl_prefer_server_ciphers  on;

   #include "/opt/bitnami/nginx/conf/bitnami/phpfastcgi.conf";

   include "/opt/bitnami/nginx/conf/bitnami/bitnami-apps-prefix.conf";
}


include "/opt/bitnami/nginx/conf/bitnami/bitnami-apps-vhosts.conf";

Logging via SSH and running curl -i localhost, one of the instances works good and return final page source code.

The other instance is returning this:

bitnami@ip-xxx-xx-xx-xxx:~$ curl -i localhost
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Mon, 04 Feb 2019 16:05:51 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/7.1.22
Location: https://localhost/
X-Frame-Options: SAMEORIGIN

Considering it is a cloned instance, what kind of problem can be happening here?


Solution

  • Bitnami Engineer here:

    The X-Forwarded-Proto (XFP) header is a standard header for identifying the protocol (HTTP or HTTPS) that a client used to connect to your proxy or load balancer.

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto

    In this case, as you are connecting directly to the NGINX server, you don't need to use that "if" block, you can simply redirect your request to HTTPS

          return 301 https://$host$request_uri;
    

    You can find more information about the https redirection here:

    https://docs.bitnami.com/general/apps/wordpress-pro/administration/force-https-nginx/