Dev Environment: Missing port leads to broken images

AI-generated summary

The discussion revolves around a development environment issue where images, such as logos, fail to load due to missing port numbers in URLs. kleinfreund reports that Discourse.base_url is set to http://localhost without the default port 3000, causing broken image links. gerhard explains that the port setting is hidden and can be set via the Rails console with SiteSetting.port = 3000, which resolves the issue. kleinfreund questions why this isn’t the default, noting it’s counterintuitive. pfaffman suggests the issue is minor, as logos are not typically a concern in development. merefield offers an alternative by modifying sitesettings.yml to expose and set the port. reswild describes a similar problem where :3000 is incorrectly appended to URLs, even when using a proxy, and finds that setting SiteSetting.port = 443 and enabling HTTPS resolves the issue, suggesting the default behavior may be flawed.

I configured a logo in the site settings, but the logo’s URL is missing the default port, i.e. the logo URL looks like this http://localhost/uploads/… rather than http://localhost:3000/uploads/….

I’m starting the development server like this:

RAILS_ENV=development bundle exec rails server

I found that Discourse.base_url is set to "http://localhost" and in config/database.yml, I find this note for the development.hostnames key:

### Don't include the port number here.
Change the "port" site setting instead, at /admin/site_settings.

However, such a site setting does not exist. What do I do?

It’s a hidden setting. Try changing it in the rails console.

bin/rails c

SiteSetting.port = 3000

This works, thank you.

Shouldn’t this be set to 3000 by default in development mode?

No, I don’t think so. Not everyone is using port 3000 for development.

It’s the default, though, isn’t it? That one has to set the hidden port setting is not at all obvious, hence seeing a logo in the development environment not load is unexpected. In other words, setting a custom logo should just work.

Logos work. It’s not something you usually care about in development.

Alternatively, I believe you can use these entries in sitesettings.yml

developer:
  force_hostname:
    hidden: false
    default: '<my-ip>'
  port:
    hidden: false
    default: '<my-port>'

@pfaffman gave me that tip! (and what a life saver that was)

I had the opposite problem to that of the original poster when setting up a dev site yesterday: I’m running my dev server behind a Nginx proxy so I want clean URL’s, but no matter what I changed in my settings, Discourse kept adding :3000 to many of my internal links including the site logo. I eventually tracked this down to lines 291 - 293 in lib/discourse.rb:

if Rails.env.development? && SiteSetting.port.blank?
    url << ":#{ENV["UNICORN_PORT"] || 3000}"
end

Commenting out these lines made my logo and other links work correctly, although I don’t know if this can lead to other things breaking down the road. It does seem a bit counter-intuitive to override SiteSetting.port in this manner.

I played around with this a bit more, and it seems that everything actually works correctly if I just set SiteSetting.port = 443 and also check the box for ‘force https’ (and I guess it would also work with port 80 and no https). I had read somewhere that I ought to leave the port settings empty, but I guess that was bad advice.