The # in the url and why?

Questions, comments, discussions. Over time certain topics might be moved to their own category.
Post Reply
bwakkie
50 Posts
50 Posts
Posts: 73
Joined: Fri Feb 14, 2020 8:28 pm

The # in the url and why?

Post by bwakkie »

Hello,

I'm trying to move mayan to a subfolder http://www.domain.com:8000/ to https://www.domain.com/docs as mayan is not the only app I am integrating in my website.

I keep having a problem that url's are redirected with a # in front. I believe the following javascript is responsible for this in:
site-packages/mayan/apps/appearance/templates/appearance/base.html

Code: Select all

<script>
    if (typeof partialNavigation === 'undefined') {
        /* Check if the template was not loaded via AJAX
         * If not then reload the root path passing the
         * current location's path as the new hash
         */
        document.write('<script type="text/undefined">')
        var currentPath = window.location.pathname + window.location.search;
        window.location = '/#' + currentPath;
    }
</script>
Why is this needed and can I remove it? As it breaks my urls. (ps strangly I tried to change it into windows.location = '/' and that seamed to have no effect)

My current setup is to run mayan-gunicorn via a unix socket and using nginx proxy running from the unix socket including let's encrypt and redirects and multiple other apps I use.

An example:

http://domain.com/docs
... redirects directly to...
https://www.domain.com/docs/
then the above script in turn adds the # and the link can't be found on the server...
https://www.domain.com/#/docs/

How can I overcome this problem? Some redirection rule or will that give problems for AJAX calls?

Kind regards,
Bastiaan
User avatar
michael
Developer
Developer
Posts: 297
Joined: Sun Apr 19, 2020 6:21 am

Re: The # in the url and why?

Post by michael »

Hello,

Mayan became a Single Page App in version 3.0 => https://docs.mayan-edms.com/releases/3. ... e-page-app
More information about SPA here => https://en.wikipedia.org/wiki/Single-page_application

This has many advantages like reducing network traffic on navigation, speed because only the screen content that changes is transmitted and updated, memory as most JavaScript and CSS assets are loaded only once.

The content after the "#" symbol is called the fragment and is how SPAs keep track of the "Page" they are showing. Since the actual URL never changes, the fragment is how the app keeps track of state.

For what you are trying to, we provide a setting called ORGANIZATIONS_URL_BASE_PATH. This setting was part of the "common" app but was moved to a new "organizations" app for clarity => https://docs.mayan-edms.com/releases/4. ... anizations

Set this to the URL sub path you want, "docs" in your case.

Cheers!
User avatar
michael
Developer
Developer
Posts: 297
Joined: Sun Apr 19, 2020 6:21 am

Re: The # in the url and why?

Post by michael »

Since you are using NGINX as a reverse proxy Mayan will not have direct access to the HTTP request header and you need to tell it where to redirect. Use the setting ORGANIZATIONS_INSTALLATION_URL for this. Set it to the top level domain name "https://www.domain.com".
bwakkie
50 Posts
50 Posts
Posts: 73
Joined: Fri Feb 14, 2020 8:28 pm

Re: The # in the url and why?

Post by bwakkie »

Thanks Michael for the swift reply! Really appriciated, I'm struggeling with is for a while now... :-)

I tried setting the ORGANIZATION_* settings without any effect still no page:

Just for verification here are my configs:

mayan.conf:

Code: Select all

[supervisord]
 environment=
     PYTHONPATH="/data/.envs/mayan/mediauser_settings",
     DJANGO_SETTINGS_MODULE=mayan.settings.production,
     ORGANIZATIONS_URL_BASE_PATH="docs",
     ORGANIZATIONS_INSTALLATION_URL="https://www.domain.com/"
     MAYAN_ALLOWED_HOSTS='["*"]',
     MAYAN_MEDIA_ROOT="/data/.envs/mayan/media",
     MAYAN_CELERY_RESULT_BACKEND="redis://{PASS}@127.0.0.1:6379/1",
     MAYAN_CELERY_BROKER_URL="redis://{PASS}@127.0.0.1:6379/0",
     MAYAN_DATABASES="{default: {ENGINE: django.db.backends.postgresql, HOST: 127.0.0.1, NAME: mayan, PASSWORD: {PASS}, USER: mayan}}" 

[program:mayan-edms-gunicorn]
autorestart = true
autostart = true
command = /data/.envs/mayan38/bin/gunicorn --name mayan-edms --bind unix:/tmp/mayan.sock -w 3 mayan.wsgi --max-requests 500 --max-requests-jitter 50 --worker-class sync --timeout 120
/etc/nginx/sites-available/domain.comn.conf
... the mayan part... all other parts like pgadmin do work btw

Code: Select all

location /docs {
                  include                        proxy_params;
                  send_timeout                   600;
                  proxy_set_header               X-Real-IP $remote_addr;
                  proxy_set_header               X-Script-Name /docs;
                  proxy_set_header               Host $host;
                  proxy_send_timeout             600;
                  proxy_read_timeout             600;
  #               proxy_pass                     http://127.0.0.1:8000/;
                  proxy_pass                     http://unix:/tmp/mayan.sock;
                  proxy_connect_timeout          600;
                  proxy_set_header               X-Forwarded-Proto https;
                  proxy_set_header               X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_set_header               Host $http_host;
                  proxy_redirect                 off;
                  proxy_pass_header              Set-Cookie;
                  proxy_headers_hash_max_size    512;
                  proxy_headers_hash_bucket_size 128;
          }
Tried sooo many config changes I do not see the trees in the forest anymore. There might be a lot of overkill here 8-)

If I look to the sock directly ...

Code: Select all

curl --unix-socket /tmp/mayan.sock localhost
I do get response though ... which seams to be ok. The problem is somewhere between the gunicorn unix-socket file and nginx I think.

Any thoughts?
User avatar
michael
Developer
Developer
Posts: 297
Joined: Sun Apr 19, 2020 6:21 am

Re: The # in the url and why?

Post by michael »

Tried sooo many config changes I do not see the trees in the forest anymore. There might be a lot of overkill here 8-)
Been there way too many times :)

This is going to be hard to diagnose. I'll queue a lab environment with a reverse proxy to test this.

Thanks for the report and for the configuration details.
bwakkie
50 Posts
50 Posts
Posts: 73
Joined: Fri Feb 14, 2020 8:28 pm

Re: The # in the url and why?

Post by bwakkie »

I finally got the Mayan-EDMS application working in a subdirectory and by using unix sockets more or less... (will explain later)

I moved to use unix sockets for all applications mayan is interacting with for speed reasons (postgresql, redis, ngnix and mayan gunicorn) and yes the application reacts swifter. I do not have statistics though but there is no TCP overhead anymore

Code: Select all

                    +------------+
                    | redis.sock |
                    +------------+
                           |
                           |
                    +------------+
                    |   celery   |
                    +------------+
                           |
  (PostgreSQL)             |            (gunicorn)         (webserver)
+---------------+      +-------+      +------------+      +------------+
| .s.PGSQL.5432 |------| mayan |------| mayan.sock |------| ngnix.sock |
+---------------+      +-------+      +------------+      +------------+

Settings to consider in this setup:

postgresql.conf:

Code: Select all

unix_socket_directories = '/tmp'
/etc/redis/redis.conf:

Code: Select all

# bind 127.0.0.1 ::1
unixsocket /tmp/redis.sock
unixsocketperm 777
config.yml: A bit confusing but celery uses 'redis+socket://' and redis 'unix://'

Code: Select all

CELERY_BROKER_URL: redis+socket:///tmp/redis.sock?db=0                                                                                                 │
CELERY_RESULT_BACKEND: redis+socket:///tmp/redis.sock?db=1
LOCK_MANAGER_BACKEND_ARGUMENTS:
  redis_url: unix:///tmp/redis.sock?db=2
/etc/supervisor.d/mayan.conf
in the [supervisord] environment= section add:

Code: Select all

    ORGANIZATIONS_URL_BASE_PATH=docs,
    ORGANIZATIONS_INSTALLATION_URL=https://www.domain.com/docs,
in the [program:mayan-edms-gunicorn] section add unix socket bind:

Code: Select all

--bind unix:/tmp/mayan.sock
/etc/nginx/nginx.conf:

Code: Select all

		location /docs {
			proxy_pass       http://unix:/tmp/mayan.sock;
			proxy_set_header SCRIPT_NAME /docs;
			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 /docs;
		}
		location /static/ {
			root /data/mayan/media;
		}
Why it is working more or less?
The Mayan EDMS application is starting but I have the following problems:
1) After logging in I am not redirected to the correct addres:

Code: Select all

https://www.domain.com/#/docs/home/
should be

Code: Select all

https://www.domain.com/docs#/docs/home/
if I add the "docs/" section to the url I enter the application. My guess is that the redirection from the login page to the application home does not take into account the new ORGANIZATIONS_* variables.

2) I have the feeling tasks are not executed. I can upload a new document. I can see them in the database, but I cannot see any thumbnail nor content and nothing is indexed so somewhere there is still a problem with the celery |<--> redis part using unix sockets.

Can somebody have a look at the above settings and spot the problem?

Cheers,
Bastiaan
bwakkie
50 Posts
50 Posts
Posts: 73
Joined: Fri Feb 14, 2020 8:28 pm

Re: The # in the url and why?

Post by bwakkie »

Most is solved now. Just the redirection after login is still not working, ill create a new bug report for that only

https://gitlab.com/mayan-edms/mayan-edms/-/issues/1036
bwakkie
50 Posts
50 Posts
Posts: 73
Joined: Fri Feb 14, 2020 8:28 pm

Re: The # in the url and why?

Post by bwakkie »

Mayan Version 4.2.1 is fully working in a subdir and unix sockets via gunicorn and nginx (e.g. https://www.domain.com/docs) with the following settings:

mayan.conf:
add

Code: Select all

 --bind unix:/var/run/mayan/mayan.sock 
to the gunicorn command and

Code: Select all

    ORGANIZATIONS_URL_BASE_PATH=docs,
    ORGANIZATIONS_INSTALLATION_URL=https://www.domain.com/,
to the supervisor environment settings in mayan conf.

For the webserver:
nginx.conf:
Don't set the proxy_set_header SCRIPT_NAME and X-Script-Name as an empty white screen will be presented:

Code: Select all

	        location /docs {
		                proxy_pass       http://unix:/var/run/mayan/mayan.sock;
#		                proxy_set_header SCRIPT_NAME /docs;
		                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 /docs;
	        }

Hope this can help anyone.
waeking
Posts: 1
Joined: Wed Jun 15, 2022 4:31 am

Re: The # in the url and why?

Post by waeking »

Is this still working for you in 4.2.5?

I am using traefik and am having some difficulties. I can get the url to not expose the "#" by setting the headers as

Code: Select all

 
 DOMAIN_NAME: example.com
 CONTAINER_NAME: medms
 
   environment:
      - "ORGANIZATIONS_INSTALLATION_URL=https://${DOMAIN_NAME}/"
      - "ORGANIZATIONS_URL_BASE_PATH=${CONTAINER_NAME}"
   labels:
       - "traefik.http.services.${CONTAINER_NAME}.loadbalancer.server.port=${PORT:-8000}"
      - "traefik.http.routers.${CONTAINER_NAME}.entrypoints=web"
      - "traefik.http.routers.${CONTAINER_NAME}.rule=Host(`${DOMAIN_NAME}`) && PathPrefix(`/${CONTAINER_NAME}
      - "traefik.http.routers.${CONTAINER_NAME}.middlewares=https-redirect"
      - "traefik.http.middlewares.https-redirect.redirectscheme.scheme=https"
      - "traefik.http.routers.${CONTAINER_NAME}-secure.tls=true"
      - "traefik.http.routers.${CONTAINER_NAME}-secure.tls.certresolver=tlsresolver"
      - "traefik.http.routers.${CONTAINER_NAME}-secure.rule=Host(`${DOMAIN_NAME}`) && PathPrefix(`/${CONTAINER_NAME}`)"
      - "traefik.http.routers.${CONTAINER_NAME}-secure.middlewares=${CONTAINER_NAME}-sslheaders"
      - "traefik.http.middlewares.${CONTAINER_NAME}-sslheaders.headers.customrequestheaders.X-Script-Name=/${CONTAINER_NAME}"
      - "traefik.http.middlewares.${CONTAINER_NAME}-sslheaders.headers.customrequestheaders.SCRIPT_NAME=/${CONTAINER_NAME}"
I can access the website at example.com/medms. There is no format to the page. The links are all shown, If I hover over a link it shows as example.com/medms/about. When I click on the link it sends me to example.com/#/medms/about.
bwakkie
50 Posts
50 Posts
Posts: 73
Joined: Fri Feb 14, 2020 8:28 pm

Re: The # in the url and why?

Post by bwakkie »

I am not familiar with traefik but yes my settings are still working but with the #

After the first render of any Mayan page the URL is changed by adding the # via javascript as Mayan is a Single Page Application (see comment above^) so that the next link is loaded dynamically, as far as I understand.

So any link will always get the # and I can live with that, it's the way it works.

Still not super beautiful to see the subfolder 2 x though:

http://localhost/docs/about/ --becomes--> http://localhost/docs/#/docs/about/
Post Reply