Nginx returns 404 error for some angularJS URLs for a while

I am using Nginx Server to surf on an AngularJS based website (with HTML True mode to avoid # in URLs). In addition, I configured Varnish to reduce server load. So, Varnish runs on port 80, and Nginx runs on port 81. I use prerender.io for the AngularJS SEO site.

The problem is that when the prerender tries to update the angular URL, Nginx returns a 404 error code.

My nginx.conf looks like this:

    server {
    listen       81 default_server;
    listen       [::]:81 default_server;
    server_name  abcxyzabc.com;
    root         /var/www/html/abcxyzabc/;

    location / {
        proxy_set_header X-Prerender-Token ****************;     

        set $prerender 0;
        if ($http_user_agent ~* "baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator") {
            set $prerender 1;
        }
        if ($args ~ "_escaped_fragment_") {
            set $prerender 1;
        }
        if ($http_user_agent ~ "Prerender") {
            set $prerender 0;
        }
        if ($uri ~* "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)") {
            set $prerender 0;
        }

        #resolve using Google DNS server to force DNS resolution and prevent caching of IPs
        resolver 8.8.8.8;

        if ($prerender = 1) {

            #setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
            set $prerender "service.prerender.io";
            rewrite .* /$scheme://$host$request_uri? break;
            proxy_pass http://$prerender;
        }

        location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
            expires 30d;
        }

        location /status {
            # Turn on nginx stats
            stub_status on;
            # I do not need logs for stats
            access_log   off;
            # Security: Only allow access from localhost IP #
            allow 127.0.0.1;
            # Send rest of the world to /dev/null #
            deny all;
        }

        gzip on;
        gzip_disable "msie6";

        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_min_length 256;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;

        error_page 404 /404.html;
        location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }

        location ~ /\. {
            deny  all;
        }
    }

    server {
        listen 81;
        server_name www.xyzabcxyz.com;
        return 301 $scheme://xyzabcxyz.com$request_uri;
        proxy_set_header X-Prerender-Token ***************;
    }

nginx, prerender URL-, Nginx 404. URL- . prerender nginx. . , . / .

+4

Source: https://habr.com/ru/post/1661765/


All Articles