有些时候,Nginx会为某些angularJS URL返回404错误

时间:2016-11-23 21:25:11

标签: angularjs nginx http-status-code-404 varnish prerender

我正在使用Nginx Server浏览基于AngularJS的网站(HTML模式为True以避免URL中的#)。除此之外,我已经配置了Varnish以减少服务器上的负载。因此Varnish在端口80上运行,Nginx在端口81上运行。我使用prerender.io来获取AngularJS网站的SEO。

问题是,当prerender尝试重新缓存角度URL时,Nginx会返回404错误代码。

我的nginx.conf如下:

    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's 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工作正常。在nginx配置中插入prerender代码后引入了这种情况。在一切都很好之前。请帮帮我。任何解决方案/建议都会有所帮助。

0 个答案:

没有答案