diff --git a/beetsplug/web/__init__.py b/beetsplug/web/__init__.py index bd4677bd8..3c18ebd5d 100644 --- a/beetsplug/web/__init__.py +++ b/beetsplug/web/__init__.py @@ -327,6 +327,7 @@ class WebPlugin(BeetsPlugin): 'host': u'127.0.0.1', 'port': 8337, 'cors': '', + 'reverse_proxy': False, 'include_paths': False, }) @@ -358,9 +359,50 @@ class WebPlugin(BeetsPlugin): r"/*": {"origins": self.config['cors'].get(str)} } CORS(app) + + # Allow serving behind a reverse proxy + if self.config['reverse_proxy']: + app.wsgi_app = ReverseProxied(app.wsgi_app) + # Start the web application. app.run(host=self.config['host'].as_str(), port=self.config['port'].get(int), debug=opts.debug, threaded=True) cmd.func = func return [cmd] + + +class ReverseProxied(object): + '''Wrap the application in this middleware and configure the + front-end server to add these headers, to let you quietly bind + this to a URL other than / and to an HTTP scheme that is + different than what is used locally. + + In nginx: + location /myprefix { + proxy_pass http://192.168.0.1:5001; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Scheme $scheme; + proxy_set_header X-Script-Name /myprefix; + } + + From: http://flask.pocoo.org/snippets/35/ + + :param app: the WSGI application + ''' + def __init__(self, app): + self.app = app + + def __call__(self, environ, start_response): + script_name = environ.get('HTTP_X_SCRIPT_NAME', '') + if script_name: + environ['SCRIPT_NAME'] = script_name + path_info = environ['PATH_INFO'] + if path_info.startswith(script_name): + environ['PATH_INFO'] = path_info[len(script_name):] + + scheme = environ.get('HTTP_X_SCHEME', '') + if scheme: + environ['wsgi.url_scheme'] = scheme + return self.app(environ, start_response) diff --git a/beetsplug/web/static/beets.js b/beetsplug/web/static/beets.js index 757f2cdab..ec9aae9b3 100644 --- a/beetsplug/web/static/beets.js +++ b/beetsplug/web/static/beets.js @@ -147,7 +147,7 @@ var BeetsRouter = Backbone.Router.extend({ }, itemQuery: function(query) { var queryURL = query.split(/\s+/).map(encodeURIComponent).join('/'); - $.getJSON('/item/query/' + queryURL, function(data) { + $.getJSON('item/query/' + queryURL, function(data) { var models = _.map( data['results'], function(d) { return new Item(d); } @@ -161,7 +161,7 @@ var router = new BeetsRouter(); // Model. var Item = Backbone.Model.extend({ - urlRoot: '/item' + urlRoot: 'item' }); var Items = Backbone.Collection.extend({ model: Item @@ -264,7 +264,7 @@ var AppView = Backbone.View.extend({ $('#extra-detail').empty().append(extraDetailView.render().el); }, playItem: function(item) { - var url = '/item/' + item.get('id') + '/file'; + var url = 'item/' + item.get('id') + '/file'; $('#player audio').attr('src', url); $('#player audio').get(0).play(); diff --git a/beetsplug/web/templates/index.html b/beetsplug/web/templates/index.html index 7c37c82d6..0fdd46d15 100644 --- a/beetsplug/web/templates/index.html +++ b/beetsplug/web/templates/index.html @@ -82,7 +82,7 @@ <% } %>
File
- download + download
<% if (lyrics) { %>
Lyrics
diff --git a/docs/changelog.rst b/docs/changelog.rst index cb8999726..47cfddfea 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -42,6 +42,8 @@ New features: resulting lists of tracks are concatenated. :bug:`2468` * :doc:`/plugins/missing`: A new mode lets you see missing albums from artists you have in your library. Thanks to :user:`qlyoung`. :bug:`2481` +* :doc:`/plugins/web` : Add new `reverse_proxy` config option to allow serving + the web plugins under a reverse proxy. Fixes: diff --git a/docs/plugins/web.rst b/docs/plugins/web.rst index 9f1bbcd99..73a2b9147 100644 --- a/docs/plugins/web.rst +++ b/docs/plugins/web.rst @@ -63,6 +63,9 @@ configuration file. The available options are: Default: 8337. - **cors**: The CORS allowed origin (see :ref:`web-cors`, below). Default: CORS is disabled. +- **reverse_proxy**: If true, enable reverse proxy support (see + :ref:`reverse-proxy`, below). + Default: false. - **include_paths**: If true, includes paths in item objects. Default: false. @@ -111,11 +114,37 @@ For example:: host: 0.0.0.0 cors: 'http://example.com' +.. _reverse-proxy: + +Reverse Proxy Support +--------------------- + +When the server is running behind a reverse proxy, you can tell the plugin to +respect forwarded headers. Specifically, this can help when you host the +plugin at a base URL other than the root ``/`` or when you use the proxy to +handle secure connections. Enable the ``reverse_proxy`` configuration option +if you do this. + +Technically, this option lets the proxy provide ``X-Script-Name`` and +``X-Scheme`` HTTP headers to control the plugin's the ``SCRIPT_NAME`` and its +``wsgi.url_scheme`` parameter. + +Here's a sample `Nginx`_ configuration that serves the web plugin under the +/beets directory:: + + location /beets { + proxy_pass http://127.0.0.1:8080; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Scheme $scheme; + proxy_set_header X-Script-Name /beets; + } + +.. _Nginx: https://www.nginx.com JSON API -------- - ``GET /item/`` ++++++++++++++