How I Fixed the Connection Reset Error Caused by Windows Line Endings on a Linux Server in Symfony 7 with a simple command, executable in prod ( Twig templates )

Alberto Robles
2 min readOct 20, 2024

Hey there, I’ve been battling this pesky “Connection Reset” error on my Linux server, and it was driving me nuts! You know, I did all the typical optimizations — cleared caches, optimized performance settings, checked configurations — and still, the error persisted. Super frustrating, right?

It turns out that the culprit was something much simpler than I had thought.

It was the line endings in my Twig templates! 😱

So, here’s what happened. I developed my Symfony app locally on a Windows machine (classic setup, right?), and then uploaded everything to my Linux server using FTP. Yeah, I know — FTP is old school, but when the price is right on a VPS, you make do!

What I didn’t realize is that Windows and Linux handle End-Of-Line (EOL) characters differently. Windows uses CR+LF (Carriage Return + Line Feed), while Linux uses just LF (Line Feed). So, when I uploaded the files, all my .twig templates still had the Windows-style EOLs, and that messed up the server.

This mismatch in line endings caused my Linux server to stumble, and the only symptom I saw was a “Connection Reset” error. 😑

The Fix? Use sed to Convert Those Line Endings

Here’s what I did to clean up all my Twig files and convert them to Unix-style line endings across my entire project:

find . -name "*.twig" -type f -exec sed -i 's/\r$//' {} +

What this does is go through every folder and subfolder, find all the .twig files, and use sed to remove those pesky Windows-style \r characters.

Basically:

  • find . looks for all files in the current directory and subdirectories.
  • -name "*.twig" ensures it’s only targeting Twig files.
  • -exec sed -i 's/\r$//' {} strips out the Windows-style \r from the end of each line.

After that, I cleared the Symfony cache with:

php bin/console cache:clear --env=prod

And guess what? No more connection resets! 🙌 My server was happy, and so was I.

So, if you’re getting a weird “Connection Reset” error after uploading files from a Windows machine to a Linux server, check those line endings! It might just save you hours of head-scratching.

I hope this helps anyone dealing with the same headaches I had!

--

--

Alberto Robles
Alberto Robles

No responses yet