1

I have just installed wordpress in a subdirectory of my domain: exaple.com/wordpress/

When I enter the url:

 http://exaple.com/wordpress/

I am redirected to :

  https://localhost/wordpress/

Apache2 is listening on port 8080 NGINX is listening on port 80, 443

I use NGINX as a reverse proxy, the configuration is the following:

    server {
        server_name example.com;
        location / {
                proxy_pass       http://localhost:8080;
        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
 }
 server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        server_name example.com;
    listen 80;
    return 404; # managed by Certbot
 }

Apache configuration is:

<VirtualHost *:8080>

    DocumentRoot "/var/www/example.com"
    ServerName example.com

    <Directory "/var/www/example.com">
        Options MultiViews FollowSymlinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

    TransferLog /var/log/apache2/nextcloud_access.log
    ErrorLog /var/log/apache2/nextcloud_error.log


</VirtualHost>

In the WordPress database, table wp_options, I have already changed the two rows "siteurl" and "home" from:

http://localhost/wordpress/

to:

http://example.com/wordpress/

It is still not working.

1
  • The important thing here is: proxy_set_header Host $http_host; just below the proxy_pass, otherwise, "upstream" will get the host name of the given URL (localhost...) Commented Nov 15, 2023 at 12:05

1 Answer 1

0

The answer was on this post:

Problems with Nginx Reverse SSL Proxy for Apache for WordPress

To make that work I changed Nginx configuration like this:

    server {
        server_name example.com;
        location / {
                proxy_pass       http://localhost:8080;

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Server $host;


        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
 }
 server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        server_name example.com;
    listen 80;
    return 404; # managed by Certbot
 }

Within the WordPress installation I changed the configuration while wp-config.php adding the following code immediately after the first line "<?PHP" (at the end of the file it does not work):

/**If we got HTTPS from Nginx, we must reply the same way */
if ( $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' )
{
        $_SERVER['HTTPS']       = 'on';
    $_SERVER['SERVER_PORT'] = '443';
        define('FORCE_SSL_ADMIN', true);
}

/**When replying make sure the reply HOST is set to Nginx Rerverse Proxy address, not us */
if ( isset($_SERVER['HTTP_X_FORWARDED_HOST']) )
{
        $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.