2

I have my angular project deployed on nginx server in which angular universal is applied. Everything works fine. But when user log in and reload page then its showing '504 Gateway Time-out'.

My nginx configuration file is like below:

server {
    listen 80;
    listen [::]:80;

    server_name mysite.at;
    server_name www.mysite.at;

    return 301 https://mysite.at$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl_certificate /etc/letsencrypt/live/mysite.at/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mysite.at/privkey.pem; # managed by Certbot
    include /etc/nginx/snippets/sslsetup.conf;

    server_name www.mysite.at;

    return 301 https://mysite.at$request_uri;

}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl_certificate /etc/letsencrypt/live/mysite.at/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mysite.at/privkey.pem; # managed by Certbot
    include /etc/nginx/snippets/sslsetup.conf;

    server_name mysite.at;

    root /var/www/html/dist/mysite/browser;
    index index.php index.html;

    location / {
       try_files $uri $uri @backend;      
    }
    
    location @backend {
        proxy_pass http://localhost:4000;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_http_version 1.1;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location ~ \.php$ {
       include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
    }

    location ~ /\.ht {
       deny all;
    }

    include snippets/phpmyadmin.conf;
} 

What I have tried:

Above changes tried in backend nginx configuration file and its server files as mentioned in accepted anwser

  • In frontend nginx config file

    location @backend { proxy_pass http://localhost:4000; proxy_http_version 1.1; proxy_cache_bypass $http_upgrade;

         proxy_set_header Upgrade           $http_upgrade;
         proxy_set_header Connection        "upgrade";
         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_set_header X-Forwarded-Host  $host;
         proxy_set_header X-Forwarded-Port  $server_port;
    
         port_in_redirect off;
         proxy_redirect http://localhost:4000  /;
    
         proxy_connect_timeout              60s;
         proxy_send_timeout                 60s;
         proxy_read_timeout                 60s;
    }
    

in frontend config file

location ~ \.php$ {
       include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
       fastcgi_read_timeout 500;
    }
  • In frontend config file, below changes tried

How to set http-https ProxyPassReverse of Apache in Nginx server

Any changes that I tried didn't work.

I thought this may be problem of state management as before login pages are reloading by browser reload but after login pages are not reloading, gives 504 error.

I am thinking that localstorage not working in angular SSR even though there is no error. So tried cookies instead. But still 504 issue persist. Is there any other plugin like 'vuex-persist' which I should try that will work in angular SSR instead of localStorage?

Main problem is AFTER LOGIN pages are not reloading by browser reload/F5, results in 504 Gateway time out issue.

What should I do to resolve this issue? Please help and guide.

ganesh
  • 416
  • 1
  • 11
  • 32
  • 504 timeouts usually means the the `upstream` server did not respond. It can happen if your backend is taking too much time to reply and Nginx times out. Can you confirm that without Nginx, your backend replies relatively quickly (<30s) ? – Manish Dash Apr 14 '22 at 10:06
  • Also post the exact URL or Nginx log lines of the 504 error – Manish Dash Apr 14 '22 at 10:07
  • @ManishDash 2022/04/14 10:31:08 [error] 19595#19595: *142 upstream timed out (110: Connection timed out) while reading response header from upstream, client: <>, server: http://<>, request: "GET /user-timeline HTTP/1.1", upstream: "http://127.0.0.1:4000/timeline", host: "<>" – ganesh Apr 14 '22 at 10:39
  • @ManishDash how can I confirm without Nginx that backend responds < 30s? – ganesh Apr 14 '22 at 10:41
  • Thanks. Now if you do a normal `curl localhost:4000/timeline` from the same server nginx resides, how much time does it take to load? – Manish Dash Apr 14 '22 at 10:42
  • @ManishDash how can I check time of `curl localhost:4000/timeline` using putty or else? could you please tell me the steps to check time as I am unknown to this? – ganesh Apr 14 '22 at 11:01
  • `curl -o /dev/null -s -w 'Total: %{time_total}s\n' localhost:4000/timeline` – Manish Dash Apr 14 '22 at 11:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243894/discussion-between-ganesh-and-manish-dash). – ganesh Apr 14 '22 at 11:26

0 Answers0