seblucas / cops

Calibre OPDS (and HTML) PHP Server : web-based light alternative to Calibre content server / Calibre2OPDS to serve ebooks (epub, mobi, pdf, ...)
http://blog.slucas.fr/en/oss/calibre-opds-php-server
GNU General Public License v2.0
1.44k stars 229 forks source link

NGINX + url rewriting = error 404 (NGINX bug?) #353

Closed marioscube closed 7 years ago

marioscube commented 7 years ago

I use COPS + Apache with my kobo e-reader without any problems.

I thought I'd try COPS on NGINX to see if it's any faster on my hardware. It works like a charm except with $config['cops_use_url_rewriting'] = '1'; And I added: location /download/ { rewrite ^/download/(\d+)/(\d+)/..(.)$ /fetch.php?data=$1&db=$2&type=$3 last; rewrite ^/download/(\d+)/..(.)$ /fetch.php?data=$1&type=$2 last; break; } to a working NGINX config file (and rebooting etc.) but I still get the 404 error.

What am I doing wrong? What do I have to add to COPS config files and or NGINX config files to make url_rewriting work?

Edit: NGINX version 1.6.2 PHP 5.6.30 OS Debian Jessie 8.8 on VirtualBox / WIndows 7 - 32 bits

Shadow4711 commented 7 years ago

Did you try this this?

marioscube commented 7 years ago

All, except:

location /Calibre { root /home/calibre/Dropbox; internal; }

and

$config['calibre_internal_directory'] = '/Calibre/';

I don't get how that should work with my libraries (for testing purposes) in /var/www/html/cops/

Also I did not include in config_local.php $config['cops_x_accel_redirect'] = "X-Accel-Redirect"; when I did it made no difference.

Does it work for you?

Shadow4711 commented 7 years ago

I am using Apache and I also have no Kobo reader. Just saw that your posted config is slightly different to the wiki.

seblucas commented 7 years ago

@marioscube As you suspected, the internal location has nothing to do with URL Rewriting, it's used only if you enable X-Accel-Redirect.

Is COPS installed in a directory or in the root of your webserver (do you access it with http://localhost/index.php or http://localhost/cops/index.php ?

I've always used COPS on the root of a subdomain with Nginx so there might a flaw in my rewrite rule.

marioscube commented 7 years ago

I access cops on nginx with http://lan-ip/cops/index.php

The rewrite rules (to me) look the same as used on apache where they work.

seblucas commented 7 years ago

I access cops on nginx with http:///cops/index.php

I don't understand, in your url cops is the server name or a directory ?

marioscube commented 7 years ago

Problem with accessing github from an iPad.

Http://localhost/cops/index.php

Shadow4711 commented 7 years ago

Could you try

rewrite ^/download/(\d+)/(\d+)/.*\.(.*)$ /cops/fetch.php?data=$1&db=$2&type=$3 last; rewrite ^/download/(\d+)/.*\.(.*)$ /cops/fetch.php?data=$1&type=$2 last;

I am not completely sure if it has to be /cops/download/ instead of /download/, but we will see.

marioscube commented 7 years ago

Even with several permutations of /cops/ and /download/ i still get the error "404 Not Found" on 2 different virtual machines.

But I can agree with you that if I access cops through http://localhost/index.php rewriting seems to work fine.

so root /var/www/html/cops; works, but root /var/www/html; doesn't!

Shadow4711 commented 7 years ago

Could you please post the relevant part of log?

marioscube commented 7 years ago

Something like this?

2017/05/25 23:14:45 [error] 1632#0: *7 open() "/var/www/html/cops/download/3/0/Poema - Merete Junker.epub" failed (2: No such file or directory), client: 192.168.1.109, server: _, request: "GET /cops/download/3/0/Poema%20-%20Merete%20Junker.epub HTTP/1.1", host: "192.168.1.116", referrer: "http://192.168.1.116/cops/index.php?page=10&db=0"

The previous line is from the /var/log/nginx/error.log. However this log is not always triggered when a 404 error is triggered. [fixed]

Might this part of a nginx config file have something to do with the rewrite error? (I don't really understand nginx yet): location / {

First attempt to serve request as file, then

    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
                   }

Edit: probably not. Just tested.

Shadow4711 commented 7 years ago

Seems that the rule is not matching. Did you also change location?

location /cops/download/ { rewrite ^/cops/download/(\d+)/(\d+)/.*\.(.*)$ /cops/fetch.php?data=$1&db=$2&type=$3 last; rewrite ^/cops/download/(\d+)/.*\.(.*)$ /cops/fetch.php?data=$1&type=$2 last; break; }

marioscube commented 7 years ago

I thought I had tried this permutation as well, but ............

this seems to work!! (I have not yet tried it with a Kobo) -> Thank you!

However I see no reason why the original rewrite rules do not work.

For me it's no problem, but for some other users it might be a bit harder to understand which nginx config file to edit and why as not all nginx installs are the same (I found out).

But again thank you for COPS! :D

seblucas commented 7 years ago

@Shadow4711 Thanks for your help.

I was trying the same trick. I read that Nginx always use the full URL so your proposed changes make sense

marioscube commented 7 years ago

Tested with my Kobo Glo HD and it works.

@Shadow4711 & @seblucas thank you!

If there are no further comments in the next few days I will close this issue.

[no comments or questions -> the issue is closed on 1 june 2017]