fix: improve Redis connection error messages and add staging troubleshooting playbooks

- Improve Redis connection error message to include password info
- Add Ansible playbooks for staging 502 error troubleshooting
  - check-staging-status.yml: Check nginx logs and upstream config
  - fix-staging-502-verify.yml: Fix and verify nginx upstream configuration
This commit is contained in:
2025-11-02 02:16:12 +01:00
parent 70875be4b9
commit 4cec6dd8a0
3 changed files with 142 additions and 2 deletions

View File

@@ -0,0 +1,52 @@
---
- name: Check Staging Status Complete
hosts: production
gather_facts: yes
become: no
tasks:
- name: Check nginx error log for recent 502s
shell: |
cd ~/deployment/stacks/staging
docker compose exec -T staging-nginx tail -20 /var/log/nginx/error.log 2>&1 | grep -E "(502|Bad Gateway|upstream)" || echo "No 502 errors in recent logs"
args:
executable: /bin/bash
register: nginx_errors
ignore_errors: yes
failed_when: false
- name: Display nginx errors
debug:
msg: "{{ nginx_errors.stdout_lines }}"
- name: Verify upstream configuration one more time
shell: |
cd ~/deployment/stacks/staging
docker compose exec -T staging-nginx grep -A 3 "upstream php-upstream" /etc/nginx/sites-available/default
docker compose exec -T staging-nginx grep "fastcgi_pass" /etc/nginx/sites-available/default | head -3
args:
executable: /bin/bash
register: upstream_check
ignore_errors: yes
failed_when: false
- name: Display upstream check
debug:
msg: "{{ upstream_check.stdout_lines }}"
- name: Test multiple times
shell: |
for i in 1 2 3; do
echo "Test $i:"
curl -H "User-Agent: Mozilla/5.0" -H "Cache-Control: no-cache" -s -o /dev/null -w " HTTP %{http_code}\n" https://staging.michaelschiemer.de/ || echo " Failed"
sleep 1
done
args:
executable: /bin/bash
register: multi_test
ignore_errors: yes
failed_when: false
- name: Display multi test results
debug:
msg: "{{ multi_test.stdout_lines }}"

View File

@@ -0,0 +1,88 @@
---
- name: Fix and Verify Staging 502 - Complete Fix
hosts: production
gather_facts: yes
become: no
tasks:
- name: Check current upstream configuration
shell: |
cd ~/deployment/stacks/staging
echo "=== Current upstream config in sites-available/default ==="
docker compose exec -T staging-nginx grep -A 3 "upstream php-upstream" /etc/nginx/sites-available/default 2>&1 || echo "Could not read config"
args:
executable: /bin/bash
register: current_config
ignore_errors: yes
failed_when: false
- name: Display current config
debug:
msg: "{{ current_config.stdout_lines }}"
- name: Fix upstream configuration (multiple methods)
shell: |
cd ~/deployment/stacks/staging
echo "=== Fixing nginx upstream configuration ==="
# Method 1: Fix in upstream block
docker compose exec -T staging-nginx sed -i '/upstream php-upstream {/,/}/s|server 127.0.0.1:9000;|server staging-app:9000;|g' /etc/nginx/sites-available/default || echo "Method 1 failed"
docker compose exec -T staging-nginx sed -i '/upstream php-upstream {/,/}/s|server localhost:9000;|server staging-app:9000;|g' /etc/nginx/sites-available/default || echo "Method 2 failed"
# Method 2: Fix any fastcgi_pass
docker compose exec -T staging-nginx sed -i 's|fastcgi_pass 127.0.0.1:9000;|fastcgi_pass php-upstream;|g' /etc/nginx/sites-available/default || echo "Method 3 failed"
docker compose exec -T staging-nginx sed -i 's|fastcgi_pass localhost:9000;|fastcgi_pass php-upstream;|g' /etc/nginx/sites-available/default || echo "Method 4 failed"
# Method 3: Replace entire upstream block if it still has wrong value
docker compose exec -T staging-nginx sh -c "grep -q 'server 127.0.0.1:9000' /etc/nginx/sites-available/default && sed -i '/upstream php-upstream {/,/}/c\upstream php-upstream {\n server staging-app:9000;\n}' /etc/nginx/sites-available/default || echo 'No 127.0.0.1 found'" || echo "Method 5 failed"
echo "=== Verification ==="
docker compose exec -T staging-nginx grep -A 3 "upstream php-upstream" /etc/nginx/sites-available/default
args:
executable: /bin/bash
register: fix_result
ignore_errors: yes
failed_when: false
- name: Display fix result
debug:
msg: "{{ fix_result.stdout_lines }}"
- name: Test nginx configuration
shell: |
cd ~/deployment/stacks/staging
docker compose exec -T staging-nginx nginx -t 2>&1
args:
executable: /bin/bash
register: nginx_test
ignore_errors: yes
failed_when: false
- name: Display nginx test result
debug:
msg: "{{ nginx_test.stdout_lines }}"
- name: Restart nginx
shell: |
cd ~/deployment/stacks/staging
docker compose restart staging-nginx
sleep 3
args:
executable: /bin/bash
register: restart_result
ignore_errors: yes
failed_when: false
- name: Display restart result
debug:
msg: "{{ restart_result.stdout_lines }}"
- name: Test connection
shell: |
sleep 2
curl -H "User-Agent: Mozilla/5.0" -s -o /dev/null -w "HTTP Status: %{http_code}" https://staging.michaelschiemer.de/ || echo "502"
args:
executable: /bin/bash
register: test_result
ignore_errors: yes
failed_when: false
- name: Display test result
debug:
msg: "Final HTTP Status: {{ test_result.stdout }} (200/404 = OK, 502 = Still broken)"

View File

@@ -88,7 +88,7 @@ final class RedisConnection implements RedisConnectionInterface
throw new RedisConnectionException("Failed to connect to Redis server"); throw new RedisConnectionException("Failed to connect to Redis server");
} }
// Authenticate if password is provided // Authenticate if a password is provided
if ($this->config->password) { if ($this->config->password) {
if (! $this->client->auth($this->config->password)) { if (! $this->client->auth($this->config->password)) {
throw new RedisConnectionException("Redis authentication failed"); throw new RedisConnectionException("Redis authentication failed");
@@ -112,7 +112,7 @@ final class RedisConnection implements RedisConnectionInterface
$this->connected = false; $this->connected = false;
throw new RedisConnectionException( throw new RedisConnectionException(
"Failed to connect to Redis ({$this->name}): " . $e->getMessage() . " with Host: {$this->config->host}", "Failed to connect to Redis ({$this->name}): " . $e->getMessage() . " with Host: {$this->config->host} and Password: {$this->config->password}",
previous: $e previous: $e
); );
} }