Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base path feature malformed CLI output and lacking documentation #1402

Closed
contactalexliu opened this issue Mar 12, 2022 · 2 comments · Fixed by #1404
Closed

Base path feature malformed CLI output and lacking documentation #1402

contactalexliu opened this issue Mar 12, 2022 · 2 comments · Fixed by #1404
Labels
bug troubleshooting Tickets in which an issue was happening which was not obvious to solve and required support
Milestone

Comments

@contactalexliu
Copy link

contactalexliu commented Mar 12, 2022

How Shlink is set-up

  • Shlink Version: 3.0.3
  • PHP Version: 8.1.3
  • How do you serve Shlink: Self-hosted nginx reverse-proxy in front of self-hosted openswoole
  • Database engine used: MariaDB 10.6.7

Summary

The base path feature seems bugged. Short URLs return 404 errors, the API endpoints return 404 errors, and the CLI output does not look correct (though unfortunately this last issue seems cosmetic).

Current behavior

The DEFAULT_DOMAIN is set to example.com and the BASE_PATH is set to shlink in generated-config.php, but the Short URL field output by bin/cli short-url:list is https://example.comshlink/shlink/ZJxAM instead of the expected https://example.com/shlink/ZJxAM. When trying to use the short URL https://example.com/shlink/ZJxAM, Shlink shows the generic 404 page and logs a regular_404 error in the database.

Edit 1: When BASE_PATH is set to /s, the Short URL output
becomes https://example.com/s/s/ZJxAM. Changing the nginx
configuration to match does not solve the issue: Shlink still shows the
generic 404 page and logs a regular_404 error in the database (with
the visited_url correctly logged as https://example.com/s/s/ZJxAM, suggesting the CLI output is wrong). I’ve tried setting BASE_PATH to s, /s, /s/, and leaving it empty and putting example.com/s in the DEFAULT_DOMAIN, all without luck.

Edit 2: Now things are looking weird. Even when I configure BASE_PATH to be empty (the Short URL becomes https://example.com/ZJxAM) and use nginx to rewrite the path before passing it on to openswoole so Shlink sees https://example.com/s/ZJxAM as just https://example.com/ZJxAM, I’m getting 404 errors.

Edit 3: Deleted data/cache/fastroute_cached_routes.php; solved the empty base path 404 problem in Edit 2. I will let nginx continue “lying” to Shlink for now as a workaround.

Edit 4: Deleting data/cache/fastroute_cached_routes.php was the missing magic sauce. It seems like I need to manually do this step every time I test a new BASE_PATH even though vendor/bin/shlink-installer set-option claims it has “properly updated” the configuration. This would be a really great note to add to the troubleshooting documentation, along with an example of how to set BASE_PATH so people don’t need to guess whether to use path (which doesn’t work) or /path (works) or /path/ (haven’t tested)!

Now I have confirmed that setting BASE_PATH to /shlink works, redirecting https://example.com/shlink/ZJxAM as expected. However, bin/cli short-url:list continues to return malformed Short URLs like https://example.com/shlink/shlink/ZJxAM. At least it’s confirmed the issue is cosmetic.

Expected behavior

When the DEFAULT_DOMAIN is set to example.com and the BASE_PATH is set to shlink in generated-config.php, the Short URL field output by bin/cli short-url:list should be https://example.com/shlink/ZJxAM, not https://example.comshlink/shlink/ZJxAM. When trying to use the short URL https://example.com/shlink/ZJxAM, Shlink redirects according to the configuration (301) and logs the visit normally in the database.

How to reproduce

Install Shlink from the openswoole dist release on an Ubuntu 20.04 LTS Minimal Google Compute Engine instance with nginx and mariadb, configuring the default domain as example.com and the base path as shlink.

nginx configuration
server {
    listen      %ip%:%web_ssl_port% ssl http2;
    server_name %domain_idn% %alias_idn%;
    root        %docroot%;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/%domain%.log combined;
    access_log  /var/log/nginx/domains/%domain%.bytes bytes;
    error_log   /var/log/nginx/domains/%domain%.error.log error;

    ssl_certificate      %ssl_pem%;
    ssl_certificate_key  %ssl_key%;
    ssl_stapling on;
    ssl_stapling_verify on;

    include %home%/%user%/conf/web/%domain%/nginx.hsts.conf*;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~ /\.(?!well-known\/) {
        deny all;
        return 404;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
        location ~* ^.+\.(ogg|ogv|svg|svgz|swf|eot|otf|woff|woff2|mov|mp3|mp4|webm|flv|ttf|rss|atom|jpg|jpeg|gif|png|webp|ico|bmp|mid|midi|wav|rtf|css|js|jar)$ {
            expires 30d;
            fastcgi_hide_header "Set-Cookie";
        }
        
        location ~* /(?:uploads|files)/.*.php$ {
            deny all;
            return 404;
        }
        
        location ~ [^/]\.php(/|$) {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            try_files $uri =404;
            fastcgi_pass %backend_lsnr%;
            fastcgi_index index.php;
            include /etc/nginx/fastcgi_params;
            include %home%/%user%/conf/web/%domain%/nginx.fastcgi_cache.conf*;
            if ($request_uri ~* "/wp-admin/|wp-.*.php|xmlrpc.php|index.php|/store.*|/cart.*|/my-account.*|/checkout.*") {
                set $no_cache 1;
            }
            if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|woocommerce_items_in_cart|woocommerce_cart_hash|PHPSESSID") {
                set $no_cache 1;
            }
        }
    }

    location /error/ {
        alias   %home%/%user%/web/%domain%/document_errors/;
    }

    location /vstats/ {
        alias   %home%/%user%/web/%domain%/stats/;
        include %home%/%user%/web/%domain%/stats/auth.conf*;
    }

    location ^~ /shlink/ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://localhost:8080;
        proxy_read_timeout 90s;
    }

    proxy_hide_header Upgrade;

    include /etc/nginx/conf.d/phpmyadmin.inc*;
    include /etc/nginx/conf.d/phppgadmin.inc*;
    include %home%/%user%/conf/web/%domain%/nginx.ssl.conf_*;
}
@contactalexliu contactalexliu changed the title Base path feature not working Error 404/Base path feature not working Mar 12, 2022
@contactalexliu contactalexliu changed the title Error 404/Base path feature not working Base path feature malformed CLI output and lacking documentation Mar 12, 2022
@acelaya
Copy link
Member

acelaya commented Mar 12, 2022

The base URL needs to include the leading bar, so in your case it should be /shlink, not shlink.

I'll try to improve the documentation to make it clear.

Edit 4: Deleting data/cache/fastroute_cached_routes.php was the missing magic sauce

This one is a bug. This particular option affects routes, which have a separated cached config. I missed this use case.

I'll get it fixed.

However, bin/cli short-url:list continues to return malformed Short URLs like https://example.com/shlink/shlink/ZJxAM.

I'll investigate this one.

@acelaya acelaya added the troubleshooting Tickets in which an issue was happening which was not obvious to solve and required support label Mar 12, 2022
@acelaya acelaya added this to the 3.1.0 milestone Mar 14, 2022
@acelaya
Copy link
Member

acelaya commented Mar 14, 2022

I have just fixed the missing deletion of the fastroute config cache, and the cosmetic duplicated base path. Both fixes will be included in Shlink 3.1.0

I will update the documentation about the base path next.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug troubleshooting Tickets in which an issue was happening which was not obvious to solve and required support
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants