Home > Mobile >  configure nginx with wordpress
configure nginx with wordpress

Time:06-23

I am trying to configure nginx with wordpress. the Apache server is running on port 8083. the wordpress url is https://dhahbya.com

my nginx config file is :

server {
        listen *:443 ssl;

        server_name www.dhahbya.com dhahbya.com;

        ssl_certificate ****************;
        ssl_certificate_key **************;

        location / {
                proxy_pass http://127.0.0.1:8083;
        }
}
server {
    listen      80;
    server_name *.dhahbya.com;

     return 301 https://$host$request_uri;
}

the issue is that when I try to navigate in the website it redirects me to 127.0.0.1:8083.

CodePudding user response:

the work around was to remove the apache server and configure nginx to run wordpress.

CodePudding user response:

According to redirect responce you showed in comments (please, use markdown formatting in the future to post such a things):

HTTP/1.1 301 Moved Permanently
Server: nginx/1.18.0 (Ubuntu)
Date: Wed, 22 Jun 2022 17:27:56 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: keep-alive
X-Redirect-By: WordPress
Location: https://www.dhahbya.com/

This redirect is coming from the WordPress core, and the most probable reason is that WordPress can't detect the original request was made using the HTTPS protocol. Usually such problems are solved using the X-Forwarded-... HTTP headers:

location / {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://127.0.0.1:8083;
}

However for some unknown reasons WordPress didn't try to detect original request scheme using the non-standard yet widely used X-Forwarded-Proto HTTP header, the is_ssl() WordPress function looks like the following (at the time this being written):

function is_ssl() {
    if ( isset( $_SERVER['HTTPS'] ) ) {
        if ( 'on' === strtolower( $_SERVER['HTTPS'] ) ) {
            return true;
        }

        if ( '1' == $_SERVER['HTTPS'] ) {
            return true;
        }
    } elseif ( isset( $_SERVER['SERVER_PORT'] ) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
        return true;
    }
    return false;
}

The very common solution is to add this functionality to the wp-config.php file by yourself:

if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
    $_SERVER['HTTPS'] = 'on';
}

However I didn't like it since the same can be achieved without making changes to the WordPress files. If your upstream is an apache server using mod_php module, you can set that HTTPS variable from the apache configuration the following way:

<IfModule setenvif_module>
    SetEnvIf X-Forwarded-Proto "^https$" HTTPS=on
</IfModule>

And if you serve WordPress by your upstream with another nginx instance via PHP-FPM, you can make that instance setting the HTTPS FastCGI variable with the fastcgi_param directive according to the X-Forwarded-Proto HTTP header value:

map $http_x_forwarded_proto $forwarded_https {
    https  on;
}

server {
    ...
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param HTTPS $forwarded_https if_not_empty;
        ...
    }
}
  • Related