配置ssl nginx服务器for prerender angular js app

时间:2018-02-21 20:35:24

标签: javascript angularjs node.js nginx prerender

你能告诉我如何配置nginx来预防我的应用程序。 本教程无法正常工作,当我使用抓取工具googlebot抓取网站时,我会看到有角度的括号。

我迷路了。

非常感谢提前

我的nginx配置:

server {
listen 80;
listen [::]:80 default_server ipv6only=on;
return 301 https://$host$request_uri;

}

server {
listen 443;
server_name www.thedomain.com;

ssl on;
# Use certificate and key provided by Let's Encrypt:
ssl_certificate /etc/letsencrypt/live/www.thedomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.thedomain.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

location / {
    # Proxy_pass configuration
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_max_temp_file_size 0;
    proxy_pass http://0.0.0.0:3000;
    proxy_redirect off;
    proxy_read_timeout 240s;
}

}

}

1 个答案:

答案 0 :(得分:0)

这应该有效:

server {
listen 80;
listen [::]:80 default_server ipv6only=on;
return 301 https://$host$request_uri;

}

server {
listen 443;
server_name www.thedomain.com;

ssl on;
# Use certificate and key provided by Let's Encrypt:
ssl_certificate /etc/letsencrypt/live/www.thedomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.thedomain.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

location / {
    #proxy_set_header X-Prerender-Token YOUR_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;
    }

    # Proxy_pass configuration
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_max_temp_file_size 0;
    proxy_pass http://0.0.0.0:3000;
    proxy_redirect off;
    proxy_read_timeout 240s;
}

}

}

替换YOUR_TOKEN后,不要忘记从X-Prerender-Token的proxy_set_header中删除#。

然后,您需要测试您的URL是否附加?_escaped_fragment_ =查询参数。如果您的网址如下:

https://www.example.com/

您将对它们进行测试:

https://www.example.com/?_escaped_fragment_=

这将强制推出一个预呈现的页面,以便您可以在那里查看来源,并确保您看到的是静态HTML页面,而不仅仅是标签。