The original post: /r/nginx by /u/Current_Cat4150 on 2025-03-06 01:42:15.

Hey everyone,

I’m pretty new to nginx and would love some insight on how to get this to work. Basically I have a proxy set up for my angular app that I want users to use. If it is a google bot, I want to check if I have a prerendered html (for seo) and if I do return that instead. However, nginx is testing my patience lol. How can I get my config to serve the html? Right now I can return the path to the file and the file is there but can’t get seem to serve it.

I’ve tried using try_files $static_file @proxy but that just gave me 404s and 403s. I know there has to be some way to make this work. Please HELP!

sites-enabled for reference

 location / {
 set $isBot 0;
 if ($http\_user\_agent ~\* "googlebot| a bunch more but I removed them for now">
 set $isBot 1;
 }

    set $static_file /var/www/main/static$uri/index.html;

    set $render 0;
    if (-f $static_file) {
      set $render 1$isBot;
    }

    if ($render = 11) {
      # TODO HELP just serve this html I cant get it to work
      rewrite ^ $static_file;
    }

    # proxy to my server running spa
    proxy_pass http://localhost:4200/;
    proxy_http_version 1.1;
    proxy_buffering off;
    proxy_connect_timeout 60s;
    proxy_read_timeout 5400s;
    proxy_send_timeout 5400s;
    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_cache_bypass $http_upgrade;
}