3

Apache is not serving some content from a mounted network share for me on Ubuntu.

Depending on the content of the file I try to open it is not correctly served:

This does not work:

is a link to another nifty site
<H2>This is a Medium Header</H2>

Chrome issues the following error message: "ERR_INVALID_HTTP_RESPONSE"

Postman: "Error: Parse Error: Expected HTTP/"

curl: "curl: (1) Received HTTP/0.9 when not allowed"

This works (just one more blank line)

is a link to another nifty site
<H2>This is a Medium Header</H2>


The network share is mounted in /media/data (cifs; for testing purposes permissions are set to 777)

There's a symbolic link from /var/www/server to /media/data and i can see and change the contents from inside the VM.

It does not make to much sense to me that apache is picky about the file contents. I'm not entirely sure how to interpret the error messages from the 3 different clients, but since all of them are failing it looks like Apache is the issue even though the error log does not show any problems.

After copying the file to /var/www/html (default directory) and changing the vhost to that location the files get served no matter what the content is.

I don't know if this does matter at all but while searching "ERR_INVALID_HTTP_RESPONSE" online I found some people talking about chrome behaving weirdly on some ports: I'm currently running the server on Port 28080 (for backwards compatibility with the current setup and because I'm planning on setting up nginx on port 80); nginx is already installed but I don't think it's interfering.

Please excuse me if this sounds somewhat confusing but that's propably just because I'm utterly confused myself.

This is a clean VM, the only additional packages are apache, nginx and cifs-utils as far as I know.

Has anyone else encountered weird behaviour with network shares in this context? I want the htdocs on the network share since it is easier for me to access from my windows machine, are there any recommended ways of accessing the htdocs other than a network share?

Edit:

curl --verbose --output out.txt --http0.9 localhost:28080/test.html gives the following console output:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 127.0.0.1:28080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 28080 (#0)
> GET /test.html HTTP/1.1
> Host: localhost:28080
> User-Agent: curl/7.68.0
> Accept: */*
>
{ [15 bytes data]
100   294    0   294    0     0     58      0 --:--:--  0:00:05 --:--:--     0
* Closing connection 0

While out.txt contains the following:

 18:55:12 GMT
ETag: *removed*
Accept-Ranges: bytes
Content-Length: 67
Content-Type: text/html

is a link to another nifty site
<H2>This is a Medium Header</H2>
^@^@^@^@^@^@�)^@^@^@^@^@^@w�^@^@�r�X]﹟^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^X^@^@^@^@^@^@^@����������������^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@

On another file the weird string at the end actually contains the keyword SMB. I don't know if this is a coincidence or if this actually means something:

 19:34:11 GMT
ETag: *removed*
Accept-Ranges: bytes
Content-Length: 6
Content-Type: text/plain

sadssa^@^@^@^@]﹟^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@)^@^A^Re ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@�����������������SMB@^@^@^@^@^@^@^@^F^@
^@^D^@^@^@^@^@^@^@�)^@^@^@^@^@^@w�^@^@�r�X]﹟^@^@^@^@^@

I also noticed the r and X part which could have something to do with the file permissions?

Edit 2:

network share (fstab):

//www.example.com/Share      /media/data     cifs    username=user,password=pass,domain=WORKGROUP,vers=2.0,file_mode=0777,dir_mode=0777      0       0

symlink established with:

sudo ln -s /media/data/ /var/www/server

apache2.conf:

[...]
<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>
[...]
7
  • 1
    If Apache is returning an invalid answer, it would be helpful if you could dump the raw answer (or curl verbose debug) and include it here. Does curl work with the --http0.9 flag? Try also --http2-prior-knowledge.
    – harrymc
    Jul 4, 2020 at 19:33
  • @harrymc : I added the output for --http0.9. --http2-prior-knowledge throws: http2 error: Remote peer returned unexpected data while we expected SETTINGS frame. Perhaps, peer does not support HTTP/2 properly.
    – Jojomatik
    Jul 5, 2020 at 10:08
  • You seem to have zero bytes at the end of the file. Try removing them and ensure an end-of-line on the last line.
    – harrymc
    Jul 5, 2020 at 10:14
  • 1
    Could you write out the commands you use to mount and create the symlink? Add also the permissions of the folders. Do you have Options +FollowSymLinks in httpd.conf?
    – harrymc
    Jul 5, 2020 at 11:03
  • 1
    I see somebody else found first the www-data problem. Have fun.
    – harrymc
    Jul 5, 2020 at 14:19

4 Answers 4

2
+50

Do not symlink /media/data to /var/www/server but use bindfs instead:

# Install bindfs
sudo apt install bindfs

# Stop apache
sudo systemctl stop apache2

# Remove symlink
sudo rm /var/www/server

# Create dir
sudo mkdir /var/www/server

# Bind mount
sudo bindfs -u www-data -g www-data /media/data /var/www/server

# Start apache
sudo systemctl start apache2

If the served webpage is static or uses a database as backend you can also add the -r option to the bindfs call, to make the mount read-only.

Add an entry to /etc/fstab to automount at boot:

/media/data /var/www/server fuse.bindfs user=www-data,force-group=www-data 0 0

Most webservers expect the files to be owned by www-data and need certain capabilities of the filesystem that a samba mount does not provide. A symlink does not solve those issues. The bindfs solution provides all those requirements to the webserver by adding another, transparent layer of abstraction. FWIW, I would consider the bindfs approach to be the much cleaner solution.

3
  • This does actually work. But in my opinion it does feel more like a workaround than a solution to my problem. Do you have any explanation why it should not work with symlink?
    – Jojomatik
    Jul 5, 2020 at 11:02
  • 1
    I extended my answer. Hope this helps.
    – dirdi
    Jul 5, 2020 at 11:20
  • Thank you. Makes sense that apache expects www-data as the owner. I still wonder why it has worked on some occasions before though, but if it works consistanly with this approach it's fine to me.
    – Jojomatik
    Jul 5, 2020 at 12:37
4

In /etc/apache2/apache2.conf, add:

EnableSendfile Off
EnableMMAP off

This saved my life.

1
  • 1
    You saved my life ;).
    – Sơn Lâm
    Jun 15, 2022 at 6:39
0

This could also be an incompatibility with SendFile. See this answer: https://serverfault.com/a/62732

Or an incompatibility with MMAP. See this answer: https://serverfault.com/a/1047215/191716

-1

I recently encountered an issue with AKS, PHP, and mounting a file share via CSI and a blob container via Fuse. When attempting to access images using curl, the error "Received HTTP/0.9 when not allowed" was displayed. Although allowing HTTP 0.9 did allow access, it was slow and not suitable for page navigation.

However, thanks to the solution provided by Jack Wang, the issue was resolved. I would like to thank him for his help.

Other views of same problem: https://serverfault.com/questions/1090235/unable-to-serve-images-from-wordpress-site-response-is-malformed

Or this: https://github.com/Azure/AKS/issues/2614

1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Mar 21, 2023 at 5:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .