Cannot connect WP Discourse to Discourse forum

(Christian) #1

Hello. I need some help, I’m working on this from yesterday but I can’t figure out what is the problem.

In brief, I had a Blog built with Wordpress connected to Discourse, so every post published in Wordpress would create a new discussion in the forum. The SSO wasn’t enabled.

Then, one week ago, I enabled the HTTPS to the Discourse forum. It works very well and no problem for the certificate, but WordPress now doesn’t want to connect.

The URL is right. I tried with http, https and without “http”, but nothing.
The API key is right, I tried re-generating it. Nothing.
The username is system, which is the default and it is present in my Discourse installation.

Tried about everything. But it doesn’t want to connect. It shows:
You are not connected to a Discourse forum. Please check your settings for 'Discourse URL', 'API Key', and 'Publishing username' Also, make sure that your Discourse forum is online.

Any ideas on this?

(Simon Cossar) #2

Is your blog also using https?

(Christian) #3

Yes, it is.

Both are using https, same certificate (wildcard subdomain) but different servers. Blog in a server of my country and Discourse in Digital Ocean.

(Simon Cossar) #4

Yes, I’m seeing the same problem with https setup through Let’s Encrypt on both my WordPress site and my forum. The error I’m getting on the WordPress site is ‘Too many redirects’.

(Christian) #5

I have a Comodo Wildcard Subdomain certificate but I don’t get any “too many redirects” errors. Maybe you misconfiguerd the installation of https in WordPress?

I don’t get any errors, just that is about that my discourse forum doesn’t exist…

(Simon Cossar) #6

I am getting a ‘too many redirects’ error as the result of the function that makes the API call to Discourse.

(Christian) #7

But are you using the SSO feature? Because I don’t have this error…

I don’t know how to fix this.

(Stephen) #8

Do you have ssh access at both ends?

If so can each box see the other via their HTTPS addresses?

curl -Is | head -1

Do you get the HTTP/1.1 200 OK response?

(Simon Cossar) #9

Yes, when the initial request is made from the server hosting the WordPress site with curl, it returns 200. The plugin uses wp_remote_get to make the request. When the request is made with wp_remote_get it is returning the 'too many redirects' error. This is with the server hosting the WordPress site running Ubuntu 16.04.1.

(Stephen) #10

Ok, so you can cURL from both hosts to one another?

(Simon Cossar) #11

I think that the problem has to do with the server configuration for the host of your WordPress site. You could probably confirm that by installing the query monitor plugin on your site. After doing that, go to the Discourse options page and refresh it. You will probably see something like this:

Click on the orange box and then look at the results under ‘HTTP Requests’. The result should look something like this (I’ve deleted my api-key from the image):

This is the result that I have been getting using the DigitalOcean 1 click install of WordPress with their letsencrypt setup for ssl.

I’d really like to figure out how to configure it so that so that the DigitalOcean 1 click WordPress install will work with https with the wp-discourse plugin. I’m not sure what the problem is.

For now, for setting up a WordPress site on DigitalOcean with https enabled, follow this guide (with the letsencrypt option.) How To Install WordPress with LAMP on Ubuntu 16.04 | DigitalOcean. This is working with both WordPress and Discourse setup on DigitalOcean with letsencrypt.

I’ll look at this some more later to try to find what the problem is that is causing the redirects. Maybe you could contact the host of your WordPress site and see if they have any ideas.

(Jeff Atwood) #12

If it is requesting http:// isn’t the problem that it should be requesting https:// instead? Where is it redirecting to, exactly?

(Simon Cossar) #13

It seems to be redirecting to itself (the request was made from this page):
[Thu Oct 06 20:20:16.877097 2016] [:error] [pid 1658] [client] Too many redirects, referer:

There are a few references on the web to redirect loops with ssl enabled in the WordPress admin, but nothing that I can follow that explains what’s going on. I now have a configuration that works and one that doesn’t, so it should be easier to see what’s happening.

(Simon Cossar) #14

From what I can figure out (this took a while), this is what’s causing the error:

It’s redirecting to Discourse. The plugin function that tests the connection to Discourse makes a request to GET /users/system.json when the settings page is saved. TheWordPress function wp_remote_get is used to make the request. It is set to follow a maximum of 5 redirections (that’s the default.) When HTTPS is enabled on the Discourse site, each time the function is run, it makes 5 requests to Discourse. Each request returns a 301 and a 549 status code. After five requests, the wp_remote_get function returns the ‘too many redirects’ error to WordPress. I don’t know why Discourse is returning the 549 status code.

Increasing the value for the allowable number of redirects just causes more requests to be made, but eventually results in the same error.

When HTTPS is not enabled on Discourse, everything runs fine.

The solution that working for me on the DigitalOcean setup is to install the php-curl extension and restart the server. Doing this makes the DigitalOcean 1 click WordPress install work with HTTPS enabled on both WordPress and Discourse. After enabling php-curl the request to Discourse is returning 200 and 2045.

It looks like what is going on is that the WordPress HTTP API functions (they are used throughout the plugin for making API calls to Discourse) have a function called _get_first_available_transport that first tries to use curl but falls back to streams if curl isn’t available.

(Simon Cossar) #15

Can you check to see if php-curl is installed on your server?

(Jeff Atwood) #16

Wow great detective work Simon! :mag:

Do we know why it’s returning 301 and 549? 301 is “moved permanently”, but I can’t find 549 anywhere as a valid HTTP status code, or in our codebase.

(Simon Cossar) #17

Yeah, I was reading the nginx access log incorrectly. 301 is the status code, the number after that (549) is the number of bytes sent.

(blaumeer) #18

Wordpress and Discourse on https. Installing php5-curl resolved the issue for me, thank you!


CURL is already installed on my server. But i’ve same problem after using https on my blog and forum. Different SSL certificate is problem for WP Discourse.

wp-json/wp-discourse/v1/update-topic-content works fine.

/wp-json/wp-discourse/v1/update-user 404 error.

(Simon Cossar) #20

The update-user route is only initialized on WordPress if you have enabled the SSO Provider option. If you haven’t enable the SSO Provider option, then returning a 404 error is the correct response. From the option’s description:

This setting will only be activated if your site is functioning as the SSO provider for Discourse (this can be overridden by hooking into the ‘wpdc_use_discourse_user_webhook’ filter.)

The update-user webhook was added to the plugin before the Create or Sync Discourse Users on Login option was added (that option is found on the SSO Provider options tab.) The Create or Sync Discourse Users on Login option is probably more useful for most cases.

If you have enabled the SSO Provider option and the Update Userdata webhook and you are still getting a 404 error, please let me know and I’ll look into it.