Discourse 子目录支持


(Erick Guan) #1

本文翻译自官方论坛,于 2015-09-12。

将 Discourse 至于你域名的子目录中(即加目录前缀),像是 http://www.example.com/forum,本文是教程!

Docker 配置

在你 Docker 容器的 yml 文件的 env 小节中,增加 DISCOURSE_RELATIVE_URL_ROOT 设置为你想要的子目录名字。确保它没有以 / 结尾。

env:
  ...
  DISCOURSE_RELATIVE_URL_ROOT: /forum

run 小结中需要做一些更改将 Discourse 路由发送至正确的地方。以下是子目录支持所需要的文完整代码:

run:
    - exec:
        cd: $home
        cmd:
          - mkdir -p public/forum
          - cd public/forum && ln -s ../uploads && ln -s ../backups
          - rm public/uploads
          - rm public/backups
    - replace:
       global: true
       filename: /etc/nginx/conf.d/discourse.conf
       from: proxy_pass http://discourse;
       to: |
          rewrite ^/(.*)$ /forum/$1 break;
          proxy_pass http://discourse;
    - replace:
       filename: /etc/nginx/conf.d/discourse.conf
       from: etag off;
       to: |
          etag off;
          location /forum {
             rewrite ^/forum/?(.*)$ /$1;
          }
    - replace:
         filename: /etc/nginx/conf.d/discourse.conf
         from: $proxy_add_x_forwarded_for
         to: $http_fastly_client_ip
         global: true

在做了这些改变后,和平常一样初始化 Docker 容器,或者如果有已经存在的容器则重建。

./launcher bootstrap app

./launcher rebuild app

附件是一个完整的单容器的 yml 文件。

subfolder-sample.yml (3.1 KB)

已存在的主题

如果你的站点原来没有子域名,你会发现原来上传的文件失效了。有一个工具能帮助解决这个问题,将所有链接包含子目录名。进入容器后,进入 Discourse 目录(一般是 /var/www/discourse)。在备份后运行:

discourse remap '/uploads' '/forum/uploads'

在同一服务器上部署 Wordpress 和 Discourse 不成功
(悠悠掌柜) #2

@fantasticfears 站长你好,我的论坛今天从子目录改成子域名了(即原先是abc.com/forum 改成现在的 forum.abc.com),现在就是出现过去的一些帖子附件显示不出来了。我通过执行这个命令是无效的:
RAILS_ENV=production bundle exec script/discourse remap '/forum/uploads' '/uploads'

不知道正确的做法是什么呢?


(Erick Guan) #3

错误信息是什么?有没有进入容器执行?


(悠悠掌柜) #4

进入到容器了,执行命令之后提示如下代码:

