Force HTTPS Redirection With NGINX

Keywords: WordPress + NGINX + SSL - AWS - How to - Secure Connections (SSL/HTTPS)

Description:
Greetings everyone.

After successfully completing the Guide on how to Generate And Install A Let’s Encrypt SSL Certificate For A Bitnami Application I proceeded to Force HTTPS Redirection with NGINX using the following:

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

I edited my nginx.conf file located at: /home/bitnami/wordpressspro/nginx/conf/ saved it and restarted using ctlscript.sh

My issue is that I receive the following error:

nginx: [emerg] “server” directive is not allowed here in /home/bitnami/wordpresspro/nginx/conf/nginx.conf:71
/home/bitnami/wordpresspro/nginx/scripts/ctl.sh: 77: [: Illegal number:
/home/bitnami/wordpresspro/nginx/scripts/ctl.sh : Nginx could not be started

I found this similar situation: Redirect http to https on nginx

However, the solution here references the usage of Server Blocks for handling the https redirect.

In my case, Server Blocks are not contained within the Bitnami Wordpresspro Package that I installed, I have searched for them.

Hello @avanzate1,

You were checking the right guide but it seems there is a typo in the Approach B instructions. Could you please try following these steps instead:

  1. Modify the /opt/bitnami/nginx/conf/bitnami/bitnami.conf file so that it looks like this:
server {
      listen 80;
      server_name localhost;
      return 301 https://$host$request_uri;
      include "/opt/bitnami/nginx/conf/bitnami/bitnami-apps-prefix.conf";
  }
  1. Restart NGINX to apply the changes.

Regards,
Francisco de Paz

Thank you @fdepaz

I tried the code as per instructed. Here is how it was saved:

Screenshot (635)

Most of the code was already filled in. However after restarting services I came across this error.

$ sudo /home/bitnami/wordpresspro/ctlscript.sh start
/home/bitnami/wordpresspro/mysql/scripts/ctl.sh : mysql  (pid 32383) already running
/home/bitnami/wordpresspro/php/scripts/ctl.sh : php-fpm (pid 32445) already running
nginx: [emerg] "server" directive is not allowed here in 
/home/bitnami/wordpresspro/nginx/conf/nginx.conf:71
/home/bitnami/wordpresspro/nginx/scripts/ctl.sh: 77: [: Illegal number: 
/home/bitnami/wordpresspro/nginx/scripts/ctl.sh : Nginx could not be started
$

I´m sure we´ll resolve this. Thank you very much for your support. I appreciate it.

Cordially,

Marco Fernando

Hello @avanzate1

Could you please share with us a new bnsupportID? It would be of great help to review the updated configuration in case there is something we are missing.

Regards,
Francisco de Paz

Sure thing. 86745f00-026e-9d19-2d1b-f862c0765294
thanks

Hello @avanzate1,

I checked nginx/conf/bitnami/bitnami.conf and it was missing the return directive:

    # HTTP server

    server {
        listen       80;
        server_name  localhost;

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

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

Could you double-check the file in case your redirection change wasn’t saved?

Regards,
Francisco de Paz

Yeah. Sorry. I actually have 3 Instances stuck at this stage. The instance I shared the Diagnostic Code had the code reverted. Please try this one: 809d90b9-4049-5b74-77c2-e0ea2489da4a

I just reviewed and it contained the saved return directive. I get the same error on this instance.

Hello @avanzate1,

The error is telling you that the server directive is not allowed in /opt/bitnami/nginx/conf/nginx.conf. Remove the server block added there and leave the one at nginx/conf/bitnami/bitnami.conf as we discussed.

Regards,
Francisco de Paz

1 Like

Oh gee, what a dork. Thank you so much for catching that. I appreciate it. Case closed.

Hello @avanzate1,

No problem, I’m glad this got solved! I will be closing the thread as it is now marked as solved.

Regards,
Francisco de Paz