root@VM-0-5-debian:/var/discourse# ./launcher enter app
root@VM-0-5-debian-app:/var/www/discourse# RAILS_ENV=production bundle exec script/discourse remap '/community/uploads' '/uploads'
bundler: failed to load command: script/discourse (script/discourse)
PG::ConnectionBad: FATAL:  Peer authentication failed for user "discourse"

  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/pg-0.20.0/lib/pg.rb:56:in `initialize'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/pg-0.20.0/lib/pg.rb:56:in `new'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/pg-0.20.0/lib/pg.rb:56:in `connect'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:695:in `connect'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:220:in `initialize'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in `new'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in `postgresql_connection'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:759:in `new_connection'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:803:in `checkout_new_connection'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:782:in `try_to_checkout_new_connection'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:743:in `acquire_connection'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:500:in `checkout'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `connection'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `retrieve_connection'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_handling.rb:116:in `retrieve_connection'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_handling.rb:88:in `connection'
  /var/www/discourse/lib/site_settings/db_provider.rb:62:in `table_exists?'
  /var/www/discourse/lib/site_settings/db_provider.rb:21:in `find'
  /var/www/discourse/lib/site_settings/defaults_provider.rb:109:in `block in refresh_site_locale!'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rails_multisite-1.1.2/lib/rails_multisite/connection_management.rb:132:in `block in each_connection'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rails_multisite-1.1.2/lib/rails_multisite/connection_management.rb:130:in `each'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rails_multisite-1.1.2/lib/rails_multisite/connection_management.rb:130:in `each_connection'
  /var/www/discourse/lib/site_settings/defaults_provider.rb:103:in `refresh_site_locale!'
  /var/www/discourse/lib/site_settings/defaults_provider.rb:20:in `initialize'
  /var/www/discourse/lib/site_setting_extension.rb:35:in `new'
  /var/www/discourse/lib/site_setting_extension.rb:35:in `defaults'
  /var/www/discourse/lib/site_setting_extension.rb:72:in `block in setting'
  /var/www/discourse/lib/site_setting_extension.rb:71:in `synchronize'
  /var/www/discourse/lib/site_setting_extension.rb:71:in `setting'
  /var/www/discourse/app/models/site_setting.rb:17:in `block in load_settings'
  /var/www/discourse/lib/site_settings/yaml_loader.rb:25:in `block (2 levels) in load'
  /var/www/discourse/lib/site_settings/yaml_loader.rb:11:in `each'
  /var/www/discourse/lib/site_settings/yaml_loader.rb:11:in `block in load'
  /var/www/discourse/lib/site_settings/yaml_loader.rb:10:in `each_key'
  /var/www/discourse/lib/site_settings/yaml_loader.rb:10:in `load'
  /var/www/discourse/app/models/site_setting.rb:16:in `load_settings'
  /var/www/discourse/app/models/site_setting.rb:21:in `<class:SiteSetting>'
  /var/www/discourse/app/models/site_setting.rb:4:in `<top (required)>'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `block in require'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:379:in `block in require_or_load'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:36:in `block in load_interlock'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies/interlock.rb:12:in `block in loading'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/concurrency/share_lock.rb:149:in `exclusive'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies/interlock.rb:11:in `loading'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:36:in `load_interlock'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:357:in `require_or_load'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:510:in `load_missing_constant'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:202:in `const_missing'
  /var/www/discourse/config/initializers/004-message_bus.rb:84:in `<top (required)>'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `load'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `block in load'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `load'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/engine.rb:655:in `block in load_config_initializer'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.4/lib/active_support/notifications.rb:168:in `instrument'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/engine.rb:654:in `load_config_initializer'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/engine.rb:612:in `block (2 levels) in <class:Engine>'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/engine.rb:611:in `each'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/engine.rb:611:in `block in <class:Engine>'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/initializable.rb:30:in `instance_exec'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/initializable.rb:30:in `run'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/initializable.rb:59:in `block in run_initializers'
  /usr/local/lib/ruby/2.4.0/tsort.rb:228:in `block in tsort_each'
  /usr/local/lib/ruby/2.4.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
  /usr/local/lib/ruby/2.4.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'
  /usr/local/lib/ruby/2.4.0/tsort.rb:431:in `each_strongly_connected_component_from'
  /usr/local/lib/ruby/2.4.0/tsort.rb:421:in `block in each_strongly_connected_component_from'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/initializable.rb:48:in `each'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/initializable.rb:48:in `tsort_each_child'
  /usr/local/lib/ruby/2.4.0/tsort.rb:415:in `call'
  /usr/local/lib/ruby/2.4.0/tsort.rb:415:in `each_strongly_connected_component_from'
  /usr/local/lib/ruby/2.4.0/tsort.rb:349:in `block in each_strongly_connected_component'
  /usr/local/lib/ruby/2.4.0/tsort.rb:347:in `each'
  /usr/local/lib/ruby/2.4.0/tsort.rb:347:in `call'
  /usr/local/lib/ruby/2.4.0/tsort.rb:347:in `each_strongly_connected_component'
  /usr/local/lib/ruby/2.4.0/tsort.rb:226:in `tsort_each'
  /usr/local/lib/ruby/2.4.0/tsort.rb:205:in `tsort_each'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/initializable.rb:58:in `run_initializers'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/application.rb:353:in `initialize!'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/railtie.rb:185:in `public_send'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/railtie.rb:185:in `method_missing'
  /var/www/discourse/config/environment.rb:5:in `<top (required)>'
  script/discourse:232:in `require'
  script/discourse:232:in `load_rails'
  script/discourse:32:in `remap'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/thor-0.19.4/lib/thor/command.rb:27:in `run'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/thor-0.19.4/lib/thor/invocation.rb:126:in `invoke_command'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/thor-0.19.4/lib/thor.rb:369:in `dispatch'
  /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/thor-0.19.4/lib/thor/base.rb:444:in `start'
  script/discourse:273:in `<top (required)>'
root@VM-0-5-debian-app:/var/www/discourse#

(Erick Guan) #5

试试执行这个:discourse remap '/forum/uploads' '/uploads'


(悠悠掌柜) #6

哈哈,可以了,这个命令有效!:blush